What are Data Types in Java?
In Java, data types specify the kind of data a variable can hold. They ensure the efficient use of memory and help developers identify the type of operations that can be performed on the data. Java is a statically typed language, meaning variables must be declared with a specific data type before use.
Types of Data Types in Java
Java data types are categorized into Primitive Data Types and Non-Primitive Data Types.
1. Primitive Data Types
Primitive data types are the most basic types of data. They are predefined by Java and are directly supported by the language. Java provides eight primitive data types:
Data Type | Size | Description |
---|---|---|
byte | 1 byte | Stores integers from -128 to 127. |
short | 2 bytes | Stores integers from -32,768 to 32,767. |
int | 4 bytes | Stores integers from -2^31 to 2^31-1. |
long | 8 bytes | Stores integers from -2^63 to 2^63-1. |
float | 4 bytes | Stores fractional numbers (7 decimal digits). |
double | 8 bytes | Stores fractional numbers (15 decimal digits). |
char | 2 bytes | Stores a single character using Unicode. |
boolean | 1 bit | Stores true or false. |
Examples of Primitive Data Types
Example 1: Using int
and double
:
public class PrimitiveExample {
public static void main(String[] args) {
int age = 25; // Integer data type
double price = 99.99; // Double data type
System.out.println("Age: " + age);
System.out.println("Price: " + price);
}
}
Output:
Age: 25
Price: 99.99
Example 2: Using boolean and char:
public class BooleanCharExample {
public static void main(String[] args) {
boolean isJavaFun = true; // Boolean data type
char grade = 'A'; // Character data type
System.out.println("Is Java Fun? " + isJavaFun);
System.out.println("Grade: " + grade);
}
}
Output:
Is Java Fun? true
Grade: A
2. Non-Primitive Data Types
Non-primitive data types are more complex and include classes, arrays and interfaces. Unlike primitive types, these are reference types, meaning they refer to memory locations.
Examples of Non-Primitive Data Types
- String: A sequence of characters.
- Array: A collection of elements of the same type.
- Class: A blueprint for creating objects.
- Interface: A collection of abstract methods.
Example: Using a String and an Array:
public class NonPrimitiveExample {
public static void main(String[] args) {
String message = "Hello, Java!"; // String data type
int[] numbers = {10, 20, 30, 40}; // Array data type
System.out.println("Message: " + message);
System.out.println("Numbers: ");
for (int num : numbers) {
System.out.println(num);
}
}
}
Output:
Message: Hello, Java!
Numbers:
10
20
30
40
Differences Between Primitive and Non-Primitive Data Types
Feature | Primitive Data Types | Non-Primitive Data Types |
---|---|---|
Predefined | Yes | No (created by the programmer). |
Memory Usage | Directly stores values. | Stores references to memory. |
Operations | Basic operations like addition, etc. | More complex operations available. |
Type Casting in Java
Type casting is converting one data type to another. There are two types of type casting:
Implicit Casting (Widening): Automatically converts a smaller data type to a larger one.
Example:
int num = 100;
double result = num; // Implicit casting
System.out.println("Result: " + result);
Explicit Casting (Narrowing): Manually converts a larger data type to a smaller one.
Example:
double price = 99.99;
int discountedPrice = (int) price; // Explicit casting
System.out.println("Discounted Price: " + discountedPrice);
Output:
Result: 100.0
Discounted Price: 99
Wrapper Classes
Primitive data types can be converted to objects using wrapper classes. These are part of the java.lang package.
Primitive Type | Wrapper Class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Example:
public class WrapperExample {
public static void main(String[] args) {
int num = 10;
Integer wrappedNum = Integer.valueOf(num); // Wrapping
System.out.println("Wrapped Integer: " + wrappedNum);
}
}
Output:
Wrapped Integer: 10