Numeric Literals

paulike

Paul Ngugi

Posted on April 24, 2024

Numeric Literals

A literal is a constant value that appears directly in a program.

Integer Literals

An integer literal can be assigned to an integer variable as long as it can fit into the variable. A compile error will occur if the literal is too large for the variable to hold. The statement byte b = 128, for example, will cause a compile error, because 128 cannot be stored in a variable of the byte type. (Note that the range for a byte value is from –128 to 127.)

An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) and 231 - 1 (2147483647). To denote an integer literal of the long type, append the letter L or l to it. For example, to write integer 2147483648 in a Java program, you have to write it as 2147483648L or 2147483648l, because 2147483648 exceeds the range for the int value. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one).

By default, an integer literal is a decimal integer number. To denote a binary integer literal, use a leading 0b or 0B (zero B), to denote an octal integer literal, use a leading 0 (zero), and to denote a hexadecimal integer literal, use a leading 0x or 0X (zero X). For example,

System.out.println(0B1111); // Displays 15
System.out.println(07777); // Displays 4095
System.out.println(0XFFFF); // Displays 65535
Enter fullscreen mode Exit fullscreen mode

Floating-Point Literals

Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. You can make a number a float by appending the letter f or F, and you can make a number a double by appending the letter d or D. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number. The double type values are more accurate than the float type values.

Scientific Notation

Floating-point literals can be written in scientific notation in the form of a * 10^b . For example, the scientific notation for 123.456 is 1.23456 * 10^2 and for 0.0123456 is 1.23456 * 10^-2 . A special syntax is used to write scientific notation numbers. For example, 1.23456 * 10^2 is written as 1.23456E2 or 1.23456E+2 and 1.23456 * 10^-2 as 1.23456E-2. E (or e) represents an exponent and can be in either lowercase or uppercase.

The float and double types are used to represent numbers with a decimal point. Why are they called floating-point numbers? These numbers are stored in scientific notation internally. When a number such as 50.534 is converted into scientific notation, such as 5.0534E+1, its decimal point is moved (i.e., floated) to a new position.

To improve readability, Java allows you to use underscores between two digits in a number literal. For example, the following literals are correct.

long ssn = 232_45_4519;
long creditCardNumber = 2324_4545_4519_3415L;
Enter fullscreen mode Exit fullscreen mode

However, 45_ or _45 is incorrect. The underscore must be placed between two digits.

💖 💪 🙅 🚩
paulike
Paul Ngugi

Posted on April 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Augmented Assignment Operators
java Augmented Assignment Operators

April 24, 2024

Numeric Literals
java Numeric Literals

April 24, 2024

Named Constants
java Named Constants

April 24, 2024

Programming Errors
java Programming Errors

April 22, 2024