Variables#
A variable is a named memory location. Java is statically typed: every variable has a fixed type, declared before use.
int age; // declaration
age = 21; // assignment
int marks = 95; // declaration + initialisation
Aditi is 21 years old CGPA 9.47, grade A, placed: true
+ with a String joins (concatenates) values into text.
The 8 primitive types#
| Type | Size | Range / precision | Example |
|---|---|---|---|
byte |
1 byte | -128 to 127 | byte b = 100; |
short |
2 bytes | -32,768 to 32,767 | short s = 3000; |
int |
4 bytes | about ±2.1 billion | int n = 150000; |
long |
8 bytes | about ±9.2 × 10¹⁸ | long pop = 1_400_000_000L; |
float |
4 bytes | ~7 decimal digits | float f = 3.14f; |
double |
8 bytes | ~15 decimal digits | double d = 3.14159; |
char |
2 bytes | one Unicode character | char c = 'अ'; |
boolean |
- | true or false |
boolean ok = false; |
Unlike C, these sizes are the same on every platform - part of Java's portability.
int max: 2147483647 long max: 9223372036854775807 byte range: -128 to 127 India: 1428627663 Unicode char code: 2309 Overflow wraps around: -2147483648
Integer overflow does not give an error in Java - the value silently wraps around. Use long when numbers can exceed about 2 billion (money in paise, populations, factorials).
Literals and var#
longliterals end withL,floatliterals withf:5000000000L,2.5f.- Decimal literals are
doubleby default. - Since Java 10,
varlets the compiler infer the type of a local variable:
var count = 10; // int
var price = 99.5; // double
var city = "Indore"; // String
The type is still fixed - var only saves typing.
Constants: final#
Area: 153.93791 Seats: 60
By convention, constant names are written in UPPER_SNAKE_CASE.
Type casting#
Widening (small type → bigger type) happens automatically because no data is lost:
byte → short → int → long → float → double.
Narrowing (big → small) needs an explicit cast and may lose data:
87.0 vs 87.4 Rupees: 99 (byte) 300 = 44 65 B
छोटे type से बड़े type में बदलना (int → double) अपने आप हो जाता है - इसे widening कहते हैं। बड़े से छोटे में (double → int) बदलने के लिए (int) लिखकर cast करना पड़ता है, और दशमलव वाला हिस्सा कट जाता है। याद रखें: 437 / 5 पहले int में भाग होता है (87), फिर double बनता है।
Wrapper classes#
Each primitive has an object version (a wrapper class) with useful methods. Collections like ArrayList can only hold objects, so Java converts automatically (autoboxing):
| Primitive | Wrapper |
|---|---|
int |
Integer |
double |
Double |
char |
Character |
boolean |
Boolean |
long |
Long |
2027 91.5 2027! 1010 ff true Q 42
Primitive vs reference types#
Primitives (int, double...) store the value itself. Everything else - String, arrays, objects - is a reference type: the variable stores a reference (address) to an object on the heap. A reference can be null (pointing to nothing). You will see why this matters when comparing Strings.
Summary#
- Java has 8 primitives with fixed sizes on every platform; everything else is a reference type.
- Use
Lfor long andffor float literals;varinfers local types. finalmakes constants; widening is automatic, narrowing needs(type).- Wrapper classes (
Integer,Double...) convert between text and numbers.