1. Integer literals

And now for a new interesting topic — literals. Data written directly in program code are called literals. We're not talking about any old data, but the values of primitive types and the String type.

For example, suppose you have the following code:

Code Literals
int a = 5;
int b = a + 10;
String s = "Sum=" + (a + b);
5
10
"Sum="

The literals in this code are the number 5, the number 10 and the string 'Sum = '.

In Java, everything, including literals, has a type. By default, all integer literals (whole numbers) in code are ints. The int type is the standard Java integer type.

What's more, if you decide to assign an integer literal to a byte variable or a short variable in your code, there will be no problems. You just need to ensure that the literal value lies within the range of values that the variable's type can store.

The Java compiler is smart enough to understand that a byte variable can be assigned the integer literal 100 without creating problems.

Examples:

Code Description
int a = 300;
This will compile just fine.
byte a = 100;
This will compile just fine.
byte a = 300;
There will be a compilation error, because the maximum byte value is 127.

You can also write long literals in your code. To do this, add the Latin letter 'L' or 'l' at the end of the integer.

Examples:

Code Description
long a = 3000000000L;
This will compile just fine.
long a = 3000000000;
Compilation error: 3 billion is too large for an int literal.
int a = 3000000000L;
Compilation error: the literal is a long, but the variable is an int. Additionally, 3 billion is more than the maximum int.

Did you notice how difficult it is to read large numbers of 10 or more digits? You can't immediately tell whether the code says 3 billion or 30 billion. To make code more readable (and this is important!), Java allows underscores to be inserted into numeric literals (they do not affect the value of the number).

The example above can be rewritten with underscores to make it a little clearer:

Code Description
long a = 3_000_000_000L;
This will compile just fine.
long a = 3_000_000_000;
Compilation error: 3 billion is too large for an int literal.
int a = 3_000_000_000L;
Compilation error: the literal is a long, but the variable is an int. Additionally, 3 billion is more than the maximum int.

But we can't use commas in numeric literals because they are already used for another purpose. For example, for separating arguments from each other when calling a method.



2. Real number literals

In your code, you can specify not only integers, but also floating-point literals (real numbers).

Actually, the rule is quite simple: if a number in the code has a decimal point in it, then the number is a floating-point literal. And not just any literal, but a double literal.

You can create a float literal, but you need to put the letter 'F' (or 'f') at the end of the number to do that.

Examples:

Code Description
double a = 100.0;
This will compile just fine.
double a = 100.;
This will compile just fine.
double a = .0;
This will compile just fine.
float a = 100.0f;
This will compile just fine.
float a = 100.0;
There will be a compilation error: the variable is a float, but the literal is a double.

"By the way, you can explicitly convert an integer literal into a float or double literal by simply appending the suffix 'F' (for float) or D (for double). Examples:

Code Description
double a = 100D;
This will compile just fine.
float a = 100F;
This will compile just fine.
int a = 300D;
There will be a compilation error: the variable is an int, but the literal is a double.

Floating-point literals can use scientific notation: in addition to the signed part of the number, you can also specify a power of ten. Example:

Literal Mathematical notation Final value
1.23E2
1.23 * 102
123.0
1.23E3
1.23 * 103
1230.0
1.23E-6
1.23 * 10-6
0.00000123
1E6
1.0 * 106
1000000.0
1E-10
1.0 * 10-10
0.0000000001


3. String literals

You can also specify whole lines of text in your code. In order to tell the compiler to treat a string as data (a literal) and not as part of the code, the entire string is surrounded by double quotes on both sides.

If a single line of code has several double quotes, then they are split into pairs. The first double quotation mark indicates the beginning of a literal. The next one indicates the end of the literal. The next one after that once again marks the beginning of a new literal. And the next marks the end of the second literal. And so on.

Each such literal is a String.

Examples

Code Explanation
"+" + "+" + "+" + "+" + "+"
There are 5 literals in a line. Each of them consists of a single + character
""
This literal is an empty string. A string with no characters.
"2+3" + "-5"
There are two literals here. The result will be the string '2+3-5', not a number
"return" + ";"
There are also two literals here. There is no return statement here.

If a string literal is too long, it can be split into several lines and glued together with the 'plus operator':

Code Explanation
String s = "I hold it true, whate'er befall, "
         + "I feel it when I sorrow most; "
         + "'Tis better to have loved and lost "
         + "Than never to have loved at all.";
If you output this line to the screen, then all the text will be displayed on a single line!


4. Character literals

You can specify not only string literals in your code, but also literals comprised of individual characters. Note that we're not talking about a string consisting of a single character, but rather about literals whose type is char.

Unlike a string, a character literal is surrounded by single quotes. Inside the single quotes there must be a character and just one character. You cannot use empty single quotes.

Examples:

Code Explanation
'A'
A literal whose type is char. It contains the Latin letter 'A'.
'@'
A literal whose type is char. It contains the '@' symbol
'本'
A literal whose type is char. It contains a Japanese character. And this is also possible.
'\u1f3a'
A literal whose type is char. It contains a Unicode character specified by its number.

The last example assigns a Unicode character using a special notation: first we have the prefix \u, followed by 4 hexadecimal characters. The next lessons include detailed information about this.