Java Literals Overview Tutorial

What is a Java literal? A literal is simply a value in everyday human-readable form. The number 58 is an integer literal. The character 'Z' is a character literal. A boolean literal is either true or false. The number 123.99F is a floating-point literal. "Quick brown fox" is a string literal. Whenever we initialize a data type variable it is assigned a literal value.
Literals fit into a declaration or assignment statement like this: (data type) (variable name) (assignment operator =) (literal) (semicolon ;)

  • int a = 58;
  • char ch = 'Z';
  • String s = "Quick brown fox";
There are many different types of literals, too many to cover all at once in this tutorial. I will provide a quick overview of each of the different types of literals and then I will go into detail on each type in separate tutorials.

Integer Literals

Integer literals are whole numbers that can be assigned to variables of the following data types: Byte, char, short, int, and long. An integer literal can be represented using one of the following four numbering systems: hexadecimal (base 16), decimal (base 10), octal (base 8), and binary (base 2).

Integer Literals with Underscores

Beginning in Java 7, you can insert an underscore character(s) in a numeric literal for easier to read visual representation.

  • int a = 5600000; // no underscores
  • int b = 5_600_000; // with underscores

Decimal Literals

Decimal literals are whole numbers of the base 10 numbering system. Don't confuse decimal literals with floating point literals. Contrary to their name, decimal literals cannot contain a decimal point.

  • int a = 23;
  • int b = -156;

Binary Literals

Binary literals are numeric numbers represented by the binary (base 2) numbering system. Binary literals begin with a 0B or 0b prefix.

  • int a = 0B00000011;
  • int b = 0b00001111;

Hexadecimal Literals

Hexadecimal literals are numeric numbers represented by the hexadecimal (base 16) numbering system. Hexadecimal literals begin with a 0X or 0x prefix. The numbers 0 through 9 and the letters A through F (uppercase or lowercase) are used to represent hexadecimal numbers. 0123456789ABCDEF

  • int four = 0X04;
  • int ten = 0x0A;
  • int twenty = 0x14;

Octal Literals

Octal literals are numeric numbers represented by the octal (base 8) numbering system. Octal literals begin with a 0 (zero) prefix.

  • int seven = 07;
  • int eight = 010;
  • int nine = 011;

Floating-point Literals

Floating-point literals are numeric numbers with a decimal point symbol included somewhere. Floating-point literals can be assigned to variables of these two data types: double and float. The suffix of; D or d; or F or f, is required in certain literal representations.

  • double a = 1234.56789;
  • float b = 45.9876F;
  • double c = 1234.56789D; // the D is optional in this case

Boolean Literals

Boolean literals are perhaps the simplest of all the literals. The value can either be true or false. For obvious reasons, I will not be creating a separate tutorial for boolean literals.

  • boolean a = true;
  • boolean b = false;

Character Literals

Character literals can be represented in many different ways. The standard representation for a character literal is a single character inside a pair of single quotes.

  • char a = 'A';
  • char one = '1';
If you watched my tutorial on the Char data type, then you learned that a char data type is infact a 16-bit unsigned Unicode character. Because of that, you can assign a char variable a value using a decimal literal, hexadecimal literal, octal literal, or a binary literal. Keep in mind that the value must be between 0 and 65535.
  • char aDec = 65; // ASCII letter A
  • char aHex = 0x41; // ASCII letter A
  • char aOct = 0101; // ASCII letter A
  • char aBin = 0x01000001; // ASCII letter A

String Literals

String literals are simply a set of characters inside of a pair of double quotes.

  • String a = "The quick brown fox.";
  • String b = "Jumped over the lazy dog.";
But wait... what if we want to display a quote in the middle of a string literal? Good question, I'll glad I asked that. Java has what are called Escape Sequences. I will go into Escape Sequences in my String Literals tutorial, but for now I'll give you a little preview of how that will work.
  • String a = "He told me that it was a \"bad\" idea.";

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md Literals
C:\Java>cd Literals

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

class Literals {
    public static void main(String args[]) {
        int a = 5600000;
        int b = 5_600_000;
        int c = 23;
        int d = -156;
        int e = 0B00000011;
        int f = 0b00001111;
        int four = 0X04;
        int ten = 0x0A;
        int twenty = 0x14;
        int seven = 07;
        int eight = 010;
        int nine = 011;
        double g = 1234.56789;
        float h = 45.9876F;
        double i = 1234.56789D; // the D is optional in this case
        boolean j = true;
        boolean k = false;
        char one = '1';
        char upperA = 'A';
        char aDec = 65; // ASCII letter A
        char aHex = 0x41; // ASCII letter A
        char aOct = 0101; // ASCII letter A
        char aBin = 0b01000001; // ASCII letter A
        String m = "The quick brown fox.";
        String n = "Jumped over the lazy dog.";
        String p = "He told me that it was a \"bad\" idea.";


Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java Literals and press Enter.

C:\Java\Literals>java Literals

Final thoughts

It is really easy to understand certain types of literals as I am sure you work with some of them on a daily basis. It is worth taking the time to scratch the surface and get a better understanding each category of the Java literals. You will need that deeper understanding in the future, especially when converting and concatenating between different data types.