Constants are the values that never change during program execution, any attempt to change the lead to a compilation error. The special type of constants that are constant by nature is called literal for example numerical values such as 5, 19, 100, 12.345, etc. are literal. In addition to numerical constants, we have character, string, and Boolean constants. Character constants in JAVA are a single character in single quotes such as ‘a’, ‘C’ etc. String constants are one or more characters in double-quotes as “This is a string”. Boolean constants are true and false.
It is possible to assign a literal to a variable of a primitive type as shown below:
boolean result = true;
char capital C = ‘C’,
byte b = 100;
The integral types (byte, short, int, and long) can be expressed using decimal, octal, or hexadecimal number systems. Decimal is the number system you already use every day, it’s based on 10 digits, numbered 0 through 9. The octal number system is base 8, consisting of the digits 0 through 7. The hexadecimal system is base 16, whose digits are the numbers 0 through 9 and the letters A through F. For general-purpose programming, the decimal system is likely to be the only number system you’ll ever use. However, if you need octal or hexadecimal, the following example shows the correct syntax. The prefix 0 indicates octal, whereas Ox indicates hexadecimal.
int dec Val = 26; // The number 26, in decimal
int octVal = 032; // The number 26, in octal
int hexVal-Oxla ; // The number 26, in hexadecimal
The floating-point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal), and D or d (64-bit double literal; this is the default and by convention is omitted).
double d1 = 123.4;
double d2 = 1.234c2; // same value as dl, but in scientific notation
float fl = 123.4f // f is used for float data type
Literals of types char and String may contain any Unicode (UTF-16) characters. If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a “Unicode escape” such as ‘\u0108’ (capital C with circumflex), or “S\u00EDse\u00F1or” (Si Señor in Spanish). Always use ‘single quotes’ for char literals and “double quotes” for String literals. Unicode escape sequences may be used elsewhere in a program (such as in field names, for example), not just in char or String literals.
The Java programming language also supports a few special escape sequences for char and String literals: \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return). \” (double quote), \'(single quote), and \\ (backslash).
There’s also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There’s little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.
Finally, there’s also a special kind of literal called a class literal, formed by taking a type name and appending “.class”; for example, String.class. This refers to the object (of type Class) representing the type itself.