Data Types References: www.en.wikipedia.org/wiki/Data_type  Data Type:  In computer science and computer programming, a data type or simply type is a.

Slides:



Advertisements
Similar presentations
Introduction to Programming G51PRG University of Nottingham Revision 1
Advertisements

DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Java Syntax Primitive data types Operators Control statements.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
August 6, 2009 Data Types, Variables, and Arrays.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Java Data Types.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1.2 Primitive Data Types and Variables
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Chapter One Lesson Three DATA TYPES ©
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 06 – Java Datatypes Webpage:
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
String and StringBuffer classes
Chapter 6: Data Types Lectures # 10.
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
Selenium WebDriver Web Test Tool Training
Java package classes Java package classes.
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 2.
IDENTIFIERS CSC 111.
MSIS 655 Advanced Business Applications Programming
Introduction to Primitives
Java Basics Data Types in Java.
Names of variables, functions, classes
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a classification identifying one of various types of data,  such as real, integer or Boolean, that determines the possible values for that type;  the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored.  Types of Data Type:  primitive data types  non-primitive data types

Data Types References::  Data Type:

Data Types References:  Primitive Data Types:  The term "data type" and "primitive data type" are often used interchangeably.  Primitive data types are predefined types of data, which are supported by the programming language.  For example,integer, character, and string are all primitive data types.  Programmers can use these data types when creating variables in their programs.  For example, a programmer may create a variable called "lastname" and define it as a string data type.  The variable will then store data as a string of characters. Primitive data types are predefined by the language and named by a keyword.

Data Types References:  Primitive Data Types  Java defines eight primitive types:  TypeExplanation  BooleanA true or false value  ByteAn 8-bit (1-byte) integer value  ShortA 16-bit (2-byte) integer value  IntA 32-bit (4-byte) integer value  LongA 64-bit (8-byte) integer value  FloatA 32-bit (4-byte) floating-point value  DoubleA 64-bit (8-byte) floating-point value  CharA 16-bit character using the Unicode encoding scheme

Data Types References :  Primitive Data Types  There are eight primitive data types supported by Java.  byte:  Byte data type is an 8-bit signed two's complement integer.  Minimum value is -128 (-2^7)  Maximum value is 127 (inclusive)(2^7 -1)  Default value is 0  Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.  Example: byte a = 100, byte b = -50

Byte Example 1 Program: Java 0014 Java001_ByteExample class Java001_ByteExample { public static void main(String args[]) { byte num1=-128; System.out.println("Value stored in numbers=" +num1); byte num2=0; System.out.println("Value stored in numbers=" +num2); byte num3=127; System.out.println("Value stored in numbers=" +num3); }

Byte Example 2 Program: Java 0014Java002_ByteExample class Java002_ByteExample { public static void main(String args[]) { byte num1=128; System.out.println("Value stored in numbers=" +num1); byte num2=-129; System.out.println("Value stored in numbers=" +num2); }

Data Types References :  Primitive Data Types  short:  Short data type is a 16-bit signed two's complement integer.  Minimum value is -32,768 (-2^15)  Maximum value is 32,767 (inclusive) (2^15 -1)  Short data type can also be used to save memory as byte data type.  A short is 2 times smaller than an int  Default value is 0.  Example: short s = 10000, short r =

Short Example 1 Program: Java 0014 Java003_ShortExample  class Java003_ShortExample  {  public static void main(String args[])  {  short shortNum1=-32768;  System.out.println("Value stored in numbers1=" +shortNum1);   short shortNum3=32767;  System.out.println("Value stored in numbers3=" +shortNum3);   short shortNum4=0;  System.out.println("Value stored in numbers4=" +shortNum4);  }

Short Example 2 Program: Java 0014 Java004_ShortExample  class Java004_ShortExample  {  public static void main(String args[])  {  short shortNum1=-32769;  System.out.println("Value stored in numbers1=" +shortNum1);   short shortNum3=32768;  System.out.println("Value stored in numbers3=" +shortNum3);  }

Data Types References :  Primitive Data Types  int:  Int data type is a 32-bit signed two's complement integer.  Minimum value is - 2,147,483,648.(-2^31)  Maximum value is 2,147,483,647(inclusive).(2^31 -1)  Int is generally used as the default data type for integral values unless there is a concern about memory.  The default value is 0.  Example: int a = , int b =

Int Example 1 Program: Java 0014 Java005_IntExample  class Java005_IntExample  {  public static void main(String args[])  {  int intNum1= ;  System.out.println("Value stored in numbers1=" +intNum1);   int intNum2=0;  System.out.println("Value stored in numbers2=" +intNum2);   int intNum3= ;  System.out.println("Value stored in numbers3=" +intNum3);   }

Int Example 2 Program: Java 0014 Java006_IntExample  class Java006_IntExample  {  public static void main(String args[])  {  int intNum1= ;  System.out.println("Value stored in numbers1=" +intNum1);   int intNum3= ;  System.out.println("Value stored in numbers3=" +intNum3);   }

Data Types References :  In Java SE 8 and later  you can use the int data type to represent an unsigned 32-bit integer  minimum value of unsigned int 0  maximum value of unsigned int =  Example can shown after String chapter.

Data Types References :  Primitive Data Types  long:  Long data type is a 64-bit signed two's complement integer.  Minimum value is -9,223,372,036,854,775,808.(-2^63)  Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)  This type is used when a wider range than int is needed.  Default value is 0L.  Example: long a = L, long b = L

Long Example 1 Program: Java 0014 Java007_LongExample  class Java007_LongExample  {  public static void main(String args[])  {  long longNum1= l;  System.out.println("Value stored in numbers1=" +longNum1);  long longNum2=0;  System.out.println("Value stored in numbers2=" +longNum2);  long longNum3= l;  System.out.println("Value stored in numbers3=" +longNum3);   }

Long Example 2 Program: Java 0014 Java008_LongExample  class Java008_LongExample  {  public static void main(String args[])  {  long longNum1= L;  System.out.println("Value stored in numbers1=" +longNum1);  long longNum3= L;  System.out.println("Value stored in numbers3=" +longNum3);   }

Data Types References :  In Java SE 8 and later  you can use the long data type to represent an unsigned 64-bit long  Minimum value of unsigned long 0  Maximum value of = 18,446,744,073,709,551,615  Use this data type when you need a range of values wider than those provided by int.  Example can shown after String chapter.

Data Types References :  Primitive Data Types  float:  Float data type is a single-precision 32-bit IEEE 754 floating point.  Float is mainly used to save memory in large arrays of floating point numbers.  Default value is 0.0f.  Float data type is never used for precise values such as currency.  Example: float f1 = 234.5f

Data Types References :  Primitive Data Types  float:  Minimum (negative) values:  = E+38  To  = E-45  Maximum (positive )values:  = E-45  To  = E+38

Float Example 1 Program: Java 0014 Java009_FloatExample  class Java009_FloatExample{  public static void main(String args[])  {  float floatNum1= E+38f;  System.out.println("Value stored in numbers1=" +floatNum1);  float floatNum2= e-45f;  System.out.println("Value stored in numbers2=" +floatNum2);   float floatNum3=0.0f;  System.out.println("Value stored in numbers3=" +floatNum3);  float floatNum4= e-45f;  System.out.println("Value stored in numbers4=" +floatNum4);  float floatNum5= e+38f;  System.out.println("Value stored in numbers5=" +floatNum5);  }

Float Example 2 Program: Java 0014 Java010_FloatExample  class Java010_FloatExample  {  public static void main(String args[])  {  float floatNum1= E+39f;  System.out.println("Value stored in numbers1=" +floatNum1);  float floatNum2= e-46f;  System.out.println("Value stored in numbers2=" +floatNum2);   float floatNum4= e-46f;  System.out.println("Value stored in numbers4=" +floatNum4);   float floatNum5= e+39f;  System.out.println("Value stored in numbers5=" +floatNum5);  }

Data Types References :  Primitive Data Types  double:  double data type is a double-precision 64-bit IEEE 754 floating point.  This data type is generally used as the default data type for decimal values, generally the default choice.  Double data type should never be used for precise values such as currency.  Default value is 0.0d.  Example: double d1 = 123.4

Data Types References :  Primitive Data Types  double:  Minimum (negative) values:  = E+ 308  To  = -4.9 E-324  Maximum (positive )values:  = 4.9 E-324  To  = E+308

Double Example 1 Program: Java 0014 Java011_DoubleExample  class Java011_DoubleExample  {  public static void main(String args[])  {  double doubleNum1= e+308d;  System.out.println("Value stored in numbers1=" +doubleNum1);  double doubleNum2=-4.9e-324d;  System.out.println("Value stored in numbers2=" +doubleNum2);   double doubleNum3=0.0d;  System.out.println("Value stored in numbers3=" +doubleNum3);   double doubleNum4=4.9e-324d;  System.out.println("Value stored in numbers4=" +doubleNum4);  double doubleNum5= e+308d;  System.out.println("Value stored in numbers5=" +doubleNum5);  }}

Double Example 2 Program: Java 0014 Java012_DoubleExample  class Java012_DoubleExample  {  public static void main(String args[])  {  double doubleNum1= e+308d;  System.out.println("Value stored in numbers1=" +doubleNum1);  double doubleNum2=-4.9e-325d;  System.out.println("Value stored in numbers2=" +doubleNum2);   double doubleNum4=4.9e-325d;  System.out.println("Value stored in numbers4=" +doubleNum4);   double doubleNum5= e+308d;  System.out.println("Value stored in numbers5=" +doubleNum5);  }}

Data Types References :  Primitive Data Types  boolean:  boolean data type represents one bit of information.  There are only two possible values: true and false.  This data type is used for simple flags that track true/false conditions.  Default value is false.  Example: boolean one = true

Boolean Example 1 Program: Java 0014 Java013_BooleanExample  class Java013_BooleanExample  {  public static void main(String args[])  {   boolean booleanNum;  booleanNum = false;  System.out.println("Value is=" +booleanNum);  booleanNum = true;  System.out.println("Value is=" +booleanNum);  }

Data Types References :  Primitive Data Types  char:  char data type is a single 16-bit Unicode character.  Minimum value is '\u0000' (or 0).  Maximum value is '\uffff' (or 65,535 inclusive).  Char data type is used to store any character.  Example: char letterA ='A‘

Char Example 1 Program: Java 0014 Java014_CharExample  class Java014_CharExample  {  public static void main(String args[])  {   char charNum1, charNum2, charNum3;  charNum1 = 'D';  System.out.println("Value is=" +charNum1);  charNum2 = 64;  System.out.println("Value is=" +charNum2);  charNum3 = 70;  System.out.println("Value is=" +charNum3);  }

Data Types References:  Non - Primitive Data Types  Non-primitive data types are not defined by the programming language, but are instead created by the programmer.  They are sometimes called "reference variables," or "object references," since they reference a memory location, which stores the data.  In the Java programming language, non-primitive data types are simply called "objects" because they are created, rather than predefined.  While an object may contain any type of data, the information referenced by the object may still be stored as a primitive data type.  For Example: - Class, Array, String, Interface, Collection etc.

Data Types Program:  Non - Primitive Data Types  For Example: -  Class  Array  String  Interface  Collection etc.

String References:  Strings:-  A Java String contains an immutable sequence of Unicode characters.  Unlike C/C++, where string is simply an array of char, A Java String is an object of the class “java.lang”.  Java String is, however, special. Unlike an ordinary class:-  String is associated with string literal in the form of double-quoted texts such as "Hello, world!". You can assign a string literal directly into a String variable, instead of calling the constructor to create a String instance.

String References:  Strings:-  The '+' operator is overloaded to concatenate two String operands. '+' does not work on any other objects such as Point and Circle.  String is immutable. That is, its content cannot be modified once it is created.  For example, the method toUpperCase() constructs and returns a new String instead of modifying the its existing content.

String References:  Strings:-  Strings, which are widely used in Java programming, are a sequence of characters.  In the Java programming language, strings are objects.  The Java platform provides the String class to create and manipulate strings.

String References:  There are two ways to create a String in Java:-  String literal  Using new keyword

String References: Program : Java 0014 Java015_StringExample  String literal :-  In java, Strings can be created like this:  Assigning a String literal to a String instance: String str1 = "Welcome"; String str2 = "Welcome”;

String References:  The problem with this approach: -  As I stated in the beginning that String is an object in Java.  However we have not created any string object using new keyword above.  The compiler does that task for us it creates a string object having the string literal (that we have provided, in this case it is “Welcome”) and assigns it to the provided string instances.

String References:  But ?...  if the object already exist in the memory it does not create a new Object rather it assigns the same old object to the new instance, that means even though we have two string instances above(str1 and str2) compiler only created on string object (having the value “Welcome”) and assigned the same to both the instances.  For example there are 10 string instances that have same value, it means that in memory there is only one object having the value and all the 10 string instances would be pointing to the same object.  What if we want to have two different object with the same string? For that we would need to create strings using new keyword.

String References: Program : Java 0014 Java016_StringExample  Using new keyword  As we saw above that when we tried to assign the same string object to two different literals, compiler only created one object and made both of the literals to point the same object.  To overcome that approach we can create strings like this: String str1 = new String("Welcome"); String str2 = new String("Welcome"); In this case compiler would create two different object in memory having the same text.

String Program : Java 0014 Java017_StringExample AND Java018_StringExample  The commonly-used method in the String class :-  Length methods:-  int length() :- It returns the length of the String  boolean isEmpty() :- It returns true if, and only if, length() is 0. :-It is same as thisString.length == 0

String Reference :  The commonly-used method in the String class :-  Comparison :-  boolean startsWith(String another)  boolean startsWith(String another, int fromIndex)  boolean endsWith(String another)  boolean equals(String another)  boolean equalsIgnoreCase(String another)  int compareTo(String another)  int compareToIgnoreCase(String another)

String Reference : Program : Java 0014 Java019_StringExample  The commonly-used method in the String class :-  Comparison :-  boolean startsWith(String another) :- The method startsWith() is used for checking prefix of a String. It returns a boolean value true or false based on whether the specified string is prefix of the particular String or not. Simply:-  boolean startsWith(String str) :- It returns true if the str is a prefix of the String.

String Program : Java 0014 Java020_StringExample  The commonly-used method in the String class :-  Comparison :-  boolean startsWith(String another, int fromIndex) :- It tests if the substring of this string beginning at the specified index starts with the specified another string.