Variables and Data Types
In Java, variables are containers that hold data. Data types specify what kind of data can be stored. Think of it like labeling boxes: one box for numbers, another for text, etc.
Primitive Data Types
byte– 8-bit integer (-128 to 127)short– 16-bit integer (-32,768 to 32,767)int– 32-bit integer (most common)long– 64-bit integer (use L suffix)float– 32-bit floating point (use f suffix)double– 64-bit floating point (default for decimals)char– single character (16-bit Unicode)boolean– true or false
Declaring Variables
int age = 25;
double price = 19.99;
char grade = 'A';
boolean isJavaFun = true;
String name = "Alice"; // String is a class, not primitive
Type Casting
Converting one data type to another:
double d = 9.78;
int i = (int) d; // explicit casting, i = 9
int a = 10;
double b = a; // implicit casting, b = 10.0
Two Minute Drill
- Java has 8 primitive data types: byte, short, int, long, float, double, char, boolean.
- Variables must be declared with a type before use.
- Type casting can be implicit (widening) or explicit (narrowing).
- String is a reference type (class) but commonly used like a primitive.
Need more clarification?
Drop us an email at career@quipoinfotech.com
