4) Java's Primitive Data Types Lesson

What are the Java Data Types?

8 min to complete · By Ryan Desmond

Java is a strongly typed language. It requires all variables to be declared with a variable type and name, which informs Java what data type will be stored and its value.

Categories of Types in Java

  1. Primitive Data Types
  2. Object Data Types

What are Primitive Types in Java?

Java offers eight primitive data types. Learn more about these types below.

1. Boolean

A boolean type represents a true or false value. It has a size of 1 bit. Here's an example:

class Main {
  public static void main(String[] args){
    boolean isEmpty = false;
    System.out.println("isEmpty = " + isEmpty);
  }
}

2. Char

A char typically stores single characters such as "a" or "z". In Java, char (character) is a 16-bit type ranging from 0 to 65,535. The standard 8-bit ASCII character set (all the letters and symbols you know and love in English and many other languages range from 0 to 127). ASCII characters are valid Java characters. Interestingly, it is also possible to perform arithmetic manipulations on a char variable, such as incrementing. Lastly, notice that a char is always wrapped in single quotes. Here's an example:

class Main {
  public static void main(String[] args){
    char letter = 'A';  
    letter++; // letter now equals 'B'
    System.out.println("letter = " + letter);
  }
}

3. Byte

A byte is for very small numbers, up to 127 and down to -128. It has a size of 8 bits. Here's an example:

class Main {
  public static void main(String[] args){
    byte byteVal = 126;
    System.out.println("byteVal = " + byteVal);
  }
}

4. Short

A short is a Unicode character. Notice that 32,767 is the largest value a short can hold, and conversely, -32,768 is the lowest value it can hold. A short has a size of 16 bits. Here's an example:

class Main {
  public static void main(String[] args){
    short shortVal = 32767;
    System.out.println("shortVal = " + shortVal);
  }
}

5. Int

An int represents an integer value. Only whole numbers can be assigned as an int value. It has a maximum size of 32 bits. Notice that 2,147,483,647 is the largest value an int can hold; conversely, -2,147,483,648 is the lowest value. Here's an example:

class Main {
  public static void main(String[] args){
    int intVal = 2147483647;
    System.out.println("intVal = " + intVal);
  }
}

6. Long

A long value represents an integer value. Only whole numbers can be assigned as a long value. It has a maximum size of 64 bits. Notice that 9,223,372,036,854,775,807 is the largest value a long can hold, and conversely, -9,223,372,036,854,775,808 is the lowest value it can hold. Here's an example:

// notice that there is an "l" at the end of this number.  
// this is to inform the JVM that this is indeed a 
// long and not an int  
class Main {
  public static void main(String[] args){
    long longVal = 9223372036854775807l;
    System.out.println("longVal = " + longVal);
  }
}

7. Float

A floating point number is a word for a number containing fractional components. Java defines a float as one of two floating-point types. Notice that 340,282,346,638,528,860,000,000,000,000,000,000,000.000000 is the largest value a float can hold - wow, that's big!

//notice that there is an "f" on the number below  
// this is to inform the JVM that this is indeed a 
// float and not a double  
class Main {
  public static void main(String[] args){
    float floatVal = 
      340282346638528860000000000000000000000.000000f;
    System.out.println("floatVal = " + floatVal);
  }
}

8. Double

A double is a floating-point number with double precision and a maximum value of 64 bits. If you thought the largest float value was big, check out the maximum value of a double below:

/*  
The largest value a double can hold is: 
179,769,313,486,231,570,000,000,000,000,000,000,000,000,  
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,  
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000, 
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,  
000,000,000,000,000,000,000.000000 (WOW!!)
\*/
class Main {
  public static void main(String[] args){
    double doubleVal = 123.456;  
    System.out.println("doubleVal = " + doubleVal);
  }
}

Tree of Primitive Types

graphic showing Java primitive data types and their relationships to each other

Further Reading: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Variables

Please note that you can name variables anything you want. Variable names should be self-explanatory. You will dig into working with variables in the next section. But for now, please be aware that you can name variables (the word that follows the data type) anything you like. The more self-explanatory, the better. In the examples above, you saw variables with names: isEmpty, letter, byteVal, and intVal. These are all examples of self-explanatory variable names.

Summary: Data Types in Java

  • Java is a strongly typed language
  • The types in Java are primitive and non-primitive
  • All variables require a type and a name
  • Variable names are best when they are self-explanatory

Primitive Types in Java

  1. Boolean: bool -> true or false
  2. Character: char -> single letter
  3. Byte: byte -> 8-bit integer
  4. Short: short -> 16-bit integer
  5. Int: int -> 32-bit integer
  6. Long: long -> 64-bit integer
  7. Float: float -> 32-bit floating point (decimal)
  8. Double: double -> 64-bit floating point (decimal)