Data Types



- Primitives

An integer type means a value that is a numeric integer, a number without the decimal part. This statement applies to the first 4 that we see in the table. The fifth, or char, is different from the others because, although belongs to the same category, in reality it does not represent a number, but represents a character, belonging to the same category depends on the fact that it is possible to assign an integer to a char as a value, provided that this number is not negative and not higher than 65535.

NOTE: In Java 9 the character set used by default is Unicode (UTF-16 encoding).

Numeric Data Types
 Type  Default value  Bit  Example 
 byte  0 8 byte a = 100
 short  0 16 short s = 10000
 int  0 32 int a = 100000
 long  0L 64 long a = 100000L
 char  '\u0000' 16 char letterB = 'B'

With a floating-point type we mean a numeric value that is not integer, since it is divided into two parts: an integer part (before the point) and a decimal part (after the point).

Floating-Point Types
 Type Default value  Bit  Example 
 float  0.0f 32 float f1 = 234.5f
 double  0.0d 64 double d1 = 123.4

A Boolean type can only take two values: true or false.

Boolean type
 Type Default value  Bit  Example 
 boolean  false 32 boolean b1 = true

- References

With reference type we mean those variables that are represented by a reference pointing to an area of ​​memory in which an object has been allocated. If we assign another variable of the same type to a reference variable as value, they will point to the same memory address, so a change to the first will affect the second variable and vice versa, now let's see a simple example:

public class Hill {

    public String altitude;

    public Hill(String altitude) {
        this.altitude = altitude;
    }
}

Now we see that if we change altitude of hillX, it will also change altitude of hillY:

    Hill hillX = new Hill("500 meters");
    Hill hillY = hillX;
    hillX.altitude = "600 meters";
    System.out.println(hillX.altitude);
    System.out.println(hillY.altitude);

Output: 600 meters
              600 meters

Note: if we had modified hillY's altitude, the result it would have been analogous.