List of Constants in Java

Like other programming language, Java also has some constants. In the previous section, we have discussed, Java constants, how to declare constants. So, in this section, we will discuss the only types of constants in Java and how to use it.

List of Constants in Java

Constants

It is the value that cannot be changed once assigned. In Java, the constant values are defined by using the final keyword. The final keyword represents that the value of the variable cannot be changed. Note that the identifier name must be in capital letters. We can also define constants as static.

Syntax:

For example:

Points to Remember

  • Write the identifier name in capital letters that we want to declare as constant. For example, PRICE=21000.
  • If we use the privateaccess-specifier before the constant name, the value of the constant cannot be changed in that particular class.
  • If we use the publicaccess-specifier before the constant name, the value of the constant can be changed in the program.

Types of Constants

There are the following types if constants in Java:

  1. Numeric Constants
    • Integer Constants
    • Real Constants
  2. Non-numeric Constants
    • Character Constants
    • String Constants

Let's discuss each in detail.

Numeric Constants

Numeric constants are the constants that contains numerals. It may also have a leading sign and decimal point.

Rule to Define Numeric Constants

  • Must have at least one digit.
  • It should not have comma, space, and another special symbol.
  • It may have positive or negative sign. If no sign is preceded then the constant assumed positive. It is optional to preceded a constant with a positive sign.

There are the following two types of numeric contestants:

Integer Constants

A constant that contains digits (0-9) and does not have decimal point is called integer constants. By default, it is type of int. There are the following three types of integer constants:

  • Decimal Constants: It contains digits between 0 to 9. Note that must not start with 0. For example, 898, 67, 66.
  • Octal Constants: It contains digits between 0 to 7 and must begin with 0. For example, 012, 032, 067.
  • Hexadecimal Constants: It contains digits between 0 to 9 and letters a to f (either in upper or lower case). It must begin with 0X or 0x. For example, 0x23, 0x76, 0X6A, 0XFF.

Real Constants

Numeric constants that have a decimal point are called real or floating-point constants. By default, the real constants are of double type. We can explicitly mention the type of a floating-point constant as a float by appending the letter f or F at the end of the constant. For example, 45f, -0.14f, 5.6F.

The real constants can be written in the following two forms:

  • Fractional Form
  • Exponential Form

Fractional Form

Rules to Define Fractional Form

  1. It must have at least one digit.
  2. It must have a decimal point
  3. It may have positive or negative sign. The default is positive sign and it is optional.
  4. Comma, spaces, or any other symbols are not allowed.

For example, 3.14, -9.1, 0.67.

Exponential Form

It is used to represent a real constant when a number is too small or too large.

For example, 0.00000149 can be represented as 1.49e-6. The part of the number before e is called mantissa i.e 1.49, whereas, the part after e is called the exponent i.e, 6.

Rules to Define Exponent Form

  • Mantissa and exponent must be separated by e or E.
  • Mantissa can be positive or negative, the default is positive.
  • Exponent must have at least one digit.
  • The exponent can be positive or negative, the default is positive

For example, 100.34e4, -56E10, 0.233E10, -0.94e15.

Non-numeric Constants

A constant that does not contain digits is called non-numeric constants. There are the following two types of non-numeric constants:

Character Constants

A Character constant is a single alphabet, digit or any special symbol enclosed using single quotes. For example, 'Y', 'd', '6', '#', '&'.

The maximum length of a character constant is 1 character long. It means that we cannot put more than one character inside single quotation marks.

As we may already know that inside computer memory everything is stored in binary form. But how character constants are stored in the memory? It turns out that Every character constant has a unique integer associated with it. An ASCII table represents the decimal number used to represent every known character in the English Language.

String Constants

String constants consist of zero or more characters enclosed in double quotes (""). At the end of the string, the null character i.e '\0' is automatically placed by the compiler. For example, "hello", " " (denotes blank space), "111".

Note: Although not officially part of Primary constants, string constants are given here for the sake of completeness. The data type for string is String, they are stored as an array of characters.

Backslash Character Constants

Java also supports backslash character constants. These are used in output methods. It is also known as escape sequence. For Example, \n, \t, \a, etc.

  • Although it consists of two characters but it represents a single character.
  • Each escape sequence has Unicode value.
  • Each and Every combination must start with backslash character (\).
  • These are non-printable characters.
  • It can also be expressed in terms of octal digits or hexadecimal sequence.
  • Escape sequence in character constants and string literals are replaced by their equivalent and then adjacent string literals are concatenated.
  • Escape Sequences are preprocessed by Preprocessor.

The following table denotes the backslash character constants used in Java.

\bBackspace
\fFrom feed
\nNew line
\rCarriage return
\tHorizontal tab
\"Double quote
\'Single quote
\\Backslash
\vVertical tab
\aAlert
\?Question mark
\NOctal constant
\xNHexadecimal constant

Let's use these constants in a Java program.

ConstnatExample.java

Output:

value of var1 : 23
value of var2 : -5
value of var3 : 9
value of var4 : -12
value of var5 : 120
value of var6 : -212
value of var7 : 90000
value of var8 : -12345
value of var9 : 14.78
value of var10 : -117.34
value of var11 : 70000.1234
value of var12 : -12345.111
value of var13 : false
value of var14 : true
value of var15 : a
value of var16 : p
javatpoint
80
Hexadecimal: 801, 2812
21300.0