Java Literals

By Raul Rios

Types of Literals

Basically, a literal is the source code representation of a fixed value. In order to be able to choose the right answer for some exam questions, you must be able to infer the data type of literals that appear in the sample code of those questions:

Literal type

Description

Data type of the literal

Integer

Decimal values with no decimal point

Decimal values ended in: l, L

Hexadecimal values (0x prefix)

Binary values (0b prefix)

long if it ends with L or l: 123L

int otherwise: 123, 0x1A, 0b110

Floating-point

Decimal values with a decimal point or using scientific notation

Decimal values ended in: f, F, d, D

float if it ends with F or f: 1.3f, 2e3f

double if it ends with D or d: 1.3d, 2e3d

double otherwise: 1.3, 2e3

Character

Values between single quotes (may contain Unicode characters)

char: 'A', '\u0041'

String

Values between double quotes (may contain Unicode characters)

String: "ABC", "\u0041BC"

Class

Any type name + .class

It refers to the object that represents the type itself

The object’s class, e.g.: String.class

Undescores in Numeric Literals

From Java version 7 on, it’s possible to use underscores in numeric literals to improve legibility. They can appear anywhere between the digits of the literal:

1

2

3

4

5

6

7

floatpi=3.14_15_927f;

bytenybbles=0b0010_0101;

longsocialSecurityNumber=999_99_9999L;

That’s all coders! If you have any comments or questions, you can drop me a line or leave a comment below and I will be pleased to help you.

In case you’re looking for a complete (but still short, precise and to-the-point) study guide to help you get your valuable Oracle® Java Programmer certification, including many tricky real-exam-like questions, study sheets, and other bonuses, then the Java Certification Roadmap book is your book.

Finally, if you found this content useful and know more programmers, colleagues or friends that may be interested in getting the Java Certification, click one of the buttons belowclick the button on the bottom left corner of your screen to let them know about the Java Certification Roadmap.