📝 What You’ll Learn

  • What data types are
  • Primitive vs reference types
  • Overview of all 8 primitive types
  • Type conversions (casting)

📦 What is a Data Type?

A data type defines the kind of value a variable can hold — like numbers, text, true/false, etc.

Every variable in Java must have a data type.

🔢 Primitive Data Types

Java has 8 primitive types built into the language:

Type Description Example
byte 8-bit integer byte a = 100;
short 16-bit integer short b = 2000;
int 32-bit integer int c = 10000;
long 64-bit integer long d = 100000L;
float 32-bit decimal float e = 5.75f;
double 64-bit decimal (default) double f = 19.99;
char Single Unicode character char g = 'A';
boolean True or false boolean h = true;

Use float with an f suffix, and long with an L suffix.

🧱 Reference (Non-Primitive) Data Types

These store references (addresses) to objects in memory.

Common examples:

  • String
  • Arrays (int[], String[])
  • Classes and Objects
String name = "Java";
int[] numbers = {1, 2, 3};

🔁 Type Conversion (Casting)

🔹 Widening Conversion (automatic)

int a = 10;
double b = a;  // OK: int to double

🔸 Narrowing Conversion (manual)

double x = 9.8;
int y = (int) x;  // y = 9

💡 Default Values (for class-level variables)

Type Default
int 0
double 0.0
boolean false
char \u0000
String null

Local variables must be initialized before use.

🧠 Tip:

Prefer int for whole numbers, double for decimals unless you need precision control (use float).

📘 Recap

  • Java has 8 built-in primitive types and many reference types
  • Use the right type for the right job
  • Type conversions let you shift between compatible types
  • Reference types point to objects, not raw data