Skip to content

Java Data types

If you've ever found yourself scratching your head over terms like integers, strings, or booleans, fear not - we're here to demystify it all and make it as clear as a sunny day.

Before we dive into the specifics, let's start with the basics. What exactly are data types? Well, think of them as categories or classifications that tell Java how to interpret and manipulate data. They define the type of values that can be stored in a variable and the operations that can be performed on those values.

NOTE

Java is what we call "statically typed." What does that mean? Well, it means that every variable and expression in Java has a type that's known at compile time. In simpler terms, when you're coding in Java, you need to declare the type of each variable before you can use it. This helps the compiler catch errors early on and ensures that your code is solid from the get-go.

Types of Data Types:

In Java, data types can be broadly categorized into two main groups: primitive data types and reference data types. Let's delve into each category and unravel its mysteries:

Primitive Data Types:

Primitive data types represent the most basic types of data that Java can handle. They are predefined by the language and are not objects. Primitive data types include:

  • Integer Types:
Data TypeSize (in bits)Minimum ValueMaximum ValueExample
byte8-128127byte num = 100;
short16-32,76832,767short num = 1000;
int32-2^312^31 - 1int num = 100000;
long64-2^632^63 - 1long num = 1000000000L;
  • Floating-Point Types:
Data TypeSize (in bits)Minimum ValueMaximum ValueExample
float32Approximately 1.4E-45Approximately 3.4E+38float num = 3.14f;
double64Approximately 4.9E-324Approximately 1.8E+308double num = 3.14;
  • Character Type:
Data TypeSize (in bits)Minimum ValueMaximum ValueExample
char16'\u0000''\uffff'char letter = 'A';
  • Boolean Type:
Data TypeSize (in bits)Minimum ValueMaximum ValueExample
booleanNot Applicablefalsetrueboolean isTrue = true;

Reference Data Types:

Reference data types, also known as object data types, refer to objects created using classes. They are stored in the heap memory and can hold a reference (address) to the actual data. Common examples of reference data types include:

  • String:

    String name = "John Doe";

  • Array:

    int[] numbers = {1, 2, 3, 4, 5};

TIP

In Java, when we talk about reference data types, it's not just about built-in stuff like strings and arrays. Your very own custom-made classes also fall into this category! So, whether you're using a class you built yourself or one that comes pre-packaged with Java, if it's not a primitive type, it's a reference type.

Example

  1. Integer Types:
java
byte numByte = 100;      // 8-bit signed integer
    short numShort = 1000;   // 16-bit signed integer
    int numInt = 100000;     // 32-bit signed integer
    long numLong = 1000000000L; // 64-bit signed integer (note the 'L' suffix)
  1. Floating-Point Types:
java
float numFloat = 3.14f;  // 32-bit floating-point number (note the 'f' suffix)
    double numDouble = 3.14; // 64-bit floating-point number
  1. Character Type:
java
char letter = 'A';       // 16-bit Unicode character
  1. Boolean Type:
java
boolean isTrue = true;   // Boolean value: true or false`
  1. String (Reference Type):
java
String name = "John Doe"; // Reference to a string object`
  1. Array (Reference Type):
java
int[] numbers = {1, 2, 3, 4, 5}; // Array of integers`
java
public class DataTypesExample {
    // Primitive data types
    byte numByte = 100;         // 8-bit signed integer
    short numShort = 1000;      // 16-bit signed integer
    int numInt = 100000;        // 32-bit signed integer
    long numLong = 1000000000L; // 64-bit signed integer
    float numFloat = 3.14f;     // 32-bit floating-point number
    double numDouble = 3.14;    // 64-bit floating-point number
    char letter = 'A';          // 16-bit Unicode character
    boolean isTrue = true;      // Boolean value: true or false
    
    // Reference data types
    String name = "John Doe";       // Reference to a string object
    int[] numbers = {1, 2, 3, 4, 5}; // Array of integers
    
}

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.