起因

查看class文件的反编译代码时发现了这么一段代码:

label47: {
                    Object this$appid = this.getAppid();
                    Object other$appid = other.getAppid();
                    if (this$appid == null) {
                        if (other$appid == null) {
                            break label47;
                        }
                    } else if (this$appid.equals(other$appid)) {
                        break label47;
                    }

                    return false;
}

一下子勾起了我的兴趣,这是什么操作,java还能这么写?
不信邪的我,在正常的代码的方法里也仿写了一段,居然编译通过!

这我能忍?

追寻

原来它在这个地方叫跳转标记,不近可以标记代码块,还可以标记循环,如

outer:for(int i=0;i<5;i++){
            for(int i=0;i<5;i++){
                if(i==3){
                    continue outer;
                }
            }
        }
//这个地方就是跳转标识,可用来标记地方 里层循环 continue直接跳出外层循环,继续执行外层循环

当然了,这里用break也能达到相同的目的,但是要是再有一层循环呢?借助中间变量也能实现,但是跳转标记来做岂不是更间接。

真是一个有趣的玩法!

回忆

回忆下java中其他用到冒号的地方:

  • 三目表达式
  • for循环
  • switch

参考链接:https://blog.csdn.net/chinajobs/article/details/45874017