1 / 44

Literals

Literals. Integer literals:. Octal: 010, 027L, 017777777777 Hexadecimal: 0x8, 0xaL, 0x7fffffff Binary: 0b1000, 0b101010L Decimal: 8, 64, 2147483647. Literals. String literals:. “A” “Hello” “This is a good day for learning java.”. Literals. float x = 2.5;.

ziva
Télécharger la présentation

Literals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Literals Integer literals: • Octal: 010, 027L, 017777777777 • Hexadecimal: 0x8, 0xaL, 0x7fffffff • Binary: 0b1000, 0b101010L • Decimal: 8, 64, 2147483647

  2. Literals String literals: • “A” • “Hello” • “This is a good day for learning java.”

  3. Literals float x = 2.5; • Why does this produce an error?

  4. Literals float x = 2.5; • Floating-point default datatype is ‘double’.

  5. Literals float x = (float) 2.5; • Solution 1: cast 2.5 to float

  6. Literals float x = 2.5f; • Solution 2: use ‘f’ or ‘F’ identifier

  7. Literals char c = ‘A’; • char literals are delimited by single quotes

  8. Literals char c = ‘A’; int x = 45 + c; System.out.println( x );

  9. numeric boolean integral floatingpoint character integer boolean char byte short int long float double 16 + 8 +/- 16 +/- 32 +/- 64 +/- 32 +/- 64 +/-

  10. Literals char c = ‘A’; int x = 45 + c; System.out.println( x ); • char is a numeric integral type (i.e. evaluates to an integer • What is the output?

  11. Literals • char c = ‘A’; char c = ‘\u0061’; • Unicode escape sequence is backslash + u + 4 digit hex • Unicode can be used ANYWHERE in java code

  12. Literals ch\u0061r c = ‘\u0061’;

  13. 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 !

  14. Numeric Promotion int x = 3 / 2 * 4 f

  15. Numeric Promotion int x = 3 / 2 * 4 f • The ‘f’ denotes ‘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

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

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

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

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

  21. Numeric Promotion float x = 3 / 2 * 4 f • Another method!

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

  23. Numeric Promotion float x = 3.0 / 2 * 4 • Another method! • Literals are promoted to ‘double’ datatype

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

  25. Numeric Promotion float x = (float) 3.0 / 2 * 4 • Finished!

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

  27. Numeric Promotion float x = (float) (3.0 / 2.0) * 4 • Phew!

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

  29. Literals long x = 2147483647; • What datatype is the literal?

  30. Literals long x = 2147483648; • Why does the above produce an error? • By default, integer literals are datatype ‘int’, and 2,147,483,648 is out of range.

  31. Literals long x = 2147483648L; • Solution: must use the ‘l’ or ‘L’ identifier

  32. Literals int x = 2147483647 + 1; System.out.println( x ); • What is the output?

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

  34. Literals 0 1111111 11111111 11111111 11111111 + 0 0000000 00000000 00000000 00000001 = 1 0000000 00000000 00000000 00000000 Sign bit is now 1

  35. Literals 2,147,483,647 0 1111111 11111111 11111111 11111111 + 0 0000000 00000000 00000000 00000001 = 1 0000000 00000000 00000000 00000000 1 - 2,147,483,648

  36. Implicit Narrowing Conversion 0 1000000 10000001 • What is the decimal value of this bit pattern?

  37. Implicit Narrowing Conversion 0 1000000 10000001 • 16513 ( 16384 + 128 + 1 )

  38. Implicit Narrowing Conversion 0 1000000 10000001 short x = 16513; ‘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.)

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

  40. loss of precision with casting 0 1000000 10000001 short x = 16513; byte b = (byte) x;

  41. loss of precision with casting short x = 16513; byte b = (byte) x; 0 1000000 10000001 bye, bye….

  42. loss of precision with casting short x = 16513; byte b = (byte) x; 0 1000000 10000001 Value of x is -127

  43. Exercise! int x = 457; double y = (byte) x; System.out.print( y );

More Related