Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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

2 Data Types References::  Data Type:

3 Data Types References: http://pc.net/helpcenter/answers/primitive_and_non_primitive_data  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.

4 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

5 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  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

6 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); }

7 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); }

8 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  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 = -20000

9 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);  }

10 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);  }

11 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  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 = 100000, int b = -200000

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

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

14 Data Types References : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html  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 2 32 -1=4294967295  Example can shown after String chapter.

15 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  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 = 100000L, long b = -200000L

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

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

18 Data Types References : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html  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 2 64 -1 = 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.

19 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  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

20 Data Types References : www.stackoverflow.com/questions/1650505/what-is-the-inclusive-range-of-float-and-double-in-java  Primitive Data Types  float:  Minimum (negative) values:  = - 3.40282346638528860 E+38  To  = - 1.40129846432481707 E-45  Maximum (positive )values:  = 1.40129846432481707 E-45  To  = 3.40282346638528860 E+38

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

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

23 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  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

24 Data Types References : www.stackoverflow.com/questions/1650505/what-is-the-inclusive-range-of-float-and-double-in-java  Primitive Data Types  double:  Minimum (negative) values:  = -1.79769313486231570 E+ 308  To  = -4.9 E-324  Maximum (positive )values:  = 4.9 E-324  To  = 1.79769313486231570 E+308

25 Double Example 1 Program: Java 0014 Java011_DoubleExample  class Java011_DoubleExample  {  public static void main(String args[])  {  double doubleNum1=-1.79769313486231570e+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=1.79769313486231570e+308d;  System.out.println("Value stored in numbers5=" +doubleNum5);  }}

26 Double Example 2 Program: Java 0014 Java012_DoubleExample  class Java012_DoubleExample  {  public static void main(String args[])  {  double doubleNum1=-1.79769313486231590e+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=1.79769313486231590e+308d;  System.out.println("Value stored in numbers5=" +doubleNum5);  }}

27 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  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

28 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);  }

29 Data Types References : http://www.tutorialspoint.com/java/java_basic_datatypes.htm  Primitive Data Types  char:  char data type is a single 16-bit Unicode character. www.ssec.wisc.edu/~tomw/java/unicode.html www.ssec.wisc.edu/~tomw/java/unicode.html  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‘

30 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);  }

31 Data Types References: http://pc.net/helpcenter/answers/primitive_and_non_primitive_data  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.

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

33 String References: www.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html  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.

34 String References: www.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html  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.

35 String References: www.docs.oracle.com/javase/tutorial/java/data/strings.html  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.

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

37 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”;

38 String References: www.beginnersbook.com/2013/12/java-strings/  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.

39 String References: www.beginnersbook.com/2013/12/java-strings/  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.

40 String References: www.beginnersbook.com/2013/12/java-strings 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.

41 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

42 String Reference : www.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html  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)

43 String Reference : www.beginnersbook.com/2013/12/java-string-startswith-method-example/ 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.

44 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.


Download ppt "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."

Similar presentations


Ads by Google