Data Type
data type in programming defines the type of data a variable can store. It helps the computer understand how much memory to allocate and what operations can be performed on the data.
For example, in Java:
int → Stores whole numbers (e.g., 10, -5)
double → Stores decimal numbers (e.g., 3.14, -0.99)
char → Stores a single character (e.g., 'A', 'b')
boolean → Stores true or false values
Why are data types important?
Memory efficiency – Prevents unnecessary memory usage.
Data integrity – Ensures only valid operations are performed.
Code clarity – Makes the program easier to understand.
Error prevention – Avoids accidental data mismatches.
Primitive Data Types in Java
Types of Primitive Data Types
A primitive data type in Java is a basic type of data that is not an object. These types are predefined by the Java language and store simple values directly in memory. They are more efficient than objects because they require less memory and have faster processing speed.
Why Use Primitive Data Types in Java?
Primitive data types are used because they are efficient, fast, and memory-friendly. Here are the key reasons why Java provides primitive data types:
1. Predefined by Java
Java provides 8 primitive data types (byte, short, int, long, float, double, char, boolean).
2. Efficient Memory Usage
Primitive types consume less memory compared to objects.
Example: int uses 4 bytes, while an Integer object takes more memory.
3. Faster Execution
Primitive data types are stored in stack memory, making them faster to access and manipulate.
4. No Methods or Properties
Unlike objects, primitives do not have methods.
Example: int a = 10; does not have methods like a.toString().
5. Stored by Value, Not by Reference
When you assign one primitive variable to another, a copy of the value is created.
Example:
int x = 5;
int y = x; // Copy of x is assigned to y
x = 10; // Changing x does not affect y
6. Immutable Values
The value of a primitive cannot be modified directly, but the variable can be reassigned.
Example:
int a = 5;
a = 10; // The value of a is changed, but 5 remains unchanged in memory.
7. Do Not Support Null Values
Primitive types cannot be null.
Example:
int num = null; // ❌ Compilation Error
8. Default Values
If not initialized, primitive types have default values.
Example:
class Test {
static int num;
public static void main(String[] args) {
System.out.println(num); // Output: 0
}
}
9. Compatible with Wrapper Classes
Java provides wrapper classes (Integer, Double, Boolean, etc.) to use primitive types as objects.
Java has 8 primitive data types, which store simple values directly in memory.
1.byte – Stores small whole numbers from -128 to 127. Uses 1 byte of memory.
Example: byte age = 25;
2.short– Stores larger whole numbers from -32,768 to 32,767. Uses 2 bytes of memory.
Example: short year = 2025;
3.int – The most commonly used type for whole numbers, ranging from -2,147,483,648 to 2,147,483,647. Uses 4 bytes.
Example: int salary = 50000;
4.long – Stores very large whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Uses 8 bytes. Requires L at the end of the number.
Example: long distance = 12345678910L;
5.float – Stores decimal numbers with less precision. Uses 4 bytes. Requires f at the end.
Example: float pi = 3.14f;
6.double – Stores decimal numbers with higher precision. Uses 8 bytes.
Example: double price = 99.99;
7.char – Stores a single character using Unicode (e.g., 'A', '1', '$'). Uses 2 bytes.
Example: char letter = 'A';
8.boolean – Stores true or false. Size depends on the JVM, but usually 1 byte.
Example: boolean isJavaFun = true;
Non-Primitive Data Types in Java
Non-primitive data types are more complex than primitive data types. They do not store the actual value directly in memory but instead store a reference (address) to the value.
Types of Non-Primitive Data Types
1.Strings – A sequence of characters.
Example:
String name = "Sugumar";
System.out.println(name);
2.Arrays – A collection of elements of the same type.
Example:
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[0]); // Output: 10
3.Classes – User-defined blueprints for creating objects.
Example:
class Person {
String name = "Sugumar";
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
System.out.println(p.name);
}
}
4.Objects – Instances of a class that contain both data and methods.
Example:
class Car {
void display() {
System.out.println("This is a car.");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.display();
}
}
5.Interfaces – A blueprint for classes that defines methods without implementations.
Example:
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark!");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.makeSound();
}
}
6.Collections (ArrayList, HashMap, etc.) – Used to store multiple objects dynamically.
Example using ArrayList:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("Java");
list.add("Python");
System.out.println(list);
}
}
Java is not considered fully object-oriented.
Java is an Object-Oriented Programming (OOP) language, but it also supports primitive data types like int, char, and boolean, which are not objects. This is why Java is not considered fully object-oriented.
Why is Java not 100% Object-Oriented?
1. Primitive Data Types:
$ Java includes primitive types (int, char, boolean, etc.) that are not objects.
$ This improves performance because objects use more memory than primitives.
2. Static Methods:
$ Java allows static methods (Math.sqrt(), Integer.parseInt(), etc.), which do not require objects.
3. Direct Memory Access:
$ Java allows direct memory allocation using primitives, which is not purely OOP.
What Makes Java an Object-Oriented Language?
Java follows OOP principles:
✔ Encapsulation – Data hiding using private variables and getters/setters.
✔ Inheritance – One class can inherit properties from another.
✔ Polymorphism – The same method can behave differently in different classes.
✔ Abstraction – Hiding implementation details using abstract classes and interfaces.