Data Types in Java
Understanding how Java stores different kinds of information
Think of data types like different kinds of containers. Just like you use a water bottle for water, a lunchbox for food, and a backpack for books, Java uses different types to store different kinds of information!
Primitive Data Types (Basic Building Blocks)
These are Java's simplest types - like basic LEGO blocks
byte8 bitsRange: -128 to 127
💡 Like counting on your fingers (very small numbers)
Use when saving memory is important
short16 bitsRange: -32,768 to 32,767
💡 Like the number of students in a school
Rarely used, but good for medium-sized numbers
int32 bitsRange: -2 billion to 2 billion
💡 Like counting population of a city
Most common type for whole numbers
long64 bitsRange: Really, really big numbers!
💡 Like counting stars in the sky or money in billions
For very large whole numbers
float32 bitsRange: Decimal numbers (less precise)
💡 Like measuring your height: 5.5 feet
When you need decimals but not super accurate
double64 bitsRange: Decimal numbers (more precise)
💡 Like calculating distance: 123.456789 miles
Most common type for decimal numbers
char16 bitsRange: Single character
💡 'A', 'z', '5', '$'
For storing a single letter or symbol
boolean1 bitRange: true or false
💡 Like a light switch: ON or OFF
For yes/no, true/false decisions
📝 Code Examples: Primitive Types
public class PrimitiveTypesDemo { public static void main(String[] args) { // Integer types (whole numbers) byte smallNumber = 100; // -128 to 127 short studentCount = 1500; // -32,768 to 32,767 int cityPopulation = 5000000; // Most common for whole numbers long starCount = 9876543210L; // Note the 'L' at the end! // Decimal types (numbers with decimal points) float height = 5.9f; // Note the 'f' at the end! double distance = 123.456789; // Most common for decimals // Character type (single character) char grade = 'A'; char symbol = '$'; // Boolean type (true or false) boolean isHappy = true; boolean isRaining = false; // Printing values System.out.println("Small number: " + smallNumber); System.out.println("Population: " + cityPopulation); System.out.println("Height: " + height); System.out.println("Distance: " + distance); System.out.println("Grade: " + grade); System.out.println("Is happy? " + isHappy); }}Reference Types (Complex Containers)
These can hold multiple things - like a toolbox or a shopping cart
String
Text like "Hello World"A sequence of characters - like a sentence
Arrays
int[] numbers = {1, 2, 3, 4, 5}A list of items of the same type - like a row of lockers
Objects
Custom classes like Person, Car, BookComplex things with properties - like a toy with multiple features
📝 Code Examples: Reference Types
public class ReferenceTypesDemo { public static void main(String[] args) { // String - sequence of characters String greeting = "Hello, World!"; String name = "Alice"; String message = greeting + " My name is " + name; System.out.println(message); System.out.println("Length: " + message.length()); // Arrays - list of items int[] numbers = {10, 20, 30, 40, 50}; String[] fruits = {"Apple", "Banana", "Orange"}; System.out.println("First number: " + numbers[0]); System.out.println("Second fruit: " + fruits[1]); System.out.println("Array length: " + numbers.length); // Loop through array for (int i = 0; i < fruits.length; i++) { System.out.println("Fruit " + (i+1) + ": " + fruits[i]); } // Objects - custom types Person student = new Person("Bob", 20); System.out.println(student.getName() + " is " + student.getAge() + " years old"); }}// Simple Person classclass Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}🔍 Primitive vs Reference: What's the Difference?
public class ComparisonDemo { public static void main(String[] args) { // PRIMITIVE TYPES // Stored directly in memory int a = 10; int b = a; // b gets a COPY of the value b = 20; // changing b doesn't affect a System.out.println("a = " + a); // Still 10 System.out.println("b = " + b); // Now 20 // REFERENCE TYPES // Store memory address (reference) to the actual object int[] arr1 = {1, 2, 3}; int[] arr2 = arr1; // arr2 gets REFERENCE to same array arr2[0] = 99; // changing arr2 also changes arr1! System.out.println("arr1[0] = " + arr1[0]); // 99! System.out.println("arr2[0] = " + arr2[0]); // 99! // STRING is special - immutable reference type String str1 = "Hello"; String str2 = str1; str2 = "Goodbye"; // Creates NEW string, doesn't change str1 System.out.println("str1 = " + str1); // Still "Hello" System.out.println("str2 = " + str2); // "Goodbye" }}🎯 Key Takeaways
- ✓Primitive types store simple values directly in memory
- ✓Reference types store the memory address where the actual object lives
- ✓int and double are the most commonly used primitive types
- ✓String is the most common reference type
- ✓Choose the right type to save memory and improve performance
💼 Interview Tips
- 💡Always remember: int for whole numbers, double for decimals, boolean for true/false
- 💡Strings are immutable (can't be changed after creation)
- 💡Be careful with float/double for money - use BigDecimal instead!
- 💡char uses single quotes 'A', String uses double quotes "Hello"
- 💡Default values: numbers = 0, boolean = false, objects = null
⚠️ Common Mistakes
- ✗Using == to compare Strings (use .equals() instead)
- ✗Mixing up int division (5/2 = 2) vs double division (5.0/2.0 = 2.5)
- ✗Forgetting 'L' suffix for long: long big = 3000000000L
- ✗Using float for currency calculations (precision problems!)
- ✗Not initializing variables before using them