Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.

Similar presentations


Presentation on theme: "Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64."— Presentation transcript:

1 Programming in java Lecture-3 (a)

2 Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64 bit signed integer float32 bit signed real number double64 bit signed real number char16 bit Unicode character (ASCII and beyond) boolean1 bit true or false value StringA sequence of characters between double quotes ( "" )

3 Boolean data type  This data type can store only two values; true and false. Declaring a boolean variable is the same as declaring any other primitive data type like int, float, char.  boolean response = false; //Valid  boolean answer = true; //Valid  boolean answer = 9943; //Invalid,  boolean response = “false”; // Invalid,  This is return type for relational & conditional operators.

4 Comments  // One line comment  /* multiple line Comment */ /*** These comment used in documentation *This is comment. *This is comment */

5 What should be in comment?  Author name  Purpose of function

6 Default values for primitive members  When a primitive type data is a member of a class, it’s guaranteed to get a default value even if you don’t initialize it.  Not true for those local variables!!  There will be compile error if you use it without initialization Primitive typeDefault booleanfalse char ‘ \u0000 ’ (null) byte(byte)0 short(short)0 int0 long0L float0.0f double0.0d

7 String (+) Operator String Concatenation "Now is " + "the time." "Now is the time."

8 String (+) Operator Automatic Conversion to a String If either expression_1 If either expression_1 or expression_2 evaluates to a string the other will be converted to a string if needed. The result will be their concatenation. expression_1 + expression_2

9 String (+) Operator Automatic Conversion with Primitives "The number is " + 4 "The number is " + "4" "The number is 4"

10 Increment Operator i + + first use the value of i and then increment it by 1.  ‘i + +’ equivalent to ‘i = (i)+1’. // postfix increment + + i first increment the value of i by 1 and then use it.  ‘+ + i’ equivalent to ‘i = (i+1)’. // prefix increment  int i=2; System.out.print(“ i = ”+ i++);//print 2, then i becomes 3 System.out.print(“ i = ”+ ++i);//add 1 to i, then print 4

11 Decrement Operator i - - first use the i ’s value and then decrement it by one  i - - equivalent to (i)-1. // postfix decrement - - i first decrement i ’s value by one and then use it.  - - i equivalent to (i-1). // prefix decrement  int i=5;  System.out.print(“i = ” + i--);//print 5, then i becomes 4  System.out.print(“i = ” + --i);//subtract 1 from i, then print 3

12 Relational Operator  a < b a less than b. (true/false)  a <= b a less than or equal b. (true/false)  a > b a greater than b. (true/false)  a >= b a greater than or equal to b. (true/false)  These operations always return a boolean value.  System.out.println(“23 is less than 65 ” +23<65); // true  System.out.println(“5 is greater than or equal to 25.00?” + 5>=25.00); // false

13 Equality Operator  a = = b a equal to b. (true/false)  a ! = b a not equal to b. (true/false)  boolean equal = 12 = = 150; // false  boolean again_equal = ‘r’= = ‘r’); // true  boolean not_equal = 53!=90); // true

14 Logical (&&) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f && f " + (f && f)); System.out.println("f && t " + (f && t)); System.out.println("t && f " + (t && f)); System.out.println("t && t " + (t && t)); } > java Example f && f false f && t false t && f false t && t true >

15 Logical (||) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f || f " + (f || f)); System.out.println("f || t " + (f || t)); System.out.println("t || f " + (t || f)); System.out.println("t || t " + (t || t)); } > java Example f || f false f || t true t || f true t || t true >

16 Logical (!) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("!f " + !f); System.out.println("!t " + !t); } > java Example !f true !t false >

17 Logical Operators (Bit Level) & | ^ ~ int a = 10; // 00001010 = 10 int b = 12; // 00001100 = 12 a00000000000000000000000000001010 10 b00000000000000000000000000001100 12 a & b00000000000000000000000000001000 8 a00000000000000000000000000001010 10 b00000000000000000000000000001100 12 a | b00000000000000000000000000001110 14 a00000000000000000000000000001010 10 b00000000000000000000000000001100 12 a ^ b00000000000000000000000000000110 6 a00000000000000000000000000001010 10 ~a11111111111111111111111111110101 -11 & AND | OR ^ XOR ~ NOT

18 Shift Operators (Bit Level) > >>> Shift Left <<Fill with Zeros Shift Right >>Based on Sign Shift Right >>>Fill with Zeros

19 Shift Operators > int a = 3; //...00000011 = 3 int b = -4; //...11111100 = -4 a 00000000000000000000000000000011 3 a << 2 00000000000000000000000000001100 12 b 11111111111111111111111111111100 -4 b << 2 11111111111111111111111111110000 -16 << Left >> Right a 00000000000000000000000000000011 3 a >> 2 00000000000000000000000000000000 0 b 11111111111111111111111111111100 -4 b >> 2 11111111111111111111111111111111 -1

20 Shift Operator >>> int a = 3; //...00000011 = 3 int b = -4; //...11111100 = -4 >>> Right 0 a 00000000000000000000000000000011 3 a >>> 2 00000000000000000000000000000000 0 b 11111111111111111111111111111100 -4 b >>> 2 00111111111111111111111111111111 +big

21 Shift Operator Examples public class Example { public static void main(String[] args) { int a = 3; //...00000011 = 3 int b = -4; //...11111100 = -4 System.out.println("a<<2 = " + (a<<2)); System.out.println("b<<2 = " + (b<<2)); System.out.println("a>>2 = " + (a>>2)); System.out.println("b>>2 = " + (b>>2)); System.out.println("a>>>2 = " + (a>>>2)); System.out.println("b>>>2 = " + (b>>>2)); } > java Example a<<2 = 12 b<<2 = -16 a>>2 = 0 b>>2 = -1 a>>>2 = 0 b>>>2 = 1073741823 >

22 Shift Operator >>> and Automatic Arithmetic Promotion byte a = 3; // 00000011 = 3 byte b = -4; // 11111100 = -4 byte c; c = (byte) a >>> 2 c = (byte) b >>> 2 >>> Right Fill 0 a 00000011 3 a >>> 2 00000000000000000000000000000000 0 c = (byte) 00000000 0 b 11111100 -4 b >>> 2 00111111111111111111111111111111 1073741823 c = (byte) Much to big for byte 11111111 -1

23 Type Casting  When a large data size variable is assigned to small data size variable.

24 DeclarationSize and TypeNumber Range byte b;// 8 bit integer -2 -7 to +2 +7 -1 short s;// 16 bit integer -2 -15 to +2 +15 -1 char c;// 16 bit Unicode 0 to +2 +16 int i;// 32 bit integer -2 -31 to +2 +31 -1 long l;// 64 bit integer -2 -63 to +2 +63 -1 float f;// 32 bit floating pointIEEE754 Standard double d;// 64 bit floating pointIEEE754 Standard The Primitive Numbers

25 Casting the Primitives b = (byte)s; b = (byte)c; b = (byte)i; b = (byte)l; b = (byte)f; b = (byte)d; i = (int)b; i = (int)s; i = (int)c; i = (int)l; i = (int)f; i = (int)d; l = (long)b; l = (long)s; l = (long)c; l = (long)i; l = (long)f; l = (long)d; s = (short)b; s = (short)c; s = (short)i; s = (short)l; s = (short)f; s = (short)d; 2's Complement c = (char)b; c = (char)s; c = (char)i; c = (char)l; c = (char)f; c = (char)d; Positive Only f = (float)b; f = (float)s; f = (float)c; f = (float)i; f = (float)l; f = (float)d; d = (double)b; d = (double)s; d = (double)c; d = (double)i; d = (double)l; d = (double)f; Floating Point b = (byte)s; b = (byte)c; b = (byte)i; b = (byte)l; b = (byte)f; b = (byte)d; i = b; i = s; i = c; i = (int)l; i = (int)f; i = (int)d; l = b; l = s; l = c; l = i; l = (long)f; l = (long)d; s = b; s = (short)c; s = (short)i; s = (short)l; s = (short)f; s = (short)d; 2's Complement c = (char)b; c = (char)s; c = (char)i; c = (char)l; c = (char)f; c = (char)d; Positive Only f = b; f = s; f = c; f = i; f = l; f = (float)d; d = b; d = s; d = c; d = i; d = l; d = f; Floating Point

26 Acceptable Implicit Casts char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Illegal b = l; l = f; c = s; OK l = b; i = c; f = l

27 char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Automatic Promotion with Arithmetic Illegal Casts s = s + b; s = s + s; OK s = (short)(s + b); s = (short)(s + s); Arithmetic is never done in 8 or 16 bit containers.

28 char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Arithmetic Promotion with Mixed Primitives Illegal Casts i = i + l; f = f + d; l = l + f; OK i = (int)(i + l); d = (double)(f + d); l = (long)(l + f); Arithmetic is done in the "widest" type.

29 Implicit Casts in Method Calls char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Illegal i = st.indexOf(f); OK i = st.indexOf(c); i = st.indexOf(b); For: String st; and, public int indexOf(int ch);

30 Program(find errors in following program) Class one { public static void main(String arp[]) { int i; Byte b=20; b=b+1; System.out.println(i); } Integer i is not initialised. Every local variable must be initialised. b is automatically converted into integer data type and integer value can’t be assigned to byte variable

31 int a=12; float f=12.2f; double d=4.5; double result = a * f + d ; float type a * f

32 = int * float + double = float * float + double = float + double = double + double = double


Download ppt "Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64."

Similar presentations


Ads by Google