Wednesday, December 7, 2022

Quiz yourself: Acceptable and unacceptable types for Java switch statements


Core Java, Oracle Java, Java Exam Prep, Java Certification, Java Tutorial and Materials, Java Quiz, Java Guides

Which switch expressions will compile successfully? Choose two.

A. var s = 1L;
switch (s) {
  case 1: {}
  default: {}
}

b. var s = 0;
switch (s>0) {
  case true: {}
  case false : {}
}

C. var s = Integer.MAX_VALUE;
switch (s+1) {
  case 0: case 1: {}; break;
  case 2: case 3, 4: {}
  case Integer.MAX_VALUE, Integer.MIN_VALUE : {}
}

D. var s = 's';
final var a = 0;
switch (s) {
  default: {break;}
  case a: {}
  case 'a': {}
}

Answer. A switch statement works with the following primitive types and their wrappers:

☉ byte
☉ short
☉ char
☉ int

In addition, you are allowed to switch on an enum or String (since Java 5). However, Boolean, long, float, and double types are prohibited. Given that a local variable declared using var takes its type from the right side of the assignment, option A attempts to switch on a long value. This is not permitted, and option A is incorrect.

As a side note, there’s a preview feature in Java 17 and Java 18 that expands the syntax, behavior, and acceptable argument types for switch significantly. Notably, you will be permitted to switch on arbitrary object types; however, at the time of writing, even though it’s permitted to switch on a wrapper such as a Double or Boolean, it remains prohibited to switch on the corresponding primitive types, and autoboxing happens. (Of course, this describes a preview feature that’s not relevant for the Java 17 exam, and it’s possible the details will change before the final release.)

In view of the previous discussion, option B—which attempts to switch on an expression of the Boolean primitive type—is also incorrect.

In option C, the switch type is presented as an Integer. This is acceptable; autounboxing will result in it being treated as an int. The arithmetic operation that adds one to the max value (2,147,483,647) will cause an overflow to the most negative value (-2,147,483,648), which is Integer.MIN_VALUE. However, that overflow does not throw an exception. Also, the question asks only if the code will compile, which it will, not if the programming logic is sound. From this, you can see option C is correct.

Option D is tricky, and two aspects demand attention.

First, one of the case expressions is a variable (specifically the int variable named a). If you ignore the preview features, Java requires that the case keyword must be followed by a constant expression, and most variables are not acceptable. However, in this example, closer inspection shows that a is, in fact, a constant expression because it’s declared as final. So, the code is acceptable from this perspective.

The second point is that you have executed the switch statement using an expression of char type, but the case expression for case a is an int. However, this is not a problem because the value of the expression a fits in the range of the char type (which is from 0 to 65,535) and the type of a is not long, float, or double—any of which would cause failure, even if they were constant expressions with a value in the acceptable range. Therefore, in this case, the expression is acceptable and option D compiles without errors. Therefore, option D is correct.

Conclusion. The correct answers are options C and D.

Source: oracle.com

Related Posts

0 comments:

Post a Comment