Java Constants

In Java, a constant value is created by using a literal representation of it. For example, below are some literals :

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

Types of Literals

In Java, there are the following types of literals:

Now let's take a look at all the above literals types in Java.

Java Integer Literals

In Java, Integers are the most commonly used type in normal programs. Any whole number value represents an integer literal.

Java allows these three types of integer literals :

Let's take a look at these three Integer Literals one by one.

Java Decimal Integer Literals

Decimal Integer Literals are decimal values i.e, they describes a base 10 number. For instance, 1, 4, 7, and 62 etc.

To convert a number from any number system to decimal number system, check the following :

Example

Following is an example program for decimal integer literals :

/* Java Program Example - Java Decimal Integer Constant */

public class JavaProgram
{   
    public static void main(String args[])
    {
        
        int nump = +100, numm = -100, numn = 100;
        
        System.out.println("The first decimal integer constant value is " + nump);
        System.out.println("The second decimal integer constant value is " + numm);
        System.out.println("The third decimal integer constant value is " + numn);
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java constants literals

Java Octal Integer Literals

In Java, octal values are denoted by a leading zero. Normal decimal numbers cannot have a leading zero. Therefore, the seemingly valid value 09 will give an error from the compiler, since 9 is outside of octal's 0 to 7 range. For instance, 8 (decimal integer) will be written as 010 as octal integer (810 = 108) and decimal integer 12 will be written as 014 as octal integer (1210 = 148).

Java Hexadecimal Integer Literals

A more common base for numbers used by programmers is hexadecimal, which matches cleanly with modulo 8 word sizes, like 8, 16, 32, and 64 bits. You signify a hexadecimal constant with a leading zero-x, (0x or 0X). The range of hexadecimal digit is from 0 to 15, therefore A through F (or a through f) are substituted for 10 through 15.

Therefore the number 12 will be written either as 12 (as decimal) or 14 (as octal) or 0XC (as hexadecimal).

To convert a number from any number system to Hexadecimal Number System, check the following :

Java Floating-point Literals

Floating-point numbers represent decimal values with fractional component. They can be expressed in either standard or scientific notation. Standard notation comprises of a whole number component followed by a decimal point followed by a fractional component. For instance, 6.0, 3.14159, and 0.6667 represent valid standard notation floating-point numbers.

Scientific Notation

Scientific notation uses the standard-notation, floating-point number plus a suffix that specifies a power of 10 by which the number is to be multiplied. And the exponent is indicated by an E (uppercase) or e (lowercase) followed by a decimal number which can be positive or negative. For example, 6.022E25, 314159E-05, and 2e+100.

In Java, floating-point literals default to double precision. To determine a float literal, you must append an F (uppercase) or f (lowercase) to the constant. You can also specify a double literal explicitly by appending a D (uppercase) or d (lowercase). Doing so is redundant. The default double type consumes 64 bits of storage, while the smaller float type requires only 32 bits.

Hexadecimal Floating-point Literals

In Java, hexadecimal floating-point literals are also supported, but they are rarely used. They must be in a form similar to the scientific notation, but a P or p, instead of an E or e, is used.

Java Boolean Literals

Boolean Literals are simple. There are only two logical values that a boolean value can have, i.e., true and false. The values of true and false do not convert into any numerical representation. In Java, the true literal doesn't equal to 1, nor does the false literal equal to 0. The Boolean literals in Java, can only be assigned to the variables declared as boolean or used in expressions with Boolean operators.

Java Character Literals

Character in Java are indices into the Unicode character set. They are 16-bit values that can be converted into integers and manipulated with integer operators, such as addition and subtraction operators. A literal character is represented within a pair of single quotes.

All the visible ASCII characters can be directly entered inside the quotes, like 'a', 'z', and '@'. For characters that are impossible to enter directly, there are several escape sequences which allow you to enter the character you need, such as '\'' for the single-quote character itself and '\n' for the newline character.

Here this table lists the escape sequences :

Escape Sequence Name
\ddd Octal character (ddd)
\uxxxx Hexadecimal Unicode character (xxxx)
\' Single quote
\" Double quote
\\ Backslash
\r Carriage return
\t Tab
\n New line (line feed)
\f Form feed
\b Backspace

Java String Literals

String literals in Java are specified like they are in most other programming languages - by enclosing a sequence of characters between a pair of double quotes. Below are some examples of the string literals:
"Hello, World"
"two\nlines"
"\"This is in quotes\""

You will learn about strings in Java Strings chapter later.

Java Online Test


« Previous Tutorial Next Tutorial »