- Literals are tokens that do not change - they are constant.
- The different types of literals in Java are:
-Integer Literals
-Floating-Point Literals
-Boolean Literals
-Character Literals
-String Literals
Integer Literals
- Integer literals come in different formats:
-decimal (base 10)
-hexadecimal (base 16)
-octal (base 8). - Special Notations in using integer literals in our programs:
-Decimal
No special notation
example: 12
-Hexadecimal
Precede by 0x or 0X
example: 0xC
-Octal
Precede by 0
example: 014
Floating Point Literals
- Represents decimals with fractional parts
-Example: 3.1416 - Can be expressed in standard or scientific notation
-Example: 583.45 (standard), 5.8345e2 (scientific)
Boolean
- Boolean literals have only two values, true or false.
Character Literals
- Character Literals represent single Unicode characters.
- Unicode character
-a 16-bit character set that replaces the 8-bit ASCII character set.
-Unicode allows the inclusion of symbols and special characters from other languages. - To use a character literal, enclose the character in single quote delimiter.
- For example
-the letter a, is represented as ‘a’.
-special characters such as a newline character, a backslash is used followed by the character code. For example, ‘\n’ for the newline character, ‘\r’ for the carriage return, ‘\b’ for backspace, '\t' for tab.
String Literals
- String literals represent multiple characters and are enclosed by double quotes.
- An example of a string literal is, “Hello World”.