-
Hajipur, Bihar, 844101
Every programming language needs a way to store and manage different types of data. In Java, this is done through data types. Data types define what kind of data a variable can hold, such as numbers, characters, text, or true/false values. Understanding Java data types is essential because it helps you choose the right type of variable for your program and prevents type-related errors during execution.
A data type in Java specifies the size and type of values that can be stored in a variable. Java is a strongly typed language, meaning you must declare a variable’s type before using it. Once declared, the type of that variable cannot change during the program.
For example:
int age = 25;
double price = 99.99;
char grade = 'A';
Here, each variable is declared with a specific data type. int stores whole numbers, double stores decimal numbers, and char stores a single character.
Java data types are mainly divided into two categories:
Primitive Data Types
Non-Primitive Data Types (Reference Types)
Let’s look at both in detail.
Primitive data types are the most basic data types in Java. They are predefined by the language and store simple values directly in memory. There are eight primitive data types in Java, grouped into four categories: integer types, floating-point types, character type, and boolean type.
These types are used to store whole numbers (both positive and negative).
| Data Type | Size | Range | Example |
|---|---|---|---|
| byte | 1 byte | -128 to 127 | byte age = 20; |
| short | 2 bytes | -32,768 to 32,767 | short year = 2025; |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 | int population = 1000000; |
| long | 8 bytes | very large range | long distance = 9876543210L; |
The letter L at the end of a long value is used to indicate it is of type long.
These types store decimal or fractional numbers.
| Data Type | Size | Precision | Example |
|---|---|---|---|
| float | 4 bytes | 6–7 digits | float price = 19.99f; |
| double | 8 bytes | 15–16 digits | double average = 4567.12345; |
When using a float, you must add the letter f or F at the end to indicate it’s a float literal.
The char type is used to store a single character. It takes 2 bytes because it supports Unicode characters.
Example:
char grade = 'A';
char symbol = '#';
char letter = '\u0041'; // Unicode for 'A'
This allows Java to handle global languages and symbols.
The boolean data type stores only two possible values: true or false. It’s mainly used for logical conditions and decision-making statements.
Example:
boolean isPassed = true;
boolean isRaining = false;
Non-primitive data types (also known as reference types) are more complex than primitive ones. They store references (memory addresses) to objects rather than the actual data itself. These include:
Strings
Arrays
Classes
Interfaces
A String is a sequence of characters enclosed within double quotes. Although it looks like a primitive type, it’s actually a class in Java.
Example:
String name = "Aisha";
System.out.println("Welcome, " + name);
Strings can hold large amounts of text and come with many built-in methods for manipulation, like length(), toUpperCase(), and concat().
An array is a collection of similar data types stored together in memory.
Example:
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[2]); // Output: 30
Arrays can hold multiple values but all must be of the same type.
A class is a blueprint for creating objects. It can contain variables (fields) and methods.
Example:
class Student {
int rollNumber;
String name;
}
Objects of this class can then be created to store data.
An interface is similar to a class but only contains abstract methods. It defines behavior that other classes can implement.
| Feature | Primitive Data Type | Non-Primitive Data Type |
|---|---|---|
| Definition | Stores simple values directly in memory | Stores references to objects |
| Examples | int, char, double, boolean | String, Array, Class |
| Default Value | Depends on type (0, false, null) | null |
| Memory | Uses less memory | Requires more memory |
| Methods | No built-in methods | Has predefined methods |
Java allows you to convert data from one type to another. There are two kinds of conversions:
This happens automatically when converting a smaller data type to a larger one.
int num = 10;
double result = num; // int to double
This requires manual casting because data may be lost.
double value = 9.99;
int result = (int) value; // double to int
Here’s a quick summary of memory usage for each primitive type:
| Data Type | Size |
|---|---|
| byte | 1 byte |
| short | 2 bytes |
| int | 4 bytes |
| long | 8 bytes |
| float | 4 bytes |
| double | 8 bytes |
| char | 2 bytes |
| boolean | 1 bit (depends on JVM) |
Java data types define the kind of data a variable can store and how much memory it needs. There are two main types: primitive (simple values like numbers and characters) and non-primitive (objects like Strings and Arrays). Choosing the right data type helps ensure better memory management and code efficiency. Once you understand Java data types, you’ll be better prepared to work with type casting, operators, and data manipulation in upcoming tutorials.
Write a Java program that declares variables of all primitive data types (byte, short, int, long, float, double, char, and boolean) and prints their values to show how they are stored in memory.
Create a Java program to find the range and size of each primitive data type using the wrapper classes like Byte, Short, Integer, and Long. Display the minimum and maximum values for each.
Write a Java program that demonstrates implicit type conversion (widening conversion) by assigning an int value to a double variable and printing the result.
Develop a Java program that demonstrates explicit type conversion (narrowing conversion) by assigning a double value to an int variable using type casting and printing the converted value.
Write a program that stores a character value in a char variable, prints its ASCII value by converting it into an integer, and then prints the next character in sequence.
Create a Java program that uses a boolean variable to represent whether a number is positive or negative and prints an appropriate message based on its value.
Write a program that declares and initializes variables for a person’s name, age, height, and weight using proper data types and then prints them in a formatted output.
Develop a program that reads two numbers as float and double, performs division on them, and prints both results to show the difference in precision between float and double.
Create a Java program that takes a user’s marks as an integer and then converts it into a double to calculate the percentage. Display the percentage result on the console.
Write a Java program that uses mixed data types in arithmetic operations, such as adding an int and a double, and displays the result after implicit type conversion.