Variables, sometimes called properties, are a core concept of programming. Similar to variables in math, you use variables in programming to store values. This allows you to reference them later.
In Java, variables have a few different parts:
Below is a variable:
String myName = "Cody Winton";
String
myName
"Cody Winton"
Finally, the line of code is terminated with a semicolon: ;
.
Java is a strongly, statically typed language, meaning that every variable has a type at compile time and you can't a variable's type after it has been set, though you can change its value.
Java requires explicit typing for any variable. Let's see this in action as we define a few String variables:
String aString = "This is a String value";
String aVal = "Can be 123 or @ or # or any other characters!"; // Testing
aVal = "Testing val"; // The value of a variable can be changed
// This below would fail. 123 is not a string, since it is not surrounded by quotes
aVal = 123; // fails
// You can also set the value after creating the variable
String newVal;
newVal = "Testing new val";