Initializing Variables

Initializing the Variable

After declaring the variable, it is time to assign a value to the variable. When you make an assignment to a variable, you update its value.  Use a single equal sign to set the value for the variable name.  This can be accomplished in two lines:

int age;
age = 45;

or in one line, which is much more convenient.

int age = 45;

This will set the value inside of age to 45.

double gpa = 3.5;

Again, this sets the value in gpa to be 3.5

The value that is set goes on the right side of the equal sign.

Reassigning a Variable

After a variable is declared and initialized the first time, it may be necessary to reassign a value into that variable. If you need to reassign a value in the variable, you do not need to declare it again.  Instead, you can just assign the new value to it by using the equal sign.

int age = 45;
//program does some other work but now the person is a year older
age = 46;