Understanding Java Data Types: Primitive and Non-Primitive
Java data types define the type of data a variable can store in a program. They help the compiler allocate memory efficiently, ensure type safety, and improve overall program reliability.
Java is a statically typed language, which means every variable must be declared with a data type before it is used.
Basic Syntax
dataType variableName = value;
Example:
int age = 21;
Why Data Types Are Important?
- Memory Allocation → Determines how much memory is required
- Operation Support → Defines allowed operations
- Type Safety → Prevents invalid assignments
- Default Values → Each data type has a default value
Types of Data Types in Java
Java provides two main categories:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive data types are the most basic types in Java. They store actual values directly in memory and have a fixed size.
Primitive Data Types in Java (Detailed Explanation)
Java provides 8 primitive data types. These are used to store simple values directly in memory and are the foundation of all Java programs.
1. boolean Data Type
The boolean data type represents only two values: true or false. It is mainly used in decision-making statements like if, while, and loops.
Syntax:
boolean isActive;
Example:
public class Main {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Is fish tasty? " + isFishTasty);
}
}
Output:
Is Java fun? true
Is fish tasty? false
2. byte Data Type
The byte data type is an 8-bit signed integer. It is useful for saving memory in large arrays.
Syntax:
byte value;
Example:
public class Main {
public static void main(String[] args) {
byte age = 25;
byte temperature = -10;
System.out.println("Age: " + age);
System.out.println("Temperature: " + temperature);
}
}
Output:
Age: 25
Temperature: -10
3. short Data Type
The short data type is a 16-bit signed integer used when values are within a moderate range.
Syntax:
short number;
Example:
public class Main {
public static void main(String[] args) {
short students = 1000;
short temp = -200;
System.out.println("Number of Students: " + students);
System.out.println("Temperature: " + temp);
}
}
Output:
Number of Students: 1000
Temperature: -200
4. int Data Type
The int data type is the most commonly used integer type in Java. It is a 32-bit signed integer.
Syntax:
int number;
Size: 4 bytes (32 bits)
Example:
public class Main {
public static void main(String[] args) {
int population = 2000000;
int distance = 150000000;
System.out.println("Population: " + population);
System.out.println("Distance: " + distance);
}
}
Output:
Population: 2000000
Distance: 150000000
5. long Data Type
The long data type is a 64-bit signed integer, used for very large values.
Syntax:
long number;
Size: 8 bytes (64 bits)
Example:
public class Main {
public static void main(String[] args) {
long worldPopulation = 7800000000L;
long lightYears = 9460730472580800L;
System.out.println("World Population: " + worldPopulation);
System.out.println("Light Years: " + lightYears);
}
}
Output:
World Population: 7800000000
Light Years: 9460730472580800
6. float Data Type
The float data type is a 32-bit floating-point type used for decimal values with less precision.
Syntax:
float number;
Size: 4 bytes (32 bits)
Example:
public class Main {
public static void main(String[] args) {
float pi = 3.14f;
float gravity = 9.81f;
System.out.println("Pi: " + pi);
System.out.println("Gravity: " + gravity);
}
}
Output:
Pi: 3.14
Gravity: 9.81
7. double Data Type
The double data type is a 64-bit floating-point type and is the default for decimal values.
Syntax:
double number;
Size: 8 bytes (64 bits)
Example:
public class Main {
public static void main(String[] args) {
double pi = 3.141592653589793;
double avogadro = 6.02214076e23;
System.out.println("Pi: " + pi);
System.out.println("Avogadro's Number: " + avogadro);
}
}
Output:
Pi: 3.141592653589793
Avogadro's Number: 6.02214076E23
8. char Data Type
The char data type is used to store a single character. It uses Unicode and supports multiple languages.
Syntax:
char letter;
Size: 2 bytes (16 bits)
Example:
public class Main {
public static void main(String[] args) {
char grade = 'A';
char symbol = '$';
System.out.println("Grade: " + grade);
System.out.println("Symbol: " + symbol);
}
}
Output:
Grade: A
Symbol: $
2.Non-Primitive (Reference) Data Types in Java
Unlike primitive data types, non-primitive data types do not store actual values directly. Instead, they store references (memory addresses) that point to objects.
These types are more powerful and are used to build real-world applications using Object-Oriented Programming (OOP).
1. String
A String represents a sequence of characters enclosed in double quotes. In Java, strings are objects and are immutable, meaning their value cannot be changed after creation.
Syntax:
```java id="p7t4yr"
String str = "Hello";
**Example:**
```java id="v2h5xq"
public class Main {
public static void main(String[] args) {
String name = "Geek1";
String message = "Welcome to Java";
System.out.println("Name: " + name);
System.out.println("Message: " + message);
}
}
Output:
Name: Geek1
Message: Welcome to Java
Note: Strings cannot be modified after creation. For heavy modifications, use StringBuilder.
2. Class
A class is a user-defined blueprint used to create objects. It defines properties (variables) and behaviors (methods).
Example:
```java id="3n5k9p"
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
void display() {
System.out.println(model + " " + year);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2020);
myCar.display();
}
}
**Output:**
```id="5l2m1n"
Toyota 2020
3. Object
An object is an instance of a class. It represents real-world entities and contains:
- State → Data (variables)
- Behavior → Methods
- Identity → Unique reference
Example:
```java id="8q1z9w"
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Honda", 2021);
System.out.println("Model: " + myCar.model);
System.out.println("Year: " + myCar.year);
}
}
**Output:**
```id="1a2b3c"
Model: Honda
Year: 2021
4. Interface
An interface defines a contract that classes must follow. It contains abstract methods that must be implemented.
It helps achieve abstraction and supports multiple inheritance in Java.
Example:
```java id="k4p8w2"
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
}
}
**Output:**
```id="z8y7x6"
Woof
5. Array
An array is used to store multiple values of the same type in a single structure. Arrays in Java are objects, dynamically allocated, and indexed starting from 0.
Example:
```java id="j9v2t6"
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Geek1", "Geek2", "Geek3"};
System.out.println("First number: " + numbers[0]);
System.out.println("Second name: " + names[1]);
}
}
**Output:**
```id="l0m9n8"
First number: 1
Second name: Geek2
References
GeeksforGeeks – Java Data Types
https://www.geeksforgeeks.org/java/java-data-types/Programiz – Java Primitive Data Types
https://www.programiz.com/java-programming/variables-primitive-data-types

Top comments (0)