List of Constants in JavaLike 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. ConstantsIt 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
Types of ConstantsThere are the following types if constants in Java:
Let's discuss each in detail. Numeric ConstantsNumeric constants are the constants that contains numerals. It may also have a leading sign and decimal point. Rule to Define Numeric Constants
There are the following two types of numeric contestants: Integer ConstantsA constant that contains digits (09) 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:
Real ConstantsNumeric constants that have a decimal point are called real or floatingpoint constants. By default, the real constants are of double type. We can explicitly mention the type of a floatingpoint 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 Rules to Define Fractional Form
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.49e6. 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
For example, 100.34e4, 56E10, 0.233E10, 0.94e15. Nonnumeric ConstantsA constant that does not contain digits is called nonnumeric constants. There are the following two types of nonnumeric 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.
The following table denotes the backslash character constants used in Java.
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
Next TopicMOOD Factors to Assess a Java Program
