Java Data Types

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:
Primitive Data Types
Non-Primitive (Reference) Data Types
2. Primitive Data Types
Java has 8 primitive data type.
Primitive Data Types Table
| Data Type | Size | Description | Example |
|---|---|---|---|
| byte | 1 byte | Very small integer | byte b = 10; |
| short | 2 bytes | Small integer | short s = 1000; |
| int | 4 bytes | Integer (most used) | int x = 25; |
| long | 8 bytes | Large integer | long l = 100000L; |
| float | 4 bytes | Decimal (less precision) | float f = 3.14f; |
| double | 8 bytes | Decimal (more precision) | double d = 99.99; |
| char | 2 bytes | Single character | char c = 'A'; |
| boolean | 1 bit | True or false | boolean 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
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Stores | Actual value | Reference (address) |
| Size | Fixed | Variable |
| Null allowed | No | Yes |
| Examples | int, char | String, Array |
6. Type Casting (Important)
Implicit Casting (Widening)
Explicit Casting (Narrowing)
7. Default Values (Instance Variables)
| Data Type | Default Value |
|---|---|
| int | 0 |
| double | 0.0 |
| boolean | false |
| char | ‘\u0000’ |
| String | null |
8. Common Beginner Mistakes
- Forgetting
fin float - Using
charwith 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
