switch (i) {
case 1:
String q_type = "2";
break;
case 99:
q_type = "3";
break;
}
以上代码,为什么能编译通过并执行,如果 i=99的话 q_type并没有定义啊
这是为什么

解决方案 »

  1.   

    全部变量和局部变量的区别。
    case1 就相当于if(**){
    String q_type = "2";
    }
    就是个局部变量。方法内部。
      

  2.   

    当i=99时,他并没用执行到q_type = "3";  这一步
      

  3.   


    局部变量,你在case方法体外部能访问?要真的能访问,那才真的奇怪了
      

  4.   


    switch (i) {
    case 1:
    String q_type = "2";//你这里有声明为一个switch的局部变量
    break;
    case 99:
    q_type = "3";//这里q_type缺不属于switch的局部变量
    break;
    }
      

  5.   


    不是,q_type就是switch里的一个局部变量,不是单属于某一个case的
      

  6.   


    我没说属于一个case的变量啊
      

  7.   

    关注下,我觉得对于这种问题,可以从现象去推论原因。
    是不是能说明 q_type 是整个switch的局部变量。
      

  8.   

    q_type 是 switch 的局部变量,编译时能通过的;
    执行的时候是99的话,就跳过了String q_type 的定义。
    是不是这样子啊~!
      

  9.   


    原因:都处于switch下面的{},所以你就把它看成一个普通的代码块。如果你不想叫他用,就把它写在case内部的一个{}里面就完了
      

  10.   

    那就是说case加{}与否编译器都会有不同的编译
    case 1:
    String q_type = "2";
    break;
    与case {
    String q_type = "2";
    break;
    }
    完全不同啊,请大侠详解,越深入越好
      

  11.   

    当99时,没有执行1。所以String qtype 没有定义。只有同时执行了情况下就没有问题,那是不可能的。
    和if 一样,变量的作用域问题!
      

  12.   

    反编译之后为:
     public static void main(String args[])
        {
            int key = 99;
            switch(key)
            {
            case 1: // '\001'
            {
                System.out.println("~~~~~~~~~~~~~~~");
                String type = "type_1";
                System.out.println(type);
                break;
            }        case 99: // 'c'
            {
                String type = "type_99";
                System.out.println(type);
                break;
            }
            }
        }
      

  13.   

    switch ( Expression )
    When the switch statement is executed, first the Expression is evaluated. If evaluation of the Expression completes abruptly for some reason, the switch statement completes abruptly for the same reason. Otherwise, execution continues by comparing the value of the Expression with each case constant. 
    字节码如下:
       0: bipush 99
       2: istore_1
       3: iload_1
       4: lookupswitch{ //2   1: 32;  99: 38;  default: 41 }
       32: ldc #2; //String 2
       34: astore_2
       35: goto 41
       38: ldc #3; //String 3
       40: astore_2
       41: return
    确实是同一个变量
      

  14.   

    case内遇到要用变量时一定要用{}括起来,不过严格的说不用{}扩起来的变量是是属于整个switch块结构的,为此编程一定要将新增变量作用域限定在case内就必须要用{}。
    case 1:{
    String q_type = "2";
    break;
    }
    case 99:
    q_type = "3";//报错,没有定义变量
    break;
      

  15.   

    String q_type = "2";
    变量声明在内部了,所以只对第一个case有用。正确的是生命倒switch外面.例如:String q_type="";
    switch (i) {
    case 1:
    q_type = "2";
    break;
    case 99:
    q_type = "3";
    break;
    }
    这样就没事了。
     
    还有楼上的朋友。谁都没说什么都会的。技术没有谁做到全面的,人家有困难咱们就帮帮,没必要说别的。
      

  16.   

    switch (i) {
    case 1:
      {String q_type = "2";}
       break;
    case 99:
       q_type = "3";
       break;
                }
    这样报错!
      

  17.   


    public class a {
    public static void main(String []args){
    int i=3;
    switch(i){
    case 1:
    mytest m=new mytest();
    break;
    case 3:
                                    m=new mytest();//顺利通过编译
    //m.a=3; 编译报错 m未初始化
    System.out.println(m.a);
    break;
    }
    }
    }class mytest{
    int a =0;
    public mytest(){
    System.out.println("进入了!");
    }

    }经过测试我得出的结论是:执行switch内部的每个case中的引用提出来放在当前case之前
      

  18.   

    补充:
    String q_type = "2";
    q_type = "3";
    其实是改变q_type 指向的地址   相当于上面例子的mytest m=new mytest();
      

  19.   

        public static void main(String args[])
        {
            byte byte0 = 9;
            String s;
            switch(byte0)
            {
            case 1: // '\001'
                s = "2";
                break;        case 99: // 'c'
                s = "3";
                break;
            }
        }楼主以后了解JVM编译机制的问题可以用先编译 再反编译解决 ! 楼主的代码 可以翻遍成如上的代码 byte byte0 = 9;
    这个是我定义的一个 int  i = 9 可能编译有点问题 但是足够解决楼主的问题了 !
      

  20.   

    有这位兄弟,发现只有在第一个case里的变量定义编译器会拿出来放外面