Java Data Types

Java Tutorial

 Java Data Types – Complete Guide

In Java, data types define what kind of data a variable can store and how much memory it uses.


 1. Types of Data Types in Java

Java data types are divided into two main categories:

  1. Primitive Data Types

  2. Non-Primitive (Reference) Data Types


2. Primitive Data Types

Java has 8 primitive data type.

 Primitive Data Types Table

Data TypeSizeDescriptionExample
byte1 byteVery small integerbyte b = 10;
short2 bytesSmall integershort s = 1000;
int4 bytesInteger (most used)int x = 25;
long8 bytesLarge integerlong l = 100000L;
float4 bytesDecimal (less precision)float f = 3.14f;
double8 bytesDecimal (more precision)double d = 99.99;
char2 bytesSingle characterchar c = 'A';
boolean1 bitTrue or falseboolean isActive = true;

 3. Example Program (Primitive Types)


 


 4. Non-Primitive (Reference) Data Types

These store references (addresses), not actual values.

Common Non-Primitive Types

  • String

  • Arrays

  • Classes

  • Objects

  • Interfaces


 String


 Array


 Object


 


 5. Difference: Primitive vs Non-Primitive

FeaturePrimitiveNon-Primitive
StoresActual valueReference (address)
SizeFixedVariable
Null allowedNoYes
Examplesint, charString, Array

 6. Type Casting (Important)

Implicit Casting (Widening)


Explicit Casting (Narrowing)


7. Default Values (Instance Variables)

Data TypeDefault Value
int0
double0.0
booleanfalse
char‘\u0000’
Stringnull

 8. Common Beginner Mistakes

  •  Forgetting f in float
  •  Using char with double quotes
  •  Assuming String is primitive

 9. Interview Questions (Must Know)

Q1: How many primitive data type in Java?
 8

Q2: Size of int?
 4 bytes

Q3: Is String a primitive type?
 No

Q4: Difference between float and double?
 Precision & size


 Quick Cheat Sheet

You may also like...