java中String,Integer的奇怪比较和带标签的break、continue

带标签的break、continue语句,在没有标签的break和continue语句中,这两个关键字都是只能操作一层循环。break能结束与自己最近的一层循环,continue能跳过与自己最近的一层循环中的一次循环。

public class test{
    public static void main(String[] args) {
        String a = "123";
        String b = "123";
        System.out.println(a==b);      //true
        Integer a1 = new Integer(3);
        Integer a2 = new Integer(3);
        System.out.println(a1==a2);    //false

        System.out.println("IS_OUT");
        flag:
        for(int i=0; i<2; i++){
            for(int j=0; j<2; j++){
                for(int k=0; k<2; k++){
                    System.out.println("IS_IN");
                    break flag;      //执行到这 直接跳出循环
                }
            }
        }
        System.out.println("end");

        label:
        for(int i=0; i<2; i++){
            for(int j=0; j<2; j++){
                for(int k=0; k<2; k++){
                    System.out.println(i+"   "+j+"   "+k);
                    continue label;    //跳一次label下的循环
                }
            }
        }
        System.out.println("end");
    }
}

其结果

自动拆装箱

public static void main(String[] args) {
    Integer a = new Integer(3);
    Integer b = 3;                  // 将3自动装箱成Integer类型
    int c = 3;
    System.out.println(a == b);     // false 两个引用没有引用同一对象
    System.out.println(a == c);     // true a自动拆箱成int类型再和c比较
}

 

posted @ 2019-06-28 17:32  缓步徐行静不哗  阅读(284)  评论(0)    收藏  举报