Java Variables
A variable is a named box that holds a value: a number, a piece of text, a true-or-false answer. Your program can read the value, change it, and use it in calculations. In Java every variable has a type that says what kind of value it can hold, and the type never changes.
Declaring a variable
You write the type, the name, and usually a first value:
public class Main {
public static void main(String[] args) {
String name = "Asha";
int age = 21;
double height = 1.62;
System.out.println(name);
System.out.println(age);
System.out.println(height);
}
}Asha 21 1.62
Stringholds text. Notice the capital S.intholds whole numbers like 21 or -5.doubleholds numbers with a decimal point like 1.62.
The Data Types topic lists all the types. These three cover most beginner programs.
Changing a value
The = sign means "store this value", not "is equal to". You can store a new value at any time, and the old one is gone:
public class Main {
public static void main(String[] args) {
int score = 10;
System.out.println("Score: " + score);
score = 15;
System.out.println("Score: " + score);
score = score + 5;
System.out.println("Score: " + score);
}
}Score: 10 Score: 15 Score: 20
score = score + 5 reads the current value (15), adds 5, and stores the result back in score. The + between text and a number joins them into one piece of text.
You can also declare a variable first and give it a value later, but Java will not let you read it before it has a value.
Several variables and constants
You can declare several variables of the same type in one line. A variable marked final can be given a value only once; it is a constant, and by convention its name is written in capital letters:
public class Main {
public static void main(String[] args) {
int x = 5, y = 7, z = 9;
final double PI = 3.14159;
System.out.println(x + y + z);
System.out.println(PI * 2);
}
}21 6.28318
If you try to write PI = 3; later, the program does not compile. Use final for values that should never change, like a tax rate or the number of days in a week.
var: let Java work out the type
Since Java 10 you can write var instead of the type when the value makes the type obvious. The variable still has a fixed type; Java only saves you from writing it:
public class Main {
public static void main(String[] args) {
var city = "Pune"; // a String
var population = 7_400_000; // an int
System.out.println(city + " has " + population + " people.");
}
}Pune has 7400000 people.
The underscores in 7_400_000 only make the number easier to read; Java ignores them.
Rules for names
- A name may contain letters, digits,
_and$, but it cannot start with a digit. - It cannot be a Java keyword such as
int,classorpublic. - Names are case-sensitive:
ageandAgeare two different variables. - Use clear names in camelCase:
totalPrice,studentCount,isReady.
Practice exercises
- Store your name, your age and your height in three variables and print them in one sentence.
- Make a variable
countwith the value 1, then add 1 to it three times, printing it after each change. - Declare a
final int DAYS_IN_WEEK = 7;and print how many days there are in 5 weeks.