Skip to main content

Summary

Boolean Literals

  1. A boolean type variable can hold either true or false.
  2. A boolean type variable can't hold numeric value. a. 0 not equals to false b. Non Zero values not equals to true
  3. Boolean literals - true and false are reserved words and must be in lower case. a. true not same as TRUE, True, TrUe etc b. false not same as FALSE, False, FaLsE etc

Character Literals

  1. A char type variable can hold following:
    • Single character enclosed in single quotation marks
    • Escape Sequence
    • ASCII Value
    • UNICODE Character
    • Octal Character
  2. SPACE is a valid character so it can be placed in single quotation marks.
  3. TAB is a valid character so it can be placed in single quotation marks.
  4. Backslash character () is used to form escape sequence so only backslash character can't be placed in single quotation marks.
  5. Single Quote is used to form character literal so only single quote can't be placed in single quotation marks.
  6. There are some characters like Backslash, Single quote which can't be placed in single quotes as it is. Those special characters have to be represented as Escape Sequence.
  7. Every character enclosed in single quotation marks will have an integer equivalent value called as ASCII value.
  8. char type variable can hold integer value ranging from 0 - 65535 only.
  9. char type variable can hold UNICODE value ranging from \u0000 - \uFFFF only.
  10. char type variable can hold OCTAL value ranging from \0 - \377 only.
  11. When you print the char type variable which holds Integer, UNICODE or Octal value then it displays corresponding character representation.
  12. If Integer, UNICODE value don't have character representation then it displays ? which indicates no character defined for that value.
  13. When you assign character literals to int type variable then internally ASCII value of that character will be assigned.

String Literals

  1. Empty String Literal is allowed whereas empty character literal is not allowed.
  2. If reference variable of String type contains null value then it is called as NULL String.
  3. You can't refer any members with NULL String. If you do so it results in NullPointerException.
  4. If reference variable of String type contains zero character in double quotations marks then it is called as EMPTY String.
  5. When Java compiler encounters any UNICODE/Octal value in String literals then that UNICODE/Octal value will be replaced with actual character.
  6. You can use escape sequence to represent UNICODE/Octal value as it is in String literals.

Integer Literals

  1. There are four types of Integer Literals a. Decimal Literals b. Octal Literals c. Hexadecimal Literals d. Binary Literals (From Java 7)
  2. Default type of Integer Literal is int.
  3. Integer value ranging from -2147483648 to 2147483647 can be represented directly.
  4. If you want to represent the integer value out of int range then you need to use L/l suffix.
  5. Decimal literals must not start with 0.
  6. Octal Literal must start with 0 (ZERO).
  7. Hexadecimal Literal must start with **0X / 0x.
  8. Binary Literal must start with 0B / 0b.
  9. When Java compiler encounters Octal, Binary or Hexadecimal Literals then that will be replaced with decimal equivalent value.

Floating Point Literals

  1. Default type of Floating Point Literal is double.
  2. You can use D/d as a suffix for the double value optionally.
  3. You must use F/f as a suffix for float value.
  4. When Integer value starts with 0 then it will be considered as Octal Literals.
  5. When Floating Point value starts with 0 then it will be considered as Floating Point Literals only, not as Octal Literal.
  6. When Integer value starts with 0x/0X then it will be considered as Hexadecimal Literals.
  7. When Floating Point value starts with 0X/0x then it must use P in fractional part.
  8. When Integer value starts with 0B/0b then it will be considered as Binary Literals.
  9. Floating Point cannot be represented as Binary Literal. UNDERSCORE in Numeric Literals
  10. You can use UNDERSCORE in numeric Literals to represent unit of values from Java 7.
  11. When UNDERSCORE is used before first digit then it will be considered as a variable.
  12. You can't use UNDERSCORE after the last digit.
  13. In the case of Octal literal UNDERSCORE can be used just after the 0.
  14. In the case of Binary literal UNDERSCORE can't be used just after the 0b/0B.
  15. In the case of Hexadecimal literal UNDERSCORE can't be used just after the 0x/0X.
  16. You can use more than one UNDERSCORE side by side between two digits.
  17. You can use UNDERSCORE with floating point literals but not allowed just before or after the decimal point.