Variables & Data Types in Java
A variable in Java is a named box that holds a value of a declared type. Java is statically typed: every variable has a type fixed at compile time, and the compiler refuses code that puts the wrong kind of value in the wrong box. Java offers eight primitive types for raw values (numbers, characters, booleans) and reference types for everything else (objects, arrays, String). Since JDK 10 you can also use var to let the compiler infer the type for local variables.
Declaring a variable
int age = 19; // declare 'age' of type int, assign 19
double price = 249.50; // a decimal number
boolean enrolled = true;
String name = "Asha"; // String is a reference type, not a primitive
The pattern is type name = value;. You can declare first and assign later, but you must assign a value before you read it.
The eight primitive types
| Type | Size | Holds | Example |
|---|---|---|---|
byte | 8-bit | small integers | byte b = 100; |
short | 16-bit | integers | short s = 30000; |
int | 32-bit | whole numbers (default) | int n = 42; |
long | 64-bit | large whole numbers | long big = 9_000_000_000L; |
float | 32-bit | decimals (less precise) | float f = 3.14f; |
double | 64-bit | decimals (default) | double d = 3.14159; |
char | 16-bit | a single Unicode character | char grade = 'A'; |
boolean | — | true or false | boolean ok = false; |
// Commented examples of each primitive.
long population = 9_000_000_000L; // 'L' suffix needed: too big for int; underscores aid reading
float ratio = 0.75f; // 'f' suffix marks a float literal
double pi = 3.141592653589793; // double is the default for decimals
char letter = 'J'; // single quotes for a single char
boolean passed = (population > 0); // a comparison yields a boolean
Use int for whole numbers and double for decimals unless you have a specific reason not to. long and float exist for special cases; byte and short are rarely needed in everyday code.
Reference types
Anything that is not one of the eight primitives is a reference type: String, arrays, and every class you or the library defines. A reference variable holds a reference (a handle) to an object stored elsewhere in memory, not the object itself.
String city = "Jalgaon"; // 'city' refers to a String object
int[] marks = {88, 92, 75}; // an array is a reference type too
A reference can be null, meaning "points at nothing". Primitives can never be null.
Literals
A literal is a value written directly in code: 42, 3.14, 'A', true, "hello". Helpful touches:
int million = 1_000_000; // underscores group digits for readability
int hex = 0xFF; // hexadecimal literal = 255
int binary = 0b1010; // binary literal = 10
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesType inference with var
For local variables (inside a method), var lets the compiler infer the type from the initializer. The variable is still strongly typed — var is not "any type".
var count = 10; // inferred as int
var label = "Batch A"; // inferred as String
var prices = new double[]{99.0, 149.0}; // inferred as double[]
// var x; // ERROR: no initializer, nothing to infer
Use var when the type is obvious from the right-hand side. Spell out the type when it improves clarity.
Common mistakes
- Reading an uninitialized local variable. Java will not compile code that uses a local before it is assigned.
- Integer division surprises.
5 / 2is2, not2.5, because both operands areint. Write5.0 / 2to get2.5. - Forgetting the
Lorfsuffix.9_000_000_000withoutLoverflowsintand fails to compile;3.14is adouble, so assigning it to afloatwithoutfis an error. - Confusing
charandStringquotes.'A'is achar;"A"is aString. They are not interchangeable. - Treating
varas dynamic typing. Once inferred, the type is fixed; you cannot later assign aStringto avarthat started as anint.
FAQ
Is String a primitive? No. It is a class (a reference type) that gets special syntax support, which is why it can feel primitive.
When should I use long instead of int? When values can exceed about 2.1 billion, such as timestamps in milliseconds or large counters.
Keep going
Next, see how to combine values with Operators in Java, or revisit installing the JDK. All tutorials live on the Java hub; for guided practice in Jalgaon, see the Java course.
Want structured Java lessons and mentorship at Infoplanet, Jalgaon? Join the waitlist for the Java course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in Java
Store many values of one type with Java arrays: declaring, initializing, indexing, iterating, multidimensional arrays, and the Arrays helper class.
Classes & Objects in Java
Define classes with fields and methods, create objects with new, use the this keyword, and understand instance vs static members in Java.
The Java Collections Framework
Use the Java Collections Framework — List, Set, and Map with ArrayList and HashMap — plus generics and iteration, to store flexible groups of objects.
