Java Programming · Lesson 2 of 15 · Updated ·
Variables and Data Types in Java — Primitives, Casting and Wrappers
In Java you must declare the type of every variable. The compiler then checks that you use it correctly — that's why Java catches many mistakes before the program even runs.
public class Main {
public static void main(String[] args) {
int age = 21;
double cgpa = 8.75;
char grade = 'A';
boolean placed = true;
String name = "Asha"; // String is a class, not a primitive
System.out.println(name + " " + age + " " + cgpa + " " + grade + " " + placed);
}
}
Asha 21 8.75 A true
1. The 8 primitive types
- Whole numbers:
byte<short<int<long(addL:9000000000L) - Decimals:
float(addf:3.14f) <double charholds one character in single quotes:'A'booleanistrueorfalse
2. Type casting
Widening (small → big) happens automatically. Narrowing (big → small) needs a cast (type) and can lose data.
public class Main {
public static void main(String[] args) {
int marks = 85;
double d = marks; // widening: automatic
double price = 99.99;
int rupees = (int) price; // narrowing: decimal part is cut
char c = 'A';
int code = c; // char → its number
System.out.println(d + " " + rupees + " " + code + " " + (char) (code + 1));
}
}
85.0 99 65 B
3. Integer division and overflow
public class Main {
public static void main(String[] args) {
System.out.println(7 / 2);
System.out.println(7 / 2.0);
System.out.println(7 % 2);
int big = Integer.MAX_VALUE;
System.out.println(big);
System.out.println(big + 1); // overflow wraps around!
long safe = (long) big + 1;
System.out.println(safe);
}
}
3
3.5
1
2147483647
-2147483648
2147483648
Two classic traps:
int / intdrops the decimal, and anintsilently overflows past about 2.1 billion. Uselongfor big values (like sums of large arrays).
4. var (Java 10+)
var count = 10; // compiler infers int
var name = "Ravi"; // infers String
The type is still fixed — var just saves typing. It only works for local variables with an initial value.
5. Constants with final
final double GST_RATE = 0.18; // cannot be changed later
By convention, constants use UPPER_CASE names.
6. Wrapper classes and autoboxing
Each primitive has a class version: int → Integer, double → Double, char → Character, boolean → Boolean. Collections (like ArrayList) can only store objects, so Java converts automatically (autoboxing).
public class Main {
public static void main(String[] args) {
Integer x = 100; // autoboxing
int y = x + 5; // unboxing
int parsed = Integer.parseInt("250"); // text → int
String text = String.valueOf(42); // int → text
System.out.println(y + " " + parsed + " " + text.length());
Integer a = 1000, b = 1000;
System.out.println(a.equals(b));
}
}
105 250 2
true
Compare wrapper objects with
.equals(), not==(which compares references and can be false for values above 127).
Common mistakes
float f = 3.14;→ error; write3.14f(decimals aredoubleby default).- Expecting
5 / 2to be 2.5. - Ignoring overflow when multiplying or summing big ints.
Quick recap
- 8 primitives;
intanddoubleare the defaults you'll use most. - Widening is automatic; narrowing needs
(type)and may lose data. int / intis integer division; watch for overflow → uselong.- Wrappers (
Integer…) for collections;Integer.parseIntconverts text.