Primitive Data Types


The 8 primitive data types byte, short, int, long, char, boolean, float, and double are the types that store most raw numerical data in Java programs.

The char primitive

A char can store a single 16-bit Unicode character. A character literal is enclosed in single quotes

char ch1 = 's';
char ch2 = '7';
char ch2 = 65; // myChar3 == 'A'

It has a minimum value of \u0000 (0 in the decimal representation, also called the null character) and a maximum value of \uffff (65,535).

The default value of a char is \u0000.

char default_char;  // defaultChar == \u0000

In order to define a char of ' value an escape sequence (character preceded by a backslash) has to be used:

char single_quote = '\'';

There are also other escape sequences:

char tab = '\t';
char backspace = '\b';
char newline = '\n';
char carriageReturn = '\r';
char formfeed = '\f';
char singleQuote = '\'';
char doubleQuote = '\"';    // escaping redundant here; '"' would be the same; however still allowed
char backslash = '\\';
char unicodeChar = '\uXXXX' // XXXX represents the Unicode-value of the character you want to display

You can declare a char of any Unicode character.

char star = '\u2730';
System.out.println(Character.toString(star)); // Prints a line containing "✰"

It is also possible to add to a char. e.g. to iterate through every lower-case letter, you could do to the following:

for (int i = 0; i <= 26; i++)
{
	char letter = (char) ('a' + i);
	System.out.println(letter);
}

Primitive Types Cheatsheet

Table showing size and values range of all primitive types:

Data Type Numeric Representation Range of Values Default Value
boolean n/a false and true false
byte 8-bit signed -27 to 27 - 1
-128 to +127
0
short 16-bit signed -215 to 215 - 1
-32,768 to +32,767
0
int 32-bit signed -231 to 231 - 1
-2,147,483,648 to +2,147,483,647
0
long 64-bit signed -263 to 263 - 1
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
0L
float 32-bit floating point 1.401298464e-45 to 3.402823466e+38 (positive or negative) 0.0F
double 64-bit floating point 4.94065645841246544e-324 to 1.79769313486231570e+308 (positive or negative) 0.0D
char 16-bit unsigned 0 to 216 - 1
0 to 65,535
0

Notes:

  • The Java Language Specification mandates that signed integral types (byte through long) use binary twos-complement representation, and the floating point types use standard IEE 754 binary floating point representations.
  • Java 8 and later provide methods to perform unsigned arithmetic operations on int and long. While these methods allow a program to treat values of the respective types as unsigned, the types remain signed types.
  • The smallest floating point shown above are subnormal; i.e. they have less precision than a normal value. The smallest normal numbers are 1.175494351e−38 and 2250738585072014e−308
  • A char conventionally represents a Unicode / UTF-16 code unit.
  • Although a boolean contains just one bit of information, its size in memory varies depending on the Java Virtual Machine implementation (see boolean type).

The float primitive

A float is a single-precision 32-bit IEEE 754 floating point number. By default, decimals are interpreted as doubles. To create a float, simply append an f to the decimal literal.

double doubleExample = 0.3;     // without 'f' after digits = double
float floatExample = 0.3f;      // with 'f' after digits = float
float myFloat = 23.5f;          // this is a float...
float positiveFloat = 72.8f;    // it can be positive,
float negativeFloat = -72.8f;   // or negative
float integerFloat = 47.2f;     // it can be a whole number (not an int)
float underZeroFloat = 0.0465f; // it can be a fractional value less than 0

Floats handle the five common arithmetical operations: addition, subtraction, multiplication, division, and modulus

Note : The following may vary slightly as a result of floating point errors. Some results have been rounded for clarity and readability purposes (i.e. the printed result of the addition example was actually 34.600002).

// addition
float additionResult = 15.6f + 7.2f; // result: 22.8
 
// subtraction
float subtractionResult = 50.5f - 20.7f; // result: 29.8
 
// multiplication
float multiplicationResult = 12.6f * 3.5f; // result: 44.1
 
// division
float divisionResult = 63.0f / 9.0f; // result: 7.0
 
// modulus
float modulusResult = 50.0f % 7.0f; // result: 1.0

Because of the way floating point numbers are stored (i.e. in binary form), many numbers don't have an exact representation.

float notExact = 3.1415926f;
System.out.println(notExact);  // 3.1415925

While using float is fine for most applications, neither float nor double should be used to store exact representations of decimal numbers (like monetary amounts), or numbers where higher precision is required. Instead, the BigDecimal class should be used

The default value of a float is 0.0f.

float defaultFloat; // defaultFloat == 0.0f

Afloat is precise to roughly an error of 1 in 10 million.

Note : Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN are float values. NaN stands for results of operations that cannot be determined, such as dividing 2 infinite values. Furthermore 0f and -0f are different, but == yields true: .

float f1 = 0f;
float f2 = -0f;
System.out.println(f1 == f2);    // true
System.out.println(1f / f1);     // Infinity
System.out.println(1f / f2);     // -Infinity
System.out.println(Float.POSITIVE_INFINITY / Float.POSITIVE_INFINITY); // NaN

The int primitive

A primitive data type such as int holds values directly into the variable that is using it, meanwhile a variable that was declared using Integer holds a reference to the value.

According to java API: "The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int."

By default, int is a 32-bit signed integer. It can store a minimum value of -231, and a maximum value of 231 - 1.

int valueA = -55;
int valueB = 419;
int valueC = 92;
 
int addedValues = valueB + valueC; // 419 + 92 = 511
int subtractedValues = valueB - valueC; // 419 - 92 = 327
 

If you need to store a number outside of this range, long should be used instead. Exceeding the value range of int leads to an integer overflow, causing the value exceeding the range to be added to the opposite site of the range (positive becomes negative and vise versa). The value is ((value - MIN_VALUE) % RANGE) + MIN_VALUE, or ((value GoalKicker.com – Java® Notes for Professionals 45 + 2147483648) % 4294967296) - 2147483648

int demo = 2147483647;    //maximum positive integer
System.out.println(demo); //prints 2147483647
demo = demo + 1;          //leads to an integer overflow
System.out.println(demo); // prints -214748364

The maximum and minimum values of int can be found at:

int high = Integer.MAX_VALUE; // high == 2147483647
int low = Integer.MIN_VALUE;  // low == -2147483648

The default value of an int is 0

int defaultInt; // defaultInt == 0

Converting Primitives

In Java, we can convert between integer values and floating-point values. Also, since every character corresponds to a number in the Unicode encoding, char types can be converted to and from the integer and floating-point types. boolean is the only primitive datatype that cannot be converted to or from any other primitive datatype.

There are two types of conversions:

  • Widening Conversion
  • Narrowing Conversion

A widening conversion is when a value of one datatype is converted to a value of another datatype that occupies more bits than the former. There is no issue of data loss in this case.

Correspondingly, A narrowing conversion is when a value of one datatype is converted to a value of another datatype that occupies fewer bits than the former. Data loss can occur in this case.

Java performs widening conversions automatically. But if you want to perform a narrowing conversion (if you are sure that no data loss will occur), then you can force Java to perform the conversion using a language construct known as a cast.

Widening Conversion:

int a = 1; 
double d = a; // valid conversion to double, no cast needed (widening)

Narrowing Conversion:

double d = 23.96
int b = d;        // invalid conversion to int, will throw a compile-time error
int b = (int) d;  // valid conversion to int, but result is truncated (gets rounded down)
                  // This is type-casting
                  // Now, b = 23

Memory consumption of primitives vs. boxed Primitives

Primitive Boxed Type Memory Size of Primitive / Boxed
boolean Boolean 1 byte / 16 bytes
byte Byte 1 byte / 16 bytes
short Short 2 bytes / 16 bytes
char Character 2 bytes / 16 bytes
int Integer 4 bytes / 16 bytes
long Long 8 bytes / 16 bytes
float Float 4 bytes / 16 bytes
double Double 8 bytes / 16 bytes

Boxed objects always require 8 bytes for type and memory management, and because the size of objects is always a multiple of 8, boxed types all require 16 bytes total. In addition, each usage of a boxed object entails storing a reference which accounts for another 4 or 8 bytes, depending on the JVM and JVM options.

In data-intensive operations, memory consumption can have a major impact on performance. Memory consumption grows even more when using arrays: a float[5] array will require only 32 bytes; whereas a Float[5] storing 5 distinct non-null values will require 112 bytes total (on 64 bit without compressed pointers, this increases to 152 bytes).

Boxed value caches

The space overheads of the boxed types can be mitigated to a degree by the boxed value caches. Some of the boxed types implement a cache of instances. For example, by default, the Integer class will cache instances to represent numbers in the range -128 to +127. This does not, however, reduce the additional cost arising from the additional memory indirection

If you create an instance of a boxed type either by autoboxing or by calling the static valueOf(primitive) method, the runtime system will attempt to use a cached value. If your application uses a lot of values in the range that is cached, then this can substantially reduce the memory penalty of using boxed types. Certainly, if you are creating boxed value instances "by hand", it is better to use valueOf rather than new. (The new operation always creates a new instance.) If, however, the majority of your values are not in the cached range, it can be faster to call new and save the cache lookup.


The double primitive

A double is a double-precision 64-bit IEEE 754 floating point number

double valueA = 1234.56;
double valueB = -789.01;
double valueC = 4321.09;
 
double addedDoubles = valueA + valueB;      // 445.55
double subtractedDoubles = valueA - valueB; // 2023.57
double scientificNotationDouble = 4.8e-5;   // 0.000048

Because of the way floating point numbers are stored, many numbers don't have an exact representation.

double valueX = 3.5;
double valueY = 2.1;
 
double notExactResult = valueX - valueY; // result should be 1.4
System.out.println(notExactResult);      // 1.3999999999999999
 

While using double is fine for most applications, neither float nor double should be used to store precise numbers such as currency. Instead, the BigDecimal class should be used

The default value of a double is 0.0d

public double defaultDouble; // defaultDouble == 0.0
 

Note : Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN are double values. NaN stands for results of operations that cannot be determined, such as dividing 2 infinite values. Furthermore 0d and -0d are different, but == yields true:

double d1 = 0d;
double d2 = -0d;
System.out.println(d1 == d2); // true
System.out.println(1d / d1);  // Infinity
System.out.println(1d / d2);  // -Infinity
System.out.println(Double.POSITIVE_INFINITY / Double.POSITIVE_INFINITY); // NaN

The long primitive

By default, long is a 64-bit signed integer (in Java 8, it can be either signed or unsigned). Signed, it can store a minimum value of -263, and a maximum value of 263 - 1, and unsigned it can store a minimum value of 0 and a maximum value of 264 - 1

long valueA = -55;
long valueB = 419;
long valueC = 92;
 
long addedLongs = valueB + valueC; // 419 + 92 = 511
long subtractedLongs = valueB - valueC; // 419 - 92 = 327
 
// Appending "L" at the end to specify it as a long literal
long anotherBigNumber = 1234567890123L;
 

The maximum and minimum values of long can be found at:

long high = Long.MAX_VALUE; // high == 9223372036854775807L
long low = Long.MIN_VALUE;  // low == -9223372036854775808L

The default value of a long is 0L

long defaultLong; // defaultLong == 0L

Note : letter "L" appended at the end of long literal is case insensitive, however it is good practice to use capital as it is easier to distinct from digit one:

2L == 2l; // true

The following results can be found:

Long val1 = 127L;
Long val2 = 127L;
 
System.out.println(val1 == val2); // true
 
Long val3 = 128L;
Long val4 = 128L;
 
System.out.println(val3 == val4); // false
 

To properly compare 2 Object Long values, use the following code:

Long val3 = 128L;
Long val4 = 128L;
 
System.out.println(Objects.equal(val3, val4)); // true

Comparing a primitive long to an Object long will not result in a false negative like comparing 2 objects with == does.


The boolean primitive

A boolean can store one of two values, either true or false

boolean isSunny = true;
System.out.println("isSunny = " + isSunny); // isSunny = true
 
boolean isRaining = false;
System.out.println("isRaining = " + isRaining); // isRaining = false
 
boolean notSunny = !isSunny;
System.out.println("notSunny = " + notSunny); // notSunny = false
 
boolean sunnyAndRaining = isSunny && isRaining;
System.out.println("sunnyAndRaining = " + sunnyAndRaining); // sunnyAndRaining = false
 
boolean sunnyOrRaining = isSunny || isRaining;
System.out.println("sunnyOrRaining = " + sunnyOrRaining); // sunnyOrRaining = true
 
boolean sunnyXorRaining = isSunny ^ isRaining;
System.out.println("sunnyXorRaining = " + sunnyXorRaining); // sunnyXorRaining = true
 

The default value of a boolean is fals

boolean defaultBoolean; // defaultBoolean == false

The byte primitive

A byte is a 8-bit signed integer. It can store a minimum value of -27 (-128), and a maximum value of 27 - 1 (127)

byte valueA = -15;
byte valueB = 30;
byte valueC = 12;
 
byte addedBytes = (byte) (valueB + valueC);      // 42
byte subtractedBytes = (byte) (valueB - valueC); // 18
 

The maximum and minimum values of byte can be found at:

byte high = Byte.MAX_VALUE; // high == 127
byte low = Byte.MIN_VALUE;  // low == -128

The default value of a byte is 0

byte defaultByte; // defaultByte == 0

Negative value representation

Java and most other languages store negative integral numbers in a representation called 2's complement notation.

For a unique binary representation of a data type using n bits, values are encoded like this:

The least significant n-1 bits store a positive integral number x in integral representation. Most significant value stores a bit vith value s. The value repesented by those bits is

x - s * 2n-1

i.e. if the most significant bit is 1, then a value that is just by 1 larger than the number you could represent with the other bits (2n-2 + 2n-3 + ... + 21 + 20 = 2n-1 - 1) is subtracted allowing a unique binary representation for each value from - 2n-1 (s = 1; x = 0) to 2n-1 - 1 (s = 0; x = 2n-1 - 1).

This also has the nice side effect, that you can add the binary representations as if they were positive binary numbers:

s1 s2 x1 + x2 overflow Addition Result
0 0 No x1 + x2 = v1 + v2
0 0 Yes Too large to be represented with data type (overflow)
0 1 No x1 + x2 - 2n-1 = x1 + x2 - s2 * 2n-1 = v1 + v2
0 1 Yes (x1 + x2) mod 2n-1 = x1 + x2 - 2n-1 = v1 + v2
1 0 * See above (swap summands)
1 1 No Too small to be represented with data type (x1 + x2 - 2n < -2n-1 ; underflow)
1 1 Yes (x1 + x2) mod 2n-1 - 2n-1 = (x1 + x2 - 2n-1) - 2n-1 = (x1 - s1 * 2n-1) + (x2 - s2 * 2n-1) = v1 + v2

Note that this fact makes finding binary representation of the additive inverse (i.e. the negative value) easy:

Observe that adding the bitwise complement to the number results in all bits being 1. Now add 1 to make value overflow and you get the neutral element 0 (all bits 0).

So the negative value of a number i can be calculated using (ignoring possible promotion to int here

(~i) + 1

Example : taking the negative value of 0 (byte):

The result of negating 0, is 11111111. Adding 1 gives a value of 100000000 (9 bits). Because a byte can only store 8 bits, the leftmost value is truncated, and the result is 00000000

Original Process Result
0 (00000000) Negate -0 (11111111)
11111111 Add 1 to binary 100000000
100000000 Truncate to 8 bits 00000000 (-0 equals 0)

The short primitive

A short is a 16-bit signed integer. It has a minimum value of -215 (-32,768), and a maximum value of 215 -1 (32,767)

short valueA = -550;
short valueB = 733;
short valueC = 125;
 
short addedShorts = (short) (valueB + valueC);      // 858
short subtractedShorts = (short) (valueB - valueC); // 608

The maximum and minimum values of short can be found at:

short high = Short.MAX_VALUE; // high == 32767
short low = Short.MIN_VALUE;  // low == -32768

The default value of a short is 0

short defaultShort; // defaultShort == 0
 

Basic Programs