Using Integer in a Switch Statement

For various business reasons, I want to store some static identifiers in one of my classes. They were originally int , but I wanted to change them to Integer so that I could do equal to them (i.e. MY_ID.equals(..) , which avoids NPE)

When I change them to Integer, I get errors in my switch statement. docs say that Integer should be in order between the switches.

To quote

[Switch] also works with enumerated types (discussed in Enum types), the String class, and several special classes that wrap certain primitive types: character, byte, short, and integer (discussed in Numbers and Strings).

In my code below, if I am int then it compiles. When it is Integer , it does not say that a constant expression is required . I tried to do .intValue() , but this does not work either.

Am I really stupid? Or read docs completely incorrectly?

 private static final Integer i = 1; @Test public void test() { switch(mObj.getId()){ case i: //do something default: //do something default } } 

Thanks for any pointers here. For now, I save them as int and do new Integer(myint).equals(...)

+4
source share
6 answers

Change the constant to a primitive type:

 private static final int i = 1; 

and everything will be alright. switch can only work with primitives, enum values, and (since Java 7). A few tips:

  • new Integer(myint).equals(...) may be supernatural. If at least one of the variables is primitive, just do: myint == ... equals() is only required when comparing with Integer wrappers.

  • Prefer Integer.valueOf(myInt) instead of new Integer(myInt) - and rely on autoboxing whenever possible.

  • A constant is typically written using the capitalization case in Java, so static final int I = 1 .

+6
source

The switch requires constant expressions in case or enumeration constants. Constant expression:

expression denoting a primitive type value or String that does not terminate abruptly

So, integers do not meet the criteria. In your case, you can use int or enumeration (which would be reasonable if your identifiers are known at compile time).

The only place you can use the boxed type (e.g. Integer) is in the switch statement:

 switch(Integer.valueOf(1)) { case 1: // } 
+2
source

Now that java offers enums, we usually do it like this:

 public enum MyKey { i, j } ... switch(mObj.getId()){ case i: //do something default: //do something default } 
+1
source

You must use a constant value in the switch statement.

 switch(mObj.getId()){ case 5: //do something default: //do something default } 
0
source

In the code shown, “i” is a pointer to an object, not a constant expression.

0
source

because I looked at it ...

The accepted answer says that:

the switch can only work with primitives, enumeration values ​​and (since Java 7) strings

but

14.11 Switch Statement

describes the JavaSE7 documentation for the switch, which shows:

The type of the expression must be char, byte, short, int, Character, Byte, Short, Integer , String or the enumeration type (§8.9) or a compile-time error occurs.

I just wanted to clarify for future surfers.

0
source

Source: https://habr.com/ru/post/1436387/


All Articles