Java Data Types
Choose a type that fits the value.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Data Types.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
Types define which values a variable can hold and which operations make sense. Use int for ordinary whole numbers, double for many decimal calculations, boolean for true/false decisions, and String for text. These examples are a starting point, not a replacement for considering precision and range in a real application.
Explore the details
Java Data Types
Every variable in Java must have a data type — it tells Java what kind of value the variable can hold and how much memory to reserve.
Data types are divided into two groups:
- Primitive data types — the 8 built-in types:
byte,short,int,long,float,double,booleanandchar - Non-primitive data types — such as
String, Arrays, and Classes (you will learn these in later chapters)
Primitive Data Types
A primitive type stores a simple value directly in memory. There are exactly 8 of them in Java. Click each one below to explore its size, range, and usage:
Declaring Primitive Variables
Here is how you declare variables for each of the common primitive types:
Primitive vs Non-Primitive
- Primitive types always have a value — they can never be
null - Non-primitive types (like
String) can benull— meaning they reference no object - Primitives store values directly; non-primitives store a reference (memory address) to an object
- Non-primitive types have built-in methods you can call (e.g.
"hello".length())
Types Are Fixed
Once a variable is declared with a type, it cannot change to another type. Java is a statically typed language — the type is checked at compile time, not at runtime.
If you need to convert a value from one type to another, use type casting — you will learn about this in the next chapter.
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
double | The data type. Tells Java this variable holds a decimal number with high precision. |
price | The variable name. Choose a meaningful name that describes what the value represents. |
= | The assignment operator. Assigns the value on the right to the variable on the left. |
19.99 | The literal value being stored. Because the type is double, decimals are allowed. |
; | Every Java statement ends with a semicolon. |
byte small = 127; // -128 to 127
short medium = 32767; // -32,768 to 32,767
int num = 2_000_000; // underscores improve readability
long big = 9_876_543_210L; // append L for long
float price = 9.99f; // append f for float
double pi = 3.14159265358979; // 15-16 decimal digits
boolean flag = true;
char ch = 'J';
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Double.MIN_VALUE); // 4.9E-324
System.out.println(Character.isLetter(ch)); // trueA complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int seats = 24;
double temperature = 28.5;
boolean open = true;
char section = 'A';
String place = "Ariyalur";
System.out.println(seats + " | " + temperature + " | " + open);
System.out.println(section + " | " + place);
}
}Predict first. Then reveal the expected output / வெளியீடு
24 | 28.5 | true A | Ariyalur
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Add a long variable called views with value 3000000000L, then print it.
Reveal the answer and reasoning / விடை
Use long views = 3000000000L; and System.out.println(views);. The L suffix makes this literal a long.
One mistake to avoid: char uses single quotes for one UTF-16 code unit; String uses double quotes for text. Integer overflow is not automatically reported as a compilation error.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.