Intro to Java Variable Initialization
Learning objective: By the end of this lesson, students will be able to identify the syntax used to declare variables in Java allowing data to be easily referenced in an application.
Initializing variables
To start writing more complex apps, we’ll have to initialize variables at some point so that we’re able to hold and reference data in our programs.
Creating a variable in Java requires us to define the data type of that variable by providing the type before the name of the variable. Here’s the syntax:

- The data type that will be stored in the declared variable.
- The identifier (name) of the variable.
- The value to be assigned to the variable. The data type of this value must match the data type we provided earlier.
Here are some more examples:
double priceOfWater = 3.99;
String bestLanguage = "Java";
In Java, the standard practice is to initialize a value to a variable when it is declared if possible, but you are able to declare a variable without assigning anything to it and then initialize it later.
int iWillBeSomethingOneDay
iWillBeSomethingOneDay = 100;
When you do this, it may be initialized with a default value depending on its data type. We’ll learn more about all of Java’s different data types soon, but these are the ones we see above:
int: Short for integer; a whole number.double: A number with a decimal.String: Any combination of letters and words.Stringis an object data type, so it begins with a capital letter unlikeintanddouble.
Naming variables
Variable names are written in lower camel case, similar to how we wrote methods - each word is joined so there are no spaces or characters between them, and every word after the first word starts with a capital letter.
Variable names follow a few rules:
- They are case-sensitive.
- They cannot contain white space.
- They can be any length of letters, digits, dollar signs (
$), and underscores (_). - They should begin with a letter. Never start a variable with a
$or_even though those are technically allowed. Variables cannot start with numbers. - They cannot be reserved words.
Variable Declaration Practice
10 minWith a partner, take seven minutes to write these variables and print them to the console:
- How many minutes it takes to cook a pot of rice.
- The names of two famous landmarks and the countries where they are located.
- The price of a cup of coffee at a local cafe in your area.
We’ll have a few pairs demo what they did when they’re done.
🏗️ Under Construction
We are constantly working to improve our resources for instructors and students.
Have something to contribute to this Instructor Guide? Let us know.