Java的奇技淫巧

Java是一种广泛使用的计算机编程语言、面向对象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。 1995年3月23日Sun公司发布了Java,至今已有近26年,可以说是一门十分成熟的开发语言了,但在某些不为人知的地方存在着一些意料之外的特性。 Java的保留关键字 goto和const 在Java里面没有goto这个功能,但它作为保留字是无法当做变量来使用的,const也是同样。 int goto = 0; int const = 0; 上面这两行代码的写法存在问题,无法正常编译通过。 Java标签Label 上面说了在Java里面没有goto这个功能,但为了处理多重循环引入了Label,目的是为了在多重循环中方便的使用 break 和coutinue ,但好像在其他地方也可以用。 outerLoop: while (true) { System.out.println("I'm the outer loop"); int i = 0; while (true) { System.out.println("I am the inner loop"); i++; if (i >= 3) { break outerLoop; } } } System.out.println("Complete the loop"); // 输出 I'm the outer loop I am the inner loop I am the inner loop I am the inner loop Complete the loop test: { System.out.println("hello"); if (true) { break test; // works } System.out.println("world"); } // 输出 hello test: if (true) { System.out.println("hello"); if (true) { break test; // works } System.out.println("world"); } // 输出 hello test: try { System.out.println("hello"); if (true) { break test; // works } System.out.println("world"); } finally { } // 输出 hello Integer的是否相等问题 日常开发使用到Java基本数据类型是不可避免的一件事,但它却包含了一些很容易犯错的点,踩过一些坑的同学可能了解Java基本包装类型的常量池技术,例如Integer就具有数值[-128,127] 的相应类型的缓存数据,但下面定义的4个变量是否相等你是否能说的出来呢? ...

三月 13, 2021 · 2 分钟 · dushixiang