Java Data Types: Primitive and Non-Primitive Types
In Java, data types define the type of data a variable can hold. Java is a statically-typed language, meaning each variable must be declared with a data type before it is used. This ensures type safety, which helps catch errors at compile time.
Java data types are broadly categorized into:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
This module will explain each type in detail with syntax, examples, use-cases, and important tips.
1. Primitive Data Types
Primitive Data Types: These are the most basic types of data, directly supported by the language. Examples include int, char, float, boolean, etc. They store simple values directly in memory and are not objects.
Java provides 8 primitive data types which are built into the language and serve as the most basic kinds of data. These are:
Data Type | Size | Description |
---|---|---|
byte | 1 byte | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2^31 to 2^31-1 |
long | 8 bytes | Stores very large whole numbers |
float | 4 bytes | Stores fractional numbers, up to 7 digits |
double | 8 bytes | Stores fractional numbers, up to 15 digits |
boolean | 1 bit | Stores true or false |
char | 2 bytes | Stores a single character |
A. Integer Types: byte, short, int, long
These data types are used to store whole numbers without decimal points.
1. byte
- Range: -128 to 127
- Use when saving memory in large arrays (like in embedded systems)
- Example:
byte age = 25;
2. short
- Range: -32,768 to 32,767
- Larger than byte, but smaller than int
- Example:
short distance = 15000;
3. int
- Most commonly used integer data type
- Range: -2,147,483,648 to 2,147,483,647
- Example:
int salary = 50000;
4. long
- Used for larger numbers
- Must end the number with L
- Example:
long population = 7800000000L;
B. Floating Point Types: float, double
These are used to store decimal numbers.
5. float
- Precision: 6-7 decimal digits
- Requires suffix f or F
- Used when memory savings are more important than precision
- Example:
float pi = 3.14f;
6. double
- Default type for decimal values
- Precision: 15-16 decimal digits
- Example:
double gravity = 9.80665;
C. Boolean Type
7. boolean
- Stores one of two values: true or false
- Often used in conditions and decision making
- Example:
boolean isJavaFun = true;
D. Character Type
8. char
- Stores a single 16-bit Unicode character
- Enclosed in single quotes ('A', 'b', '3', '@')
- Example:
char grade = 'A';
You can also use Unicode values:
char ch = '\u0041'; // Output: A
2. Non-Primitive Data Types
Non-Primitive (Reference) Data Types: These include classes, interfaces, arrays, and strings. They do not store the value directly but hold a reference to the location in memory where the data is stored. Non-primitive types are created by the programmer or provided as part of Java’s API. Also called reference types, these include:
- Strings
- Arrays
- Classes
- Interfaces
- Enums
Unlike primitive types, non-primitive types:
- Are created by the programmer
- Can be null
- Have methods associated with them
A. String
A String is a sequence of characters. It is not a primitive type, but it is so commonly used that it behaves almost like one.
Declaring a String:
String greeting = "Hello, World!";
Common String Methods:
String name = "Java";
System.out.println(name.length()); // Output: 4
System.out.println(name.toUpperCase()); // Output: JAVA
System.out.println(name.toLowerCase()); // Output: java
System.out.println(name.charAt(1)); // Output: a
B. Arrays
Arrays store multiple values of the same data type in a single variable.
Example:
int[] numbers = {10, 20, 30};
System.out.println(numbers[0]); // Output: 10
Arrays can also be of non-primitive types:
String[] names = {"Alice", "Bob", "Charlie"};
C. Class
A class is a blueprint for creating objects. It is a user-defined data type.
Example:
class Student {
int age;
String name;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.age = 20;
s1.name = "Neha";
System.out.println(s1.name);
}
}
D. Interface
An interface is a reference type similar to a class that can contain only constants, method signatures, and nested types.
Example:
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
E. Enum
Enums are special classes that represent constants.
Example:
enum Day { MONDAY, TUESDAY, WEDNESDAY };
public class Example {
public static void main(String[] args) {
Day today = Day.MONDAY;
System.out.println(today);
}
}
Type Conversion and Casting
Java allows converting values from one type to another, either automatically (widening) or manually (narrowing).
Widening (Implicit Casting):
Converting smaller type to a larger type automatically.
int a = 10;
double b = a; // Implicit casting from int to double
Narrowing (Explicit Casting):
Converting larger type to a smaller type manually.
double x = 10.5;
int y = (int) x; // Explicit casting from double to int
Summary Table
Type | Category | Size | Example Syntax |
---|---|---|---|
int | Primitive | 4 bytes | int x = 100; |
double | Primitive | 8 bytes | double d = 3.14; |
boolean | Primitive | 1 bit | boolean flag = true; |
char | Primitive | 2 bytes | char c = 'A'; |
String | Non-Primitive | N/A | String name = "Java"; |
int[] | Non-Primitive | Varies | int[] arr = {1, 2, 3}; |
Class | Non-Primitive | User-defined | Student s = new Student(); |
Enum | Non-Primitive | Fixed Set | Day.MONDAY |
Final Notes for Students
- Always choose the right data type for your variable to save memory and improve performance.
- Use primitive types when dealing with simple values.
- Use non-primitive types when you need structure, group data, or deal with objects.
- Practice working with different types and conversions.
Mastering data types is essential before moving on to control statements, loops, functions, and object-oriented concepts.
Next Blog- Java Type Casting and Type Promotion