Data Types in Java
Introduction
A data type specifies what kind of data a variable can store. In Java, every variable must have a data type.
For example:
int age = 20; String name = "Anuradha";
Here:
intis the data type ofage.Stringis the data type ofname.
Java data types are mainly divided into two categories:
Data Types │ ├── Primitive Data Types │ └── Non-Primitive Data Types
1. Primitive Data Types
Primitive data types are the basic data types provided by Java.
Java has 8 primitive data types:
| Data Type | Size | Example | Description |
|---|---|---|---|
byte | 8-bit | byte x = 10; | Small integer |
short | 16-bit | short x = 1000; | Small integer |
int | 32-bit | int x = 10000; | Integer |
long | 64-bit | long x = 100000L; | Large integer |
float | 32-bit | float x = 10.5f; | Decimal number |
double | 64-bit | double x = 10.5; | More precise decimal |
char | 16-bit | char x = 'A'; | Single character |
boolean | JVM-dependent | boolean x = true; | True or false |
1.1 byte
The byte data type is used to store small integer values.
Syntax
byte age = 20;
Range
-128 to 127
Example
public class Main { public static void main(String[] args) { byte age = 20; System.out.println(age); } }
1.2 short
The short data type is used to store integer values larger than byte.
Syntax
short number = 1000;
Range
-32,768 to 32,767
Example
short population = 30000; System.out.println(population);
1.3 int
The int data type is commonly used for whole numbers.
Syntax
int age = 20;
Range
-2,147,483,648 to 2,147,483,647
Example
int marks = 85; System.out.println(marks);
1.4 long
The long data type is used for very large integer values.
A value normally uses L or l at the end.
Syntax
long population = 1000000000L;
Example
long distance = 9876543210L; System.out.println(distance);
2. Floating-Point Data Types
Java provides two data types for decimal numbers:
floatdouble
2.1 float
The float data type stores decimal numbers with single precision.
The value should normally have f or F at the end.
Syntax
float price = 500.50f;
Example
float temperature = 25.5f; System.out.println(temperature);
2.2 double
The double data type stores decimal numbers with greater precision than float.
Syntax
double salary = 25000.50;
Example
double percentage = 85.75; System.out.println(percentage);
double is generally preferred when more decimal precision is needed.
3. char
The char data type stores a single character.
A character is written using single quotation marks ' '.
Syntax
char grade = 'A';
Example
char gender = 'M'; System.out.println(gender);
Output:
M
A char stores one UTF-16 code unit.
4. boolean
The boolean data type stores one of two values:
true false
Example
boolean isPassed = true; System.out.println(isPassed);
Output:
true
Boolean values are commonly used in conditions:
int age = 20; if (age >= 18) { System.out.println("Adult"); }
5. Non-Primitive Data Types
Non-primitive data types are also called reference types. They can refer to objects.
Common examples include:
String- Arrays
- Classes
- Objects
- Interfaces
- Enums
5.1 String
String is used to store a sequence of characters.
Example
String name = "Anuradha"; System.out.println(name);
Output:
Anuradha
Unlike primitive types, String is a class.
5.2 Array
An array stores multiple values of the same type.
Example
int[] marks = {80, 75, 90, 85}; System.out.println(marks[0]);
Output:
80
Array indexing starts from 0.
5.3 Class
A class is a blueprint for creating objects.
class Student { String name; int age; }
5.4 Object
An object is an instance of a class.
Student s1 = new Student(); s1.name = "Anuradha"; s1.age = 20;
Here, s1 refers to a Student object.
Primitive vs Non-Primitive Data Types
| Primitive | Non-Primitive |
|---|---|
| Basic data types | Reference types |
| 8 types | Many types |
| Stores a value directly | Stores a reference to an object |
| Generally has fixed size | Size depends on the type/object |
Examples: int, char, boolean | Examples: String, arrays, classes |
Default Values
Instance and static variables have default values if they are not explicitly initialized.
| Data Type | Default Value |
|---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
boolean | false |
| Reference types | null |
Note: Local variables do not automatically receive default values. They must be initialized before use.
Example Using Different Data Types
public class Main { public static void main(String[] args) { byte age = 20; short marks = 500; int salary = 25000; long population = 3000000000L; float price = 500.50f; double percentage = 85.75; char grade = 'A'; boolean passed = true; String name = "Anuradha"; System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Marks: " + marks); System.out.println("Salary: " + salary); System.out.println("Population: " + population); System.out.println("Price: " + price); System.out.println("Percentage: " + percentage); System.out.println("Grade: " + grade); System.out.println("Passed: " + passed); } }
Type Casting
Type casting means converting one data type into another.
Widening Casting
Converting a smaller type into a larger compatible type.
int number = 100; double value = number; System.out.println(value);
int → long → float → double
Narrowing Casting
Converting a larger type into a smaller type requires explicit casting.
double number = 10.5; int value = (int) number; System.out.println(value);
Output:
10
The decimal portion is removed.
Summary
Java data types are divided into:
Java Data Types │ ├── Primitive │ ├── byte │ ├── short │ ├── int │ ├── long │ ├── float │ ├── double │ ├── char │ └── boolean │ └── Non-Primitive / Reference ├── String ├── Array ├── Class ├── Object └── Interface
Conclusion
Data types are an important part of Java because they define the kind of data a variable can store. Java provides eight primitive data types and many reference types such as String, arrays, classes, and interfaces. Understanding data types helps you write correct and efficient Java programs.