What Are Data Types in Java?
Data types define the kind of data a variable can hold — for example, a number, a character, or text.
Java’s Main Data Types
1️⃣ Primitive Data Types
Primitive data types are built-in, lightweight, and directly store their values in memory.
integer (integer number):
byte (8-bit; range -128 to 127)
byte b = 100;
short (16-bit; range -32,768 to 32,767)
short s = 1000;
int (32-bit; range -2,147,483,648 to 2,147,483,647)
int number = 50000;
long (64-bit; range much larger)
long large = 10000000000L;
floating point (Decimal number):
float (32-bit; less precision)
float f = 19.99f;
double (64-bit; greater precision)
double d = 19.99;
character:
char (16-bit Unicode character)
char grade = 'A';
boolean:
Stores true or false
boolean isJavaFun = true;
2️⃣ Reference (Non-Primitive) Data Types
Reference data types hold a reference to an object or array.
String:
String message = "Hello, Java!";
Array:
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob"};
Class and Object:
public class Person {
String name = "Alice";
void showName(){
System.out.println(this.name);
}
}
Person p = new Person();
p.showName();
Summary
- Primitive — Stores raw values directly (integer, float, boolean...).
- Reference — Stores a reference or pointer to an object (String, array, custom class...).
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.