总结了一些java基本的一些陷阱

来源:互联网 发布:能恢复手机数据的软件 编辑:程序博客网 时间:2024/06/05 16:38
package ray.main;import java.util.Random;public class TestU {public static void main(String[] args) {// Unicode问题// \u0022表示16进制unicode的编码(对应unicode的第34个)即"System.out.println("a\u0022.length() + \u0022b".length());System.out.println("a+b".length());System.out.println("a\u0022);System.out.println(\u0022b");// 正则里面转义字符问题 * . ? + $ ^ [ ] ( ) { } | \ /System.out.println(TestU.class.getName().replaceAll(".", "/") + ".class");System.out.println(TestU.class.getName().replaceAll("[.]", "/") + ".class");System.out.println(TestU.class.getName().replaceAll("\\.", "/") + ".class");// case中break问题Random random = new Random();StringBuffer sbBuffer = null;int i = random.nextInt(2);System.out.println(i);switch (i) {case 1:sbBuffer = new StringBuffer("R");// break; //注意这里break不要忘记写,否则一直会执行defaultcase 2:sbBuffer = new StringBuffer("G");default:sbBuffer = new StringBuffer("B");}sbBuffer.append('a');sbBuffer.append('i');sbBuffer.append('n');System.out.println(sbBuffer);// j=j++问题// 这个就有意思了,自己跑下,效果意想不到,其实就是JVM的缓存搞的鬼/** * temp = j; j = j + 1; j = temp; */int j = 0;for (int t = 0; t < 100; t++) {j = j++;}System.out.println("j = j++" + j);/** * temp = j; j = j + 1; j = temp; */int n = 0;for (int t = 0; t < 100; t++) {n = ++n;}System.out.println(" n = ++n" + n);// 值越界问题int end = Integer.MAX_VALUE;int start = end - 100;int count = 0;/*for (int t = start; t <= end; t++) {count++;}System.out.println(count);*/// try finally问题try {System.out.println("try ……");System.exit(0);} finally {/* * 在以下4种特殊情况下,finally块不会被执行:1)在finally语句块中发生了异常。2)在前面的代码中用了System.exit()退出程序。3)程序所在的线程死亡。4)关闭CPU。(计算机断电、失火、或遭遇病毒攻击)。 */System.out.println("finally ……");}}}


输出结果:

23ab//////////////.classray/main/TestU.classray/main/TestU.class1Bainj = j++0 n = ++n100try ……


0 0
原创粉丝点击