Data Types in Java

Short Answer:
Java data types define the type of data a variable can store. They are divided into Primitive and Non-Primitive (Reference) types.

Detailed Explanation:
Java supports 8 primitive data types and several reference types like String, Array, Class, etc.


1. Primitive Data Types (8 Types)

Type Size Example
byte 1 byte byte b = 10;
short 2 bytes short s = 200;
int 4 bytes int i = 1000;
long 8 bytes long l = 10000L;
float 4 bytes float f = 10.5f;
double 8 bytes double d = 20.99;
char 2 bytes char c = 'A';
boolean JVM dependent boolean flag = true;

2. Non-Primitive (Reference) Data Types

  • String
  • Array
  • Class
  • Interface
  • Enum

Reference types store the address of the object, not the actual value.


Example

public class Test {
    public static void main(String[] args) {

        int age = 25;                // Primitive
        double salary = 50000.50;    // Primitive

        String name = "Aftab";       // Reference
        int[] arr = {1,2,3};         // Reference

        System.out.println(name + " - " + age);
    }
}

Important Interview Points

  • Primitive types store actual values.
  • Reference types store memory address.
  • Default value of boolean is false.
  • Default data type for decimal numbers is double.
  • Wrapper classes convert primitive to object (int → Integer).

0 comments

Leave a comment