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:

int theAnswerToEverything = 42; int is labeled with 1, theAnswerToEverything is labeled with 2, and 42 is labeled with 3

  1. The data type that will be stored in the declared variable.
  2. The identifier (name) of the variable.
  3. 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:

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:

Variable Declaration Practice

10 min

With a partner, take seven minutes to write these variables and print them to the console:

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.