Data Types
A data type tells Java what kind of value a variable will store — whether it's a whole number, a decimal number, a single character, or true/false.
1. What are Data Types?
A data type tells Java what kind of value a variable will store — whether it's a whole number, a decimal number, a single character, or true/false. Java has two categories: primitive types (like int, double, char, boolean) and reference types (like String, arrays, and objects).
2. Why is it used?
Data types help Java know exactly how much memory to reserve and what operations are valid on a value. For example, you can perform mathematical operations on an int, but not on a String, because their data types behave differently.
3. Real-Life Example
Think of different containers in a kitchen — a jar for sugar, a bottle for oil, a box for spices. Each container is designed for one kind of item. Similarly, each data type is designed to correctly store one kind of value.
4. Syntax
javaint wholeNumber = 10; double decimalNumber = 10.5; char letter = 'A'; boolean isActive = true; String text = "Hello";
5. Example Program
javapublic class DataTypeDemo { public static void main(String[] args) { int marks = 90; double percentage = 90.5; char grade = 'A'; boolean passed = true; System.out.println(marks + " " + percentage + " " + grade + " " + passed); } }
Output:
90 90.5 A true6. Key Points to Remember
- 8 primitive types exist in Java:
byte,short,int,long,float,double,char,boolean. Stringis not a primitive type — it is a class (reference type).- Choosing the right data type avoids wasted memory and unexpected errors.