Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

While working with the Java programming language, we’ll use plenty of literals.

In this tutorial, we’ll look at all the types of literals and how to use them.

2. What Is a Java Literal?

A Java literal is any value we specify as a constant in the code. It can be of any type – integer, float, double, long, String, char, or boolean.

In the example below, the number 1 and the string literal_string are the literals.

int x = 1;
String s = "literal_string";

When working with literals, it’s equally important to have a good understanding of Java primitives.

3. Types of Literals

Let’s look at the different types of literals with some examples.

3.1. Integer Literals

For integral numbers (int, long, short, byte), we can specify them in different ways.

Firstly, we can use decimal literals (base 10). These are the most used as they are the numbers we use daily.

int x = 1;

Secondly, we can specify integer literals in octal form (base 8). In this form, they’ve to start with a 0.

int x = 05;

Thirdly, integer literals can be used in hexadecimal form (base 16). They’ve to start with a 0x or 0X.

int x = 0x12ef;

Lastly, we have the binary form (base 2). This form was introduced in Java 1.7, and these literals have to start with 0b or 0B.

int x = 0b1101;

In the examples above,  we can change int to any integer type as long as the literal value doesn’t exceed the type limit.

These literals are considered int by default. For long, short, or byte, the compiler checks if the value is up against the limits of the type, and if true, it considers them as that type literal.

We can override the default int literal by using l or L for long literals. We need to use this only when the literal value is above the int limit.

3.2. Floating-Point Literals

Floating-point literals are considered double by default. We can also specify that the literal’s a double by using d or D. It’s not mandatory, but it’s a good practice.

We can specify that we want a float by using f or F. This is mandatory for literals of type float.

Floating-point literals can be only specified in decimal form (base 10):

double d = 123.456;
float f = 123.456;
float f2 = 123.456d;
float f3 = 123.456f;

The second and third examples won’t compile due to the type mismatch.

3.3. Char Literals

For char data type, literals can come in a few ways.

Single-quote characters are common and are especially useful for special characters.

char c = 'a';
char c2 = '\n';

We can use only one single character or a special character between the single quotes.

The integer values used for a character are translated to the Unicode value of that character:

char c = 65;

We can specify it in the same way as the integer literals.

Lastly, we can use the Unicode representation:

char c= '\u0041';

The expressions in the last two examples evaluate character A.

3.4. String Literals

Any text between double quotes is a String literal:

String s = "string_literal";

String literals can only be on one line.  In order to have a multi-line String, we can use an expression that will be executed at compile time:

String multiLineText = "When we want some text that is on more than one line,\n"
+ "then we can use expressions to add text to a new line.\n";

4. Short and Byte Literals

Above, we saw how to declare a literal of every type. For all the types, except byte and short, we don’t need to create a variable. We can simply use the literal value.

For example, when passing arguments to a method, we can pass literals:

static void literals(int i, long l, double d, float f, char c, String s) {
    // do something
}
//Call literals method
literals(1, 123L, 1.0D, 1.0F, 'a', "a");

There are notations to specify the types of literals, so they aren’t used as the default types. In the example above, F is the only mandatory notation.

Surprisingly, issues arise for byte and short types:

static void shortAndByteLiterals(short s, byte b) {
    // do something
}
//Call the method
shortAndByteLiterals(1, 0); // won't compile

Despite this limitation, there are 2 solutions.

The first solution is to use some variables we previously declared:

short s = 1;
byte b = 1;
shortAndByteLiterals(s, b);

Another option’s to cast the literal value:

shortAndByteLiterals((short) 1, (byte) 0);

This is necessary in order to make the compiler use the appropriate type.

5. Conclusion

In this article, we looked at the different ways of specifying and using literals in Java.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.