📦
Basics

Variables & Data Types

Storing Data
💡 A variable is a labeled box you put a value in — the box's name and what (type) can go in it are decided upfront.

In Java, every variable has a type — like int (whole number), double (decimal number), char (single letter), boolean (true/false), and String (text).

Java is a "statically typed" language — meaning you have to declare the box's type upfront, and only that type of thing will fit in that box.

int age = 10;
double price = 99.5;
char grade = 'A';
boolean isFun = true;
String name = "Duke";
📦
A variable is a labeled box you put a value in — the box's name and what (type) can go in it are decided upfront.
1 / 7
⚡ Quick Recap
  • Variable = a labeled box for data
  • Types: int, double, char, boolean, String
  • Java is statically typed
On this page (3 subtopics)

Java has 8 primitive types, which can be split into two groups: numbers (byte, short, int, long, float, double), text (char), and logic (boolean). Every type has its own fixed size (bits/bytes), which decides how big a number it can hold.

For whole numbers there are 4 types — from small to large: byte, short, int, long. int is the default choice in most programs (long for very large numbers). For decimals there's float and double — double is more precise, and decimal literals (like 3.14) are treated as double by default in Java, which is why you need the 'f' suffix to use float.

TypeSizeRangeDefaultExample
byte1 byte-128 to 1270byte b = 100;
short2 bytes-32,768 to 32,7670short s = 2000;
int4 bytes~-2.1B to 2.1B0int i = 50000;
long8 bytesVery large0Llong l = 100L;
float4 bytes~7 decimal digits precision0.0ffloat f = 3.14f;
double8 bytes~15 decimal digits precision0.0double d = 3.14;
char2 bytes0 to 65,535 (Unicode)'\u0000'char c = 'A';
boolean1 bit (JVM-dependent)true / falsefalseboolean b = true;
💡Tip: Unless there's a specific reason, use int for whole numbers and double for decimals — these are Java's "default" choices and fit best in most situations.

A local variable is defined inside a method/block — it's "alive" only there (once the method ends, so does the variable). It must be initialized before use, or you'll get a compile error — Java gives local variables no default value (deliberately, so uninitialized data is never used by mistake).

An instance variable is defined inside a class, outside any method — every object gets its own separate copy. It's stored in "heap" memory along with the object, and automatically gets a default value (0, null, false) if you don't explicitly initialize it.

A static variable is tied to the class, not to any individual object — all objects share a single copy. It's created the moment the program starts (when the class loads), and lasts for the whole program.

class Counter {
  int id;              // instance variable — har object ki apni
  static int total;    // static variable — sabme shared
  void increment() {
    int temp = 1;       // local variable — sirf is method mein
    total += temp;
  }
}
⚠️Common Mistake: Never try to use a local variable without initializing it — you'll get a "variable might not have been initialized" compile error. This is a Java safety feature, not a bug!

Java uses camelCase for variables/methods (studentName, calculateTotal), and PascalCase for class names (StudentRecord, BankAccount). Constants (static final variables) are written in SCREAMING_SNAKE_CASE (MAX_SPEED, PI_VALUE) — these conventions aren't official in the Java Language Specification, but the whole industry follows them, so breaking them makes code look "unprofessional".

For numeric literals, Java 7+ lets you use underscores for readability — 1_000_000 is much easier to read than 1000000. Hexadecimal starts with 0x (0x1A = 26), octal with just 0 (010 = 8 in decimal), and binary with 0b (0b1010 = 10).

Variable names can contain letters, digits, underscore (_), and dollar sign ($), but can't start with a digit, and can't use Java's reserved keywords (class, int, if, etc.).

int million = 1_000_000;
int hex = 0x1A;      // 26
int binary = 0b1010; // 10
final double MAX_SPEED = 120.0;