Variables
7 min · Java
Store values in named, typed boxes called variables.
Enemy 1 of 5
This code stores the text Ada in a String variable called name, then prints it. In Java you write the type first, then the name. Press Run to see it.
Why this works
A variable is a name that holds a value, so your code can refer to it later instead of retyping the value every time. In Java, every variable must have an explicit type, declared right where the variable is created. This is called static typing, and it's one of the biggest differences from languages like Python or JavaScript, where a variable's type is inferred and can even change later.
Text is stored as String (capital S: it's a full type name, not a lowercase keyword like int), whole numbers are int. You write the type, then the name, then an equals sign, then the value: String name = "Ada"; Once declared with a type, that variable can never hold a different kind of value: assigning name = 36; later would be a compile error, since 36 is an int, not a String.
Once a value is stored, you use the name anywhere instead of repeating the value, same idea as every other language, just with the type spelled out up front.