Java Data Types

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 TypeSizeDescription
byte1 byteStores integers from -128 to 127.
short2 bytesStores integers from -32,768 to 32,767.
int4 bytesStores integers from -2^31 to 2^31-1.
long8 bytesStores integers from -2^63 to 2^63-1.
float4 bytesStores fractional numbers (7 decimal digits).
double8 bytesStores fractional numbers (15 decimal digits).
char2 bytesStores a single character using Unicode.
boolean1 bitStores 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
  1. String: A sequence of characters.
  2. Array: A collection of elements of the same type.
  3. Class: A blueprint for creating objects.
  4. 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

FeaturePrimitive Data TypesNon-Primitive Data Types
PredefinedYesNo (created by the programmer).
Memory UsageDirectly stores values.Stores references to memory.
OperationsBasic 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 TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

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

Leave a Comment