Java – Literals

Java – Literals 2017-08-08T23:59:43+00:00

Java Literals :

A constant value in Java is created by using a literal representation of it.

For example, Here are some java literals :

integer literal value : 100

floating-point literal value : 98.6

character  literal value : ‘X’

string literal value : “This is a test”

A literal can be used anywhere a value of its type is allowed.

Integer Literals :

Integer literals are the primary literals used in Java programming. They come in a few different formats: decimal, hexadecimal, and octal. These formats correspond to the base of the number system used by the literal.

Decimal (base 10) literals appear as ordinary numbers with no special notation. Hexadecimal numbers (base 16) appear with a leading 0x or 0X. Octal (base 8) numbers appear with a leading 0 in front of the digits.

For example, an integer literal for the decimal number 12 is represented in Java as 12 in decimal, 0xC in hexadecimal, and 014 in octal.

Integer literals default to being stored in the int type, which is a signed 32-bit value. If you are working with very large numbers, you can force an integer literal to be stored in the long type by appending an l or L to the end of the number, as in 79L. The long type is a signed 64-bit value.

Floating-Point Literals :

Floating-point literals represent decimal numbers with fractional parts, such as 3.1415. They can be expressed in either standard or scientific notation, meaning that the number 143.85 also can be expressed as 1.4385e2.

Unlike integer literals, floating-point literals default to the double type, which is a 64-bit value. You have the option of using the smaller 32-bit float type if you know the full 64 bits are not required. You do this by appending an f or F to the end of the number, as in 5.6384e2f.

If you are a stickler for details, you also can explicitly state that you want a double type as the storage unit for your literal, as in 3.142d. But because the default storage for floating-point numbers is double already, this addition isn’t necessary.

Boolean Literals :

Boolean literals are certainly welcome if you are coming from the world of C/C++. In C, there is no boolean type, and therefore no boolean literals. The boolean values true and false are represented by the integer values 1 and 0.

Java fixes this problem by providing a boolean type with two possible states: true and false. Not surprisingly, these states are represented in the Java language by the keywords true and false.

Boolean literals are used in Java programming about as often as integer literals because they are present in almost every type of control structure. Any time you need to represent a condition or state with two possible values, a boolean is what you need. The two boolean literal values: true and false.

Character Literals :

Character literals represent a single Unicode character and appear within a pair of single quotation marks. Special characters (control characters and characters that cannot be printed) are represented by a backslash (\) followed by the character code.

A good example of a special character is \n, which forces the output to a new line when printed. Table shows the special characters supported by Java.










Carriage return


Form feed


Horizontal tab




Single quote


Double quote


Unicode character


Octal character


An example of a Unicode character literal is \u0048, which is a hexadecimal representation of the character H. This same character is represented in octal as \110.

String Literals :

String literals represent multiple characters and appear within a pair of double quotation marks. Unlike all the other literals discussed in this chapter, string literals are implemented in Java by the String class. This arrangement is very different from the C/C++ representation of strings as an array of characters.

When Java encounters a string literal, it creates an instance of the String class and sets its state to the characters appearing within the double quotes.

Prev Next