Home > Articles

  • Print
  • + Share This
This chapter is from the book

Primitive Types and Literal Values

Java defines eight primitive types. Variables that are declared as primitive types are not references to objects. They are only placeholders to store primitive values. The eight primitive types are byte, short, int, long, float, double, char, and boolean. The byte, short, int, and long types represent 8-, 16-, 32-, and 64-bit signed integer values. The char type represents an unsigned 16-bit value. The float and double types represent 32- and 64-bit floating-point values. The ranges of the primitive types are shown in Table 2.1.

Table 2.1: Ranges of Numeric Types




true and false


-(27) to 27 - 1


0 to 216 - 1


-(215) to 215 - 1


-(231) to 231 - 1


-(263) to 263 - 1


Float.MIN_VALUE to Float.MAX_VALUE, Float.NaN,



Double.MIN_VALUE to

Double.MAX_VALUE, Double.NaN,



Know Your Ranges

It is a good idea to memorize Table 2.1 because a few range-related questions are on the exam. See if you notice any patterns in this table that will make it easier for you to memorize the content.

The literal values of these integer types are written using positive or negative decimal, hexadecimal, or octal integers. Hexadecimal values are preceded by 0x or 0X and use the letters a through f (uppercase or lowercase) to represent the digits 10 through 15. Octal numbers are preceded by 0. Long decimal values have an l or L appended to the end of the number. Examples of conversions between decimal, hexadecimal, and octal values are shown in Table 2.2.

Table 2.2: Decimal, Hexadecimal, and Octal Values

Decimal Value

Hexadecimal Value

Octal Value










The float and double types represent 32- and 64-bit IEEE 754 floating-point numbers. Float numbers have the f or F suffix. Double numbers have d or D. If no suffix is provided, the default double type is assumed. Floating-point numbers can be written using any of the following forms:

digits . optionalDigits optionalExponentPart suffix
. digits optionalExponentPart suffix
digits optionalExponentPart suffix

The suffix is optional. It consists of f, F, d, or D, as described previously.

The exponent part is optional. It consists of an e or E followed by a signed integer. It is used to identify the exponent of 10 of the number written in scientific notation. For example, 1000000.0 could be represented as 1.0E6. If a floating-point literal does not contain a decimal point, then it needs to have either the exponent part or the suffix to be recognized as a floating-point literal (as opposed to an integer literal).

The Float and Double classes define three special float and double constants. The special value NaN is used to represent the value for "not a number" that occurs as the result of undefined mathematical operations. The values POSITIVE_INFINITY and NEGATIVE_INFINITY represent infinite values in the positive and negative directions.

The char type represents 16-bit Unicode characters. Unicode is a 16-bit superset of the ASCII character set that provides many foreign-language characters. A single character is specified by putting the character within single quotes ('). Three exceptions exist: single quote ('), double quote ("), and backslash (\). The backslash character (\) is used as an escape code to represent special character values. For example, a single quote would be represented by '\''. The character escape codes are shown in Table 2.3.

Table 2.3: Character Escape Codes

Escape Code









form feed


carriage return


double quote


single quote



Field Variables and Local Variables

Field variables are variables that are declared as members of classes. Local variables, also referred to as automatic variables, are declared relative to (or local to) a method or constructor.

The backslash can also be followed by an 8-bit octal value (\000 through \377) or by a u or U followed by a four-digit, hexadecimal value (\u0000 through \uffff). The four-digit value can be used to specify the full range of Unicode characters.

The boolean type represents the logical values true and false.

Java also supports string literals. String literals are not primitive values. They are a shorthand notation for representing String objects. Strings consist of characters enclosed by double quotes ("). The character escape codes can be used within String literals to represent special characters within the string.

The literal value null is used to identify the fact that an object is not assigned to a variable. It can be used with any variable that is not of a primitive data type.

Class literals were introduced with Java 1.1. A class literal is formed by appending .class to the name of a primitive or reference type. It evaluates to the class descriptor of the reference type or class descriptor of the primitive type's wrapper class. The expression void.class evaluates to the class descriptor of the Void class. For example, suppose that Test is a class you have declared. The following statement displays the output class Test:


Declaring Arrays and Objects

The declaration and use of arrays and objects are covered in Chapter 4.

Automatic Initialization

Field variables and the elements of arrays are automatically initialized to default values. Local variables are not automatically initialized. Failure to initialize a local variable results in a compilation error. Table 2.4 identifies the default values of each primitive type.

Table 2.4: Default Values for Primitive Types


Default Value

















Field variables of object types and the elements of arrays of object types are automatically initialized to the null value.

The Initialization program (see Listing 2.2) illustrates the use of the automatic initialization of field variables and arrays. It displays the following output:

boolean: false
byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
Object: null
int[2]: 0 0
Object[2]: null null

Listing 2.2: The Initialization Program

class Initialization {
 boolean bo;
 byte by;
 char c;
 short s;
 int i;
 long l;
 float f;
 double d;
 Object o;
 public static void main(String[] args) {
 Initialization app = new Initialization();
 void run() {
 int[] intArray = new int[2];
 Object[] objectArray = new Object[2];
 System.out.println("boolean: "+bo);
 System.out.println("byte: "+by);
 System.out.println("char: "+c);
 System.out.println("short: "+s);
 System.out.println("int: "+i);
 System.out.println("long: "+l);
 System.out.println("float: "+f);
 System.out.println("double: "+d);
 System.out.println("Object: "+o);
 System.out.println("int[2]: "+intArray[0]+" "+intArray[1]);
 System.out.println("Object[2]: "+objectArray[0]+"  "+objectArray[1]);
  • + Share This
  • 🔖 Save To Your Account