Every program needs a way to remember things: the price of a product, the number of items in a cart, whether an order has shipped. In Java, those pieces of remembered data live in variables, and every variable has a type that decides what kind of value it can hold. This lesson covers the basics of declaring variables, assigning values to them, and the broad shape of Java's type system.
A variable is a named slot in memory that holds a value. You give the slot a name, you tell Java what type of value goes in it, and from then on you can read from it or write to it by name.
Here's the smallest useful example:
stockCount is the variable. It has type int (an integer), and it currently holds the value 12. When we use the name stockCount later in the program, Java looks up whatever value is in that slot and uses it.
A variable has three things tied together:
stockCount or cartTotal.int or String.Once you've declared a variable's type, that type is locked in. You can change the value, but you can't change the type.
The basic syntax for a variable declaration is:
The first form declares a variable without giving it a value yet. The second form declares it and assigns it a value in the same line, which is called initialization.
The line int stockCount; just reserves the slot. The next line stockCount = 50; puts a value into it. The line double price = 29.99; does both at once.
You can declare multiple variables of the same type on one line, separated by commas:
That's a shortcut, not a different feature. It's the same as writing three separate lines. Most code uses one variable per line because it reads better and makes diffs cleaner when only one of them changes.
One distinction matters here.
int stockCount = 50;.stockCount = 42;.So initialization is one specific kind of assignment. The reason to keep them separate in your head is that Java treats uninitialized local variables strictly.
A local variable is a variable declared inside a method (like inside main). Java will not let you read a local variable before it has been assigned a value. The compiler refuses to compile code that does this.
What's wrong with this code?
The compiler rejects it with an error like:
Fix: Give the variable a value before you read it.
This rule applies only to local variables. Fields declared inside a class (but outside any method) get default values automatically: numeric types start at 0, boolean starts at false, and reference types start at null. For now, the takeaway is: inside a method, always assign a value before you read.
Java is a statically typed language. That means the type of every variable is decided at the moment you declare it, and the compiler checks every read and write against that type. You cannot change a variable's type after declaration, and you cannot store a value of one type in a variable of another type without an explicit conversion.
What's wrong with this code?
The compiler reports:
Fix: Either keep the variable an int and assign a number, or declare it as a String from the start:
The compiler catches type mistakes before your program ever runs. That trade-off (more rules up front, fewer surprises later) is part of why Java code tends to fail loudly at compile time rather than silently at run time.
Java's types come in two big buckets: primitive types and reference types.
The diagram shows the overall shape. Primitives hold a raw value directly, like a number or a single character. Reference types are objects, and variables of those types hold a reference to where the object lives in memory.
Java has exactly 8 primitive types.
| Type | Holds | Example literal |
|---|---|---|
byte | Whole numbers, very small range | byte tier = 1; |
short | Whole numbers, small range | short year = 2025; |
int | Whole numbers, common default | int stock = 500; |
long | Whole numbers, very large range | long views = 9_000_000_000L; |
float | Decimals, less precision | float rating = 4.5f; |
double | Decimals, common default | double price = 29.99; |
char | A single character | char grade = 'A'; |
boolean | true or false | boolean inStock = true; |
For now, just know there are 8 of them and what each one is for.
Anything that isn't a primitive is a reference type. The most common ones you'll meet early are:
String customerName = "Alex";int[] dailySales = new int[7];customerName is a reference to a String object. The other three are primitive values stored directly in the variable. From the outside, that distinction is invisible here, but it matters once you start passing variables to methods and comparing them.
varSince Java 10, you can let the compiler figure out the type of a local variable for you, using the keyword var:
var doesn't make Java dynamically typed. The compiler picks a type at the point of declaration and locks it in, just as if you'd written it out. var stockCount = 50; is identical to int stockCount = 50; in every way that matters.
var is convenient when the type on the right side is long or obvious.
A variable is only visible inside the block it was declared in. A block is anything between a pair of curly braces { }. Once you leave the block, the variable is gone.
cartItems is declared inside main, so it's visible everywhere in main. discount is declared inside the if block, so it's only visible inside that block. Trying to use discount after the closing brace of the if produces a compile error: the compiler doesn't know what discount is anymore.
This is a deliberately light look at scope. Methods, classes, and nested scopes all have their own rules.
finalSometimes you want a variable that gets assigned once and then never changes: a tax rate, a maximum cart size, a fixed shipping fee. Java lets you mark such a variable with the keyword final. Once a final variable has been assigned, any attempt to change it is a compile error.
If you later wrote TAX_RATE = 0.10;, the compiler would reject it with cannot assign a value to final variable TAX_RATE. By convention, constants like this are named in UPPER_SNAKE_CASE so they stand out from regular variables.
final does more than just make a local variable constant. It applies to fields, method parameters, classes, and methods, each with its own implications. For now, treat final as the way you mark a value that shouldn't change.
10 quizzes