< Previous | Contents | Next >

Initializing Variables

You can both declare and assign a value to variables in a single initialization statement. Thats exactly what I do next.

double engineTemp = 6572.89;

This line creates a variable of type double named engineTemp, which stores the value 6572.89.

Just as you can declare multiple variables in one statement, you can initialize more than one variable in a statement. You can even declare and initialize different variables in a single statement. Mix and match as you choose!

Hi n t

image

Although you can declare a variable without assigning it a value, it’s best to initialize a new variable with a starting value whenever you can. This makes your code clearer, plus it eliminates the chance of accessing an uninitialized variable, which may contain any value.

image