Advertisement
Guest User

Java Fundamentals - Literals

a guest
Apr 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. Literals:
  2. • int x = 10;
  3. § Int is both the datatype and keyword.
  4. § X is the name of the variable or the identifier.
  5. § 10 is considered the constant value or the literal.
  6. □ Note: A constant value which can be assigned to a variable is called a literal.
  7. • There are four integral data types.
  8. § List: byte, short, int, and long
  9. • There are two forms of literal representation. These are decimal and octal.
  10. § Decimal literals (base-10)
  11. □ Range: 0 to 9
  12. □ Int x = 10; <-- decimal form
  13. § Octal literals (base-8) can handle
  14. □ Range: [0 to 7]
  15. □ int x = 010;
  16. □ Side note:
  17. □ The JVM reads literals by default in decimal form.
  18. □ One can force a literal to be treated as an octal literal by prefixing the literal with the number 0.
  19. § Hexadecimal form (base-16)
  20. □ Range: [0 to 9] and [a to f] -
  21. □ We can use both lowercase and uppercase characters.
  22. ◊ IMPORTANT: This is one of very few areas where java is not case sensitive.
  23. □ Int x = 0x10;
  24. □ Side note:
  25. □ The JVM reads literals by default in decimal form.
  26. □ Once can force a literal to be treated as a hexadecimal literal by prefixing it with 0X (the x can be lowercase or uppercase).
  27. § Examples: int x = 10; <-- valid || int x = 0786; <-- CE: integer number too large || int x = 0777; <-- valid || int x = 0XFace; <-- valid || int x = 0XBeef; <-- valid || int x = 0XBeer; <-- CE: error: ';' expected
  28. § Detailed Example 1: class test { public static void main(String[] args) { int x = 10; int y = 010; int z = 0X10; System.out.print(z + "..." + y + "…" + z); } } //prints 10 8 16 in this order
  29. § NOTE: JVM only prints values in decimal form.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement