white tower ceiling glass window observatory with blue yellow nebula clouds in space

1.10 Java | Primitive Data Types and Literals

Table of Contents

Primitive Data Types

In the Java programming language, you must declare all variables before they can be used. Declaring the variables mean stating the variable’s type & name, like so:

double rock = 1;

Declaring, or stating the variable’s type & name tells your java program that a field named “rock” exists, that it holds numerical data, and that this variable has the initial value of “1”. A variable’s data type determines the type of values it can contain, and the actions that may be performed on this variable. In addition to double, Java also supports 7 other primitive data types. A primitive data type is defined by the language and is named by a reserved keyword (i.e. int). Note that a reserved keyword are words reserved by Java that cannot be used as identifiers (e.g., variable names, method names, class names). If a reserved keyword was used as a variable you would get an error or unexpected result.

The eight primitive data types supported by the Java programming language are byte, short, int, long, float, double, boolean, and char.


The byte data type is an 8-bit signed two’s complement integer (two’s complement means that the range extends for both positive & negative numbers) that has a minimum value of -128 and a maximum value of 127 inclusively. The byte data type can be useful for saving memory in large arrays, where the memory savings actually matter. They can also be used in place of int where their limits help to clarify your code; the fact that a variable’s range is limited can serve as a form of documentation.

The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 inclusively. As with byte, you can use a short to save memory in large arrays, in situations where memory savings actually matters.

The int data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the integer class to use int data type as an unsigned integer.

The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int.

The float data type is a single-precision 32-bit IEEE 754 floating point. (Note that IEEE 754 is in reference to the technical standard established by the Institute of Electrical and Electronics Engineers (IEEE). A standard as in a standard unit of measurement, i.e. the metric system) float should be used instead of a double if you need to save memory, say for example, in a large array of floating point numbers. This data type should never be used for precise values, like currency.

The double data type is twice as precise as long, specifically double represents a 64-bit IEEE 754 floating point. You want to use the double data type when precision matters over how much memory you can save.

The boolean data type has only two values: true & false. This data type is useful for simple flags that track true/false conditions. This data type represents one bit of information.

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusively).


In addition to the eight primitive data types listed above, Java also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically make a new String object, for example, String s = "Hello World!";. String objects are immutable, which means that once made, their values cannot be changed. Note that the String class is not a primitive data type, but is heavily used like one.

Default Values

When a field is declared, but not initialized, it will be set to a default value by the compiler. Generally the default will be zero or null, depending on the data type.

Note that relying on default values is generally considered a bad practice in programming.

The following chart shows the default values for the above data types.

Data Type | Default Value
----------+--------------
byte      | 0
----------+--------------
short     | 0
----------+--------------
int       | 0
----------+--------------
long      | 0L
----------+--------------
float     | 0.0f
----------+--------------
double    | 0.0d
----------+--------------
char      | '\u0000'
----------+--------------
String    | null
----------+--------------
Any Object| null
----------+--------------
boolean   | false
----------+--------------

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

Remember that a field is a global variable that is used by the whole class, whereas a local variable is only used by the method that it is in.

Literals

A literal is the source code representation of a a fixed value that can be of a primitive type without requiring a computation. Java literals are suffixes and prefixes that represent Boolean, character, numeric, string data, and other primitive types.

For example, 34 and 0.305 are literals in the following statements:

int numberOfYears = 34; // an integer literal
double weight = 0.305; // a double literal

Integer Literals

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. You should use the upper case letter L because the lower case letter l looks similar to the digit 1.

Values of the integral types byte, short, int, and long can be made from int literals. Values of type long that exceed the range of int can be made from long literals.

An integer literal of the int data type has a 32-bit value range between -231 (-2147483648) and 231 – 1 (2147483647). The int data type does not need a suffix or prefix.

The integer literal of the long data type has a 64-bit value range between -263 and 263-1 . The long data type uses the letter L to l suffix. For example, to write integer 2147483648 in a Java program, you have to write it as 2147483648L or 2147483648l, because 2147483648 exceeds the range for the int max value of 2147483647. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one).

An integer literal can be assigned to a variable as long as the integer literal fits the variable. A compiling error occurs if the literal is too large for the variable to hold. For example, the statement byte b = 128 causes a compiling error because 128 cannot be stored in a byte type variable. The range for a byte value is from -128 to 127, not 128.

Also, Integer literals can be expressed by these number systems:

  • Decimal Base 10, with digits consisting of numbers 0 through 9
  • Hexadecimal Base 16, with digits consisting of numbers 0 through 9 and letters A through F
  • Binary Base 2, with digits consists of numbers 0 and 1

An integer literal is a decimal number by default. To make a number a binary integer literal, use a prefix of 0b or 0B (zero B). To make a number an octal integer literal, use a prefix of 0 (zero). To make a number a hexadecimal integer literal, use a prefix of 0x or 0X (zero X). For example:

System.out.println(0B1111); // Displays 15
System.out.println(07777); // Displays 4095 
System.out.println(0XFFFF); // Displays 65535

More examples:

// The number 31 in decimal format
int decVal = 31;
//  The number 31 in hexadecimal format
int hexVal = 0x1f;
// The number 31 in binary format
int binVal = 0b11111;

Floating Point Literals

Floating-point literals are written with a decimal point. By default, numbers that use a floating point or decimal point are treated as a double data type value. For example, 5.0 is considered a double value, not a float value.

You can make a number a float by adding the letter f or F suffix, and you can make a number a double by adding the d or D suffix. For example, you can use 100.2f or 100.2F to show a float number, and 100.2d or 100.2D to show a double number. The floating point types float and double can also be expressed using E or e.

Examples:

double dataz = 123.4;
double dingus = 1.234e2; // same value as dataz, but in scientific notation
float f1  = 123.4f;

Note that the double type values are more accurate than the float type values. For example,

System.out.println("1.0 / 3.0 is" + 1.0 / 3.0);

prints out 1.0 / 3.0 is 0.3333333333333333

System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F);

prints out 1.0F / 3.0F is 0.33333334

That’s because a float value has 8 numbers of significant digits and a double value has 16 numbers of significant digits. This corresponds with the fact that a float value takes up 32-bits in computer memory, and that a double value takes up 64-bits in computer memory.

Denoting Scientific Notation

Floating-point literals can be written in scientific notation in the form of a * 10b. For example, the scientific notation for 122.455 is 1.22455 * 102 and and for 0.0122455 is 1.22455 * 10-2. A special syntax is used to write scientific notation numbers in java. For example, 1.22455 * 102 is typed as 1.22455E2 or 1.22455E+2 in Java. Similarly, 1.22455 * 10-2 is typed as 1.22455E-2. E or e (either lower or upper case is fine) says that what comes after the e or E character is an exponent of what is before the e or E character.

Character and String Literals

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\u00ED Se\u00F1or" (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.

Using Underscore Characters in Numeric Literals

To improve the readability of numbers literals in java, you can use underscores _ between 2 digits without changing the value. For example, these are some numeric literals that use the underscore correctly:

long socialSecurityNumber = 232_45_4519;
long creditCardNum = 2324_4545_4519_3415L;

However, _123 or 123_ is incorrect. The underscore need to be between 2 numbers, not just touching 1 number.

You can place underscores only between digits; you cannot place underscores in the following places:

  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating point literal
  • Right before the F or L suffix

2 thoughts to “1.10 Java | Primitive Data Types and Literals”

What's Your Opinion?