Download presentation

Presentation is loading. Please wait.

Published byAshtyn Mauger Modified over 2 years ago

1
Literals Why does this produce an error? float x = 2.5;

2
Literals Floating-point default datatype is double. float x = 2.5;

3
Literals Solution 1: cast 2.5 to float float x = (float) 2.5;

4
Literals Solution 2: use f or F identifier float x = 2.5f;

5
Literals char literals are delimited by single quotes char c = a;

6
Literals char c = a; int x = 3 + c; System.out.println( x );

7
boolean floating pointintegral floatdoublebooleancharshortintlong characterinteger byte /- 16 +/- 32 +/- 64 +/- 32 +/- 64 +/- numeric

8
Literals char is a numeric integral type (i.e. evaluates to an integer What is the output? char c = a; int x = 3 + c; System.out.println( x );

9
Literals char c = a; char c = \u0061; Unicode escape sequence is backslash + u + 4 digit hex Unicode can be used ANYWHERE in java code

10
Literals ch\u0061r c = \u0061;

11

12
Numeric Promotion int x = 3 / 2 * 4 3,2 and 4 are int literals Integer Division! Result of 3 / 2 is : 1 Therefore, x=3/2*4 is the same as x=1*4 x = 4 !

13
Numeric Promotion int x = 3 / 2 * 4 f

14
Numeric Promotion int x = 3 / 2 * 4 f The f denotes float datatype

15
Numeric Promotion int x = 3 / 2 * 4 f The f denotes float datatype The binary operator / must have same datatype on both sides. Therefore, 2 is promoted to float datatype

16
Numeric Promotion int x = 3 / 2 * 4 f The f denotes float datatype The binary operator / must have same datatype on both sides. Therefore, 2 is promoted to float datatype Now the binary operator * must promote its right hand operand. 4 is promoted to float.

17
Numeric Promotion int x = 3 / 2 * 4 f The right hand expression evaluates to 6.0 This will not compile because float to integer is a narrowing conversion requiring an explicit cast

18
Numeric Promotion int x = (int) 3 / 2 * 4 f Explicit cast ! System.out.print (x) : 4

19
Numeric Promotion float x = 3 / 2 * 4 f cast not needed System.out.print (x) : 6.0

20
Numeric Promotion float x = 3 / 2 * 4 f Another method!

21
Numeric Promotion float x = 3.0 / 2 * 4 Another method! Literals are promoted to which datatype???

22
Numeric Promotion float x = 3.0 / 2 * 4 Another method! Literals are promoted to double datatype

23
Numeric Promotion float x = 3.0 / 2 * 4 Another method! Literals are promoted to double datatype This expression will not compile because it requires an explicit narrowing conversion from double to float

24
Numeric Promotion float x = (float) 3.0 / 2 * 4 Finished!

25
Numeric Promotion float x = (float) 3.0 / 2.0 * 4 Does not compile.. Cast only operates on left hand operand 3.0

26
Numeric Promotion float x = (float) (3.0 / 2.0) * 4 Phew!

27
Finally ! evaluates left to right 3 / 2 is integer division, result is 1 Int 1 is promoted to double * 4.0 evaluates to double 4.0 double x = 3 / 2 * 4.0

28
Literals What is the output? int x = ; System.out.println( x );

29
Literals Int datatype is 32 bits: 1 sign bit + 31 bits to represent the number Sign bit: 0 means positive, 1 means negative Sign bit: +/-

30
Literals = Sign bit is now 1

31
Literals = ,147,483, ,147,483,648

32
Implicit Narrowing Conversion What is the decimal value of this bit pattern?

33
Implicit Narrowing Conversion ( )

34
Implicit Narrowing Conversion short x = 16513; is an integer literal. Default type is int. No cast required here because integral types (including char) can perform implicit narrowing conversion as long as the value is in range. ( float c = 2.5 is illegal, because floating point types cannot perform implicit narrowing conversion.)

35
Implicit Narrowing Conversion Rule 1: Both datatypes must be integer types Rule 2: Value must be within range of target datatype

36
loss of precision with casting short x = 16513; byte b = (byte) x;

37
loss of precision with casting short x = 16513; byte b = (byte) x; bye, bye….

38
loss of precision with casting short x = 16513; byte b = (byte) x; Value of x is -127

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google