You may have noticed that the new keyword isn’t used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it’s possible to assign a literal to a variable of a primitive type:

boolean result = true;     char capitalC = ‘C’;     byte b = 100;     short s = 10000;     int i = 100000;

The integral types (byte, short, int, and long) can be expressed using decimal, octal, hexadecimal, or binary number systems. (You can create binary literals in Java SE 7 and later.) 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. The binary system is base 2, whose digits are the numbers 0 and 1. For general-purpose programming, the decimal system is likely to be the only number system you’ll ever use. However, if you need octal, hexadecimal, or binary numbers, the following example shows the correct syntax. The prefix 0 indicates octal, 0x indicates hexadecimal, and 0b indicates binary:

int decVal = 26;    // The number 26, in decimal     int octVal = 032;            // The number 26, in octal     int hexVal = 0x1a;           // The number 26, in hexadecimal     int binVal = 0b11010; // The number 26, in binary

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.234e2; // same value as d1, but in scientific notation     float f1  = 123.4f;

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 “Su00ED seu00F1or” (Sí 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) that represents the type itself.

Yes No Suggest edit
Suggest Edit