There are two types of java data types: Primitive data types: The primitive java data types include boolean, char, byte, short, int, long, float, and double. Non-primitive java data types: The non-primitive java data types include Classes, Interfaces, and Arrays.

## The Double Primitive Java Data Type

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

double example = -7162.37; double myDouble = 974.21; double anotherDouble = 658.7; double addedDoubles = myDouble + anotherDouble; // 315.51 double subtractedDoubles = myDouble - anotherDouble; // 1632.91 double scientificNotationDouble = 1.2e-3; // 0.0012

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

double notExact = 1.32 - 0.42; // result should be 0.9 System.out.println(notExact); // 0.9000000000000001

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 is

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 example = -42; long myLong = 284; long anotherLong = 73; //an "L" must be appended to the end of the number, because by default, //numbers are assumed to be the int type. Appending an "L" makes it a long //as 549755813888 (2 ^ 39) is larger than the maximum value of an int (2^31 - 1), //"L" must be appended long bigNumber = 549755813888L; long addedLongs = myLong + anotherLong; // 284 + 73 = 357 long subtractedLongs = myLong - anotherLong; // 284 - 73 = 211

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 the long literal is case insensitive, however, it is good practice to use capital as it is easier to distinguish from digit one:

2L == 2l; // true

**Warning**: Java caches Integer objects instances from the range -128 to 127.

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(From Java 1.7 onward):

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 foo = true; System.out.println("foo = " + foo); // foo = true boolean bar = false; System.out.println("bar = " + bar); // bar = false boolean notFoo = !foo; System.out.println("notFoo = " + notFoo); // notFoo = false boolean fooAndBar = foo && bar; System.out.println("fooAndBar = " + fooAndBar); // fooAndBar = false boolean fooOrBar = foo || bar; System.out.println("fooOrBar = " + fooOrBar); // fooOrBar = true boolean fooXorBar = foo ^ bar; System.out.println("fooXorBar = " + fooXorBar); // fooXorBar = true The default value of a boolean is false boolean defaultBoolean; // defaultBoolean == falseInteger objects instances from the range -128 to 127.

#### 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 example = -36;

byte myByte = 96;

byte anotherByte = 7;

byte addedBytes = (byte) (myByte + anotherByte); // 103

byte subtractedBytes = (byte) (myBytes - anotherByte); // 89

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. The most significant value stores a bit with value s. The value represented 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:v1 = x1 – s1 * 2n-1

v2 = x2 - s2 * 2n-1

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 | 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 java data type

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 example = -48; short myShort = 987; short anotherShort = 17; short addedShorts = (short) (myShort + anotherShort); // 1,004 short subtractedShorts = (short) (myShort - anotherShort); // 970

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