Java Data Types

 

When learning Java, one of the most important concepts you’ll encounter early on is data types. In programming, a data type defines what kind of value a variable can hold and what operations can be performed on it. Since Java is a strongly typed language, every variable must have a declared data type before it can be used.

In this guide, we’ll explore the different data types in Java with examples to help you understand them better.

What are Data Types in Java?

 

A data type specifies the type of data a variable can store. For example, you might store:

  • Numbers (like 10, 3.14)
  • Characters (like ‘A’, ‘b’)
  • Boolean values (true/false)
  • Strings (like “Hello, Java!”)

Java data types are divided into two categories:

  1. Primitive Data Types
  2. Non-Primitive (Reference) Data Types

1. Primitive Data Types

 

Primitive data types are the basic building blocks of Java. They are predefined by the Java language and hold simple values. Java has 8 primitive data types:

Data TypeSizeDefault ValueExampleDescription
byte1 byte (8-bit)0byte age = 25;Stores small integers from -128 to 127
short2 bytes (16-bit)0short year = 2025;Stores integers from -32,768 to 32,767
int4 bytes (32-bit)0int salary = 50000;Most commonly used for integers (-2^31 to 2^31-1)
long8 bytes (64-bit)0Llong population = 8000000000L;Stores very large integers (-2^63 to 2^63-1)
float4 bytes (32-bit)0.0ffloat pi = 3.14f;Stores fractional numbers (single precision)
double8 bytes (64-bit)0.0ddouble price = 199.99;Stores fractional numbers (double precision, more accurate)
char2 bytes (16-bit, Unicode)‘\u0000’char grade = 'A';Stores a single character
boolean1 bit (JVM dependent)falseboolean isJavaFun = true;Stores logical values: true or false

Example: Using Primitive Data Types

 

public class PrimitiveExample {
  public static void main(String[] args) {
    byte age = 25;
    short year = 2025;
    int salary = 50000;
    long population = 8000000000L;
    float pi = 3.14f;
    double price = 199.99;
    char grade = ‘A’;
    boolean isJavaFun = true;

    System.out.println(“Age: ” + age);
    System.out.println(“Year: ” + year);
    System.out.println(“Salary: ” + salary);
    System.out.println(“Population: ” + population);
    System.out.println(“Pi: ” + pi);
    System.out.println(“Price: ” + price);
    System.out.println(“Grade: ” + grade);
    System.out.println(“Is Java Fun? ” + isJavaFun);
  }
}

2. Non-Primitive (Reference) Data Types

 

Non-primitive data types are objects in Java. They do not store the actual value directly but rather a reference (memory address) to the object.

Some common non-primitive types include:

  • String – Stores sequences of characters.
  • Arrays – Stores multiple values of the same type.
  • Classes – User-defined types that can contain variables and methods.
  • Interfaces – Abstract types used to define a contract.

Example: Using Non-Primitive Data Types

 

public class NonPrimitiveExample {
  public static void main(String[] args) {
    String message = “Hello, Java!”;
    int[] numbers = {10, 20, 30, 40};

    System.out.println(“Message: ” + message);
    System.out.println(“First Number: ” + numbers[0]);
  }
}

Primitive vs. Non-Primitive Data Types

 

FeaturePrimitive Data TypesNon-Primitive Data Types
Defined byJava languageProgrammer (or Java library)
Memory StorageStores actual valuesStores reference to object
Examplesint, float, char, booleanString, Array, Class, Interface
Default Valuese.g., 0, 0.0, falsenull
OperationsFaster and more efficientRich functionality, more powerful

Key Points to Remember

 
  • Java is strongly typed, so you must declare a variable’s data type before use.
  • Use int for most integer calculations unless you need very large numbers (then use long).
  • Use double for most decimal values (float is less precise).
  • Always append L to long values and f to float values.
  • String is not a primitive type but is used almost like one because it’s so common.
Java Data Types

Conclusion

 

Understanding data types in Java is essential because they form the foundation of all programming tasks. Whether you’re dealing with numbers, characters, or objects, knowing which data type to use ensures that your programs run efficiently and without errors.

By mastering both primitive and non-primitive data types, you’ll have a solid grasp of how Java manages and processes data — an essential step in becoming a confident Java programmer.

Scroll to Top