Why can a switch statement in Java contain a FINAL variable like CASE?

Why can a switch statement in Java contain a FINAL variable like CASE? ##

In JDK7, as I noted, a value cannot be reassigned to a final variable, as shown below. But why the final variable "x" can be included in the switch statement for the eventhough case, the value for the final variable "x" cannot be reassigned?

Why can this be done anyway if Oracle determines that the Java compiler accepts the final variable as an initialized value but not the variable name? http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4

Please tell me if this is a technical error of the Java compiler or is there an exception or special use of checking the case of a finite variable in the output of Switch?

class Example{
    public static void main(String args[]){
        final int x=100;
        //x=200;    //error: cannot assign a value to final variable x

        //The below piece of code compiles
        switch(x){
            case 200: System.out.println("200");
            case 300: System.out.println("300");
        }

    }
}
+5
source share
7 answers

How about this situation?

public class Foo
{
    private final int x;

    public Foo(int x){this.x = x;}

    public void boo()
    {
        switch(x)
        {
            case 200: System.out.println("200");
            case 300: System.out.println("300");
        }
    }
}

or maybe this one:

public static void doSomething(final int x)
{
    switch(x)
    {
        case 200: System.out.println("200");
        case 300: System.out.println("300");
    }
}
+4
source
switch(x){
    case 200: System.out.println("200"); break;
    case 300: System.out.println("300");
}

essentially means

if (x == 200)
  System.out.println("200");
else if (x == 300)
  System.out.println("300");

It simply compares without assigning , so the fact that xit cannot be changed does not matter.

Technically, you'll be a little different (because you don't break):

if (x == 200)
  System.out.println("200");
if (x == 200 || x == 300)
  System.out.println("300");

or something like that.

The fact that xit can never be 200or 300does not make the code uncompiled. However, it can allow Java to optimize the switch statement.

+3
source

, final :

//the function doesn't know what x  value is,
//but it knows that it can't modify its value
public someFunction(final int x) {

    x += 1; //compiler error
    switch(x) {
        case 200: System.out.println("200");
            break;
        case 300: System.out.println("300");
            break;
    }
}

//the function doesn't know what x value is,
//but it knows that it can modify it
//for internal usage
public someOtherFunction(int x) {

    switch(x) {
        case 200:
            x += 200;
            break;
        case 300:
            x += 300;
            break;
    }
    System.out.println(x);
}
+2

, final ? , .

, , switch?

+1

, switch -statements, , , , .

.

  switch(3){
    case 2: 
      System.out.println("two"); 
      break;
    case 3: 
      System.out.println("three"); 
      break;
  }

case 2: , .

0

, , ( ) . .

A) if-else - Dead Code. - if-else.

public static void main(java.lang.String[])

Stack=1, Locals=2, Args_size=1
0:  iconst_0
1:  istore_1
2:  return
LineNumberTable: 
line 42: 0
line 54: 2

LocalVariableTable: 
Start  Length  Slot  Name   Signature
 0      3      0    args       [Ljava/lang/String;
 2      1      1    selection       I


 }

B) if-else - , .

      final int selection i=100; //case A
      //int selection i=100; //case B

     if(selection==1){
         System.out.println("Hi");
  }else if(selection==2){

  }else{

   }

C) if-else , if-else :

       computeIfLese(int selection) 
  • OPtimization , ().

, , .

Java-...:)

, . 5:

public static void main(java.lang.String[]);
Code:
 Stack=2, Locals=2, Args_size=1
0:  bipush  100
2:  istore_1
3:  bipush  100
5:  lookupswitch{ //2
    200: 32;
    300: 40;
    default: 48 }
32: getstatic   #16; //Field java/lang/System.out:Ljava/io/PrintStream;
35: ldc #22; //String 200
37: invokevirtual   #24; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
40: getstatic   #16; //Field java/lang/System.out:Ljava/io/PrintStream;
43: ldc #30; //String 300
45: invokevirtual   #24; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
48: return
  LineNumberTable: 
line 11: 0
line 15: 3
line 17: 32
line 18: 40
line 21: 48

LocalVariableTable: 
Start  Length  Slot  Name   Signature
0      49      0    args       [Ljava/lang/String;
3      46      1    selection       I

StackMapTable: number_of_entries = 3
 frame_type = 252 /* append */
 offset_delta = 32
 locals = [ int ]
  frame_type = 7 /* same */
 frame_type = 7 /* same */


 }
0

In addition to the final parameters, the final local variable may contain values ​​that are not known at compile time:

public static void main(String args[]){
    final int x;

    if (someMethod())
      x = 200;
    else
      x = 300;

    switch(x){
        case 200: System.out.println("200");
        case 300: System.out.println("300");
    }

}
0
source

All Articles