Java Data Types Tutorial (Complete Beginner Guide)
Introduction
Every Java program works with data. Whether you're storing a person's age, a student's name, a product price, or a list of items, Java needs to know what type of data is being stored. This is where data types come in.
A data type tells the Java compiler:
- What kind of value a variable can store.
- How much memory should be allocated.
- What operations can be performed on the value.
Java is a strongly typed language, which means every variable must have a declared data type before it is used.
In this tutorial, you'll learn about Java's two main categories of data types:
- Primitive Data Types
- Reference Data Types
By the end of this guide, you'll understand when to use each data type and how they are stored in memory.
Table of Contents
- What are Data Types?
- Primitive Data Types
- byte
- short
- int
- long
- float
- double
- char
- boolean
- Reference Data Types
- String
- Array
- Class
- Interface
- Primitive vs Reference Types
- Memory Representation
- Best Practices
- Practice Programs
- Summary
What are Data Types?
A data type specifies the type of value that a variable can hold.
Example
1int age = 22; 2 3double salary = 55000.75; 4 5char grade = 'A'; 6 7boolean isPassed = true;
Each variable stores a different kind of data.
Categories of Java Data Types
Java data types are divided into two categories.
1Java Data Types 2│ 3├── Primitive Types 4│ ├── byte 5│ ├── short 6│ ├── int 7│ ├── long 8│ ├── float 9│ ├── double 10│ ├── char 11│ └── boolean 12│ 13└── Reference Types 14 ├── String 15 ├── Array 16 ├── Class 17 └── Interface
Primitive Data Types
Primitive data types are predefined by Java.
They store actual values directly in memory.
Java has 8 primitive data types.
| Data Type | Size | Default Value | Example |
|---|---|---|---|
| byte | 1 byte | 0 | 100 |
| short | 2 bytes | 0 | 20000 |
| int | 4 bytes | 0 | 500000 |
| long | 8 bytes | 0L | 9000000000L |
| float | 4 bytes | 0.0f | 3.14f |
| double | 8 bytes | 0.0d | 3.141592 |
| char | 2 bytes | '\u0000' | 'A' |
| boolean | JVM-dependent | false | true |
byte
The byte data type stores very small integer values.
Size
- 1 byte (8 bits)
Range
- -128 to 127
Example
1byte age = 25; 2 3System.out.println(age);
Output
125
Use Cases
- Binary data
- File processing
- Memory optimization
short
The short data type stores larger integers than byte.
Size
- 2 bytes
Range
- -32,768 to 32,767
Example
1short population = 25000; 2 3System.out.println(population);
int
The int data type is the most commonly used integer type.
Size
- 4 bytes
Range
- -2,147,483,648 to 2,147,483,647
Example
1int salary = 50000; 2 3System.out.println(salary);
Use int for most integer calculations.
long
Use long when integer values exceed the range of int.
Size
- 8 bytes
Example
1long distance = 9876543210L; 2 3System.out.println(distance);
Note: Always add
Lorlafter a long literal.
float
The float data type stores decimal numbers.
Size
- 4 bytes
Precision
- Approximately 6–7 decimal digits
Example
1float temperature = 36.5f; 2 3System.out.println(temperature);
Note: Always add
forFafter a float literal.
double
The double data type stores decimal values with higher precision than float.
Size
- 8 bytes
Precision
- Approximately 15–16 decimal digits
Example
1double pi = 3.141592653589793; 2 3System.out.println(pi);
double is the preferred choice for decimal calculations.
char
The char data type stores a single Unicode character.
Size
- 2 bytes
Example
1char grade = 'A'; 2 3System.out.println(grade);
Output
1A
Characters are enclosed in single quotes.
boolean
The boolean data type stores logical values.
Possible values:
truefalse
Example
1boolean isJavaEasy = true; 2 3System.out.println(isJavaEasy);
Output
1true
Booleans are commonly used in conditional statements and loops.
Reference Data Types
Reference data types store the memory address (reference) of an object rather than the object itself.
Common reference types include:
- String
- Array
- Class
- Interface
String
A String represents a sequence of characters.
Example
1String name = "Ankit Kushwaha"; 2 3System.out.println(name);
Output
1Ankit Kushwaha
Unlike char, a String can contain multiple characters.
Array
An array stores multiple values of the same data type.
Example
1int[] marks = {85, 90, 78, 95}; 2 3System.out.println(marks[0]);
Output
185
Arrays are useful for storing collections of related data.
Class
A class is a blueprint for creating objects.
Example
1class Student { 2 3 String name; 4 5}
Creating an object:
1Student student = new Student(); 2 3student.name = "Rahul";
Interface
An interface defines a contract that classes can implement.
Example
1interface Animal { 2 3 void sound(); 4 5}
Implementation:
1class Dog implements Animal { 2 3 public void sound() { 4 System.out.println("Bark"); 5 } 6 7}
Interfaces support abstraction and multiple inheritance of type.
Primitive vs Reference Types
| Feature | Primitive | Reference |
|---|---|---|
| Stores | Actual value | Memory reference |
| Memory | Stack (typically) | Reference on stack, object on heap |
| Fixed Size | Yes | No |
Can be null | No | Yes |
| Examples | int, double, char | String, Array, Class |
Memory Representation
1Primitive Variable 2 3int age = 22; 4 5Stack 6+------+ 7| age | 8| 22 | 9+------+
1Reference Variable 2 3String name = "Java"; 4 5Stack Heap 6+-------+ +---------+ 7| name | -----> | "Java" | 8+-------+ +---------+
Primitive variables store values directly, while reference variables point to objects in memory.
Best Practices
- Use
intfor most integer values. - Use
doublefor decimal numbers unless memory constraints requirefloat. - Use
booleanfor logical conditions. - Use
charfor single characters andStringfor text. - Choose the smallest suitable numeric type when memory usage is important.
- Use meaningful variable names.
Example:
1int studentAge = 20; 2 3double accountBalance = 10500.75; 4 5String studentName = "Rahul";
Practice Program 1: Temperature Converter
Convert Celsius to Fahrenheit.
Formula
Fahrenheit = (Celsius × 9 / 5) + 32
Program
1public class TemperatureConverter { 2 3 public static void main(String[] args) { 4 5 double celsius = 30.0; 6 7 double fahrenheit = (celsius * 9 / 5) + 32; 8 9 System.out.println("Celsius : " + celsius); 10 System.out.println("Fahrenheit : " + fahrenheit); 11 12 } 13 14}
Output
1Celsius : 30.0 2Fahrenheit : 86.0
Practice Program 2: Age Calculator
1public class AgeCalculator { 2 3 public static void main(String[] args) { 4 5 int birthYear = 2003; 6 int currentYear = 2026; 7 8 int age = currentYear - birthYear; 9 10 System.out.println("Birth Year : " + birthYear); 11 System.out.println("Current Year : " + currentYear); 12 System.out.println("Age : " + age); 13 14 } 15 16}
Output
1Birth Year : 2003 2Current Year : 2026 3Age : 23
Summary
In this tutorial, you learned:
- What data types are and why they are important
- The eight primitive data types in Java
- How to use
byte,short,int,long,float,double,char, andboolean - The purpose of reference data types such as
String, arrays, classes, and interfaces - The differences between primitive and reference types
- How Java stores primitive values and object references in memory
- Best practices for selecting the appropriate data type
Understanding Java data types is essential because every variable, method parameter, and return value depends on choosing the correct type. This knowledge forms the foundation for learning operators, expressions, control statements, methods, and object-oriented programming.
Practice Exercises
Exercise 1: Student Information
Create variables to store:
- Student name (
String) - Age (
int) - Grade (
char) - Percentage (
double) - Passed (
boolean)
Print all the values.
Exercise 2: Currency Calculator
Create a program that stores:
- Product price
- Quantity
Calculate and display the total amount.
Exercise 3: Circle Calculator
Use a double variable for the radius and calculate:
- Diameter
- Circumference
- Area
Exercise 4: Employee Record
Create a class Employee with:
- Employee ID (
int) - Name (
String) - Salary (
double)
Create an object and print its details.
Exercise 5: Array Practice
Create an integer array with five numbers and print:
- First element
- Last element
- Total number of elements
These exercises will help reinforce your understanding of Java primitive and reference data types before moving on to Operators and Expressions in the next module.