Java has very flexible literals for expressing different kinds of numbers. In this post we’ll go through all Java Literals one by one.
Java Literals
Binary numbers
Binary numbers in Java start with 0b:
System.out.println("Binary: " + 0b101010); // Binary: 42
Octal numbers
Octal numbers in Java start with 0:
System.out.println("Octal: " + 052); // Octal: 42
Decimal integer numbers
It’s integer by default:
System.out.println("Decimal integer: " + 42); // Decimal integer: 42
Decimal long
To tell Java that a number is Decimal Long we have to append L or l letter to it. Both are the same, but capital L is more readable:
System.out.println("Decimal long: " + 42L); // Decimal long: 42
Decimal with underscore – since Java 7
Since Java 7 we can use underscore for to increase readability of numbers. What’s good, it works with other literals too:
System.out.println("Decimal with underscores: " + 42_000); // Decimal with underscores: 42000 System.out.println("Binary with underscores: " + 0b10_1010); // Binary with underscores: 42
Hexadecimal numbers
The format is the same as in other C-like languages (0x before a number) and the literal is case-insensitive:
System.out.println("Hex: " + 0xff); // Hex: 255 System.out.println("Hex: " + 0XfE); // it's case insensitive // Hex: 254
Literal for Float numbers
The format is simple: a number with f appended. Similarly to hexadecimal numbers, float literals are case-insensitive. Floats can be expressed also in scientific notation.
System.out.println("Float: " + 42.0f); // Float: 42.0 System.out.println("Scientific float: " + 42e3f); // Scientific float: 42000.0
Literal for Double numbers
Doubles are almost the same as floats above, but end with d/D instead of f/F:
System.out.println("Double: " + 3.14); // Double: 3.14 System.out.println("Double: " + 2.71d); // Double: 2.71 // Scientific notation: System.out.println("Scientific double: " + 42e3); // Scientific double: 42000.0
Complete Java program
The following program shows how to represent numbers in Java. Play around with it and experiment!
package com.farenda.java.lang; import static java.lang.System.out; public class Literals { public static void main(String[] args) { // Binary numbers in Java start with 0b: out.println("Binary: " + 0b101010); // Octal numbers in Java start with 0: out.println("Octal: " + 052); // It's integer by default: out.println("Decimal integer: " + 42); // Can also use small "l", but is less readable: out.println("Decimal long: " + 42L); // Since Java 1.7 can use underscore for readability: out.println("Decimal with underscores: " + 42_000); // Can use them for other literals too: out.println("Binary with underscores: " + 0b10_1010); // Hexadecimal numbers in Java: out.println("Hex: " + 0xff); out.println("Hex: " + 0XfE); // it's case insensitive // Case-insensitive, so can use "F": out.println("Float: " + 42.0f); // In scientific notation for: out.println("Scientific float: " + 42e3f); // Double is by default when using dot: out.println("Double: " + 3.14); out.println("Double: " + 2.71d); // also case insensitive // Scientific notation: out.println("Scientific double: " + 42e3); } }
The program outputs the following results:
Binary: 42 Octal: 42 Decimal integer: 42 Decimal long: 42 Decimal with underscores: 42000 Binary with underscores: 42 Hex: 255 Hex: 254 Float: 42.0 Scientific float: 42000.0 Double: 3.14 Double: 2.71 Scientific double: 42000.0
What’s next
Check out other Java Tutorials to learn more!