Declaring Multiple Variables – Building Blocks – 1Z0-829 Study Guide

Declaring Multiple Variables

You can also declare and initialize multiple variables in the same statement. How many vari-ables do you think are declared and initialized in the following example?

void sandFence() {

String s1, s2;

String s3 = “yes”, s4 = “no”;

}

Four String variables were declared: s1, s2, s3, and s4. You can declare many variables in the same declaration as long as they are all of the same type. You can also initialize any or all of those values inline. In the previous example, we have two initialized variables: s3 and s4. The other two variables remain declared but not yet initialized.

This is where it gets tricky. Pay attention to tricky things! The exam will attempt to trick you. Again, how many variables do you think are declared and initialized in the fol-lowing code?

void paintFence() {

int i1, i2, i3 = 0;

}

As you should expect, three variables were declared: i1, i2, and i3. However, only one of those values was initialized: i3. The other two remain declared but not yet initialized. That’s the trick. Each snippet separated by a comma is a little declaration of its own. The initialization of i3 only applies to i3. It doesn’t have anything to do with i1 or i2 despite being in the same statement. As you will see in the next section, you can’t actually use i1 or i2 until they have been initialized.

Another way the exam could try to trick you is to show you code like this line:

int num, String value; // DOES NOT COMPILE

This code doesn’t compile because it tries to declare multiple variables of different types in the same statement. The shortcut to declare multiple variables in the same statement is legal only when they share a type.

Legal, valid, and compiles are all synonyms in the Java exam world. We try to use all the terminology you could encounter on the exam.

To make sure you understand this, see if you can figure out which of the following are legal declarations:

  • boolean b1, b2;
  • String s1 = “1”, s2;
  • double d1, double d2;
  • int i1; int i2;
  • int i3; i4;

Lines 4 and 5 are legal. They each declare two variables. Line 4 doesn’t initialize either variable, and line 5 initializes only one. Line 7 is also legal. Although int does appear twice, each one is in a separate statement. A semicolon (;) separates statements in Java. It just so happens there are two completely different statements on the same line.

Line 6 is not legal. Java does not allow you to declare two different types in the same statement. Wait a minute! Variables d1 and d2 are the same type. They are both of type double. Although that’s true, it still isn’t allowed. If you want to declare multiple variables in the same statement, they must share the same type declaration and not repeat it.

Line 8 is not legal. Again, we have two completely different statements on the same line. The second one on line 8 is not a valid declaration because it omits the type. When you see an oddly placed semicolon on the exam, pretend the code is on separate lines and think about whether the code compiles that way. In this case, the last two lines of code could be rewritten as follows:

int i1;

int i2;

int i3;

i4;

Looking at the last line on its own, you can easily see that the declaration is invalid. And yes, the exam really does cram multiple statements onto the same line—­partly to try to trick you and partly to fit more code on the screen. In the real world, please limit yourself to one declaration per statement and line. Your teammates will thank you for the readable code.