java sundry tips

1.关于Arrays 
记得binarySearch方法返回的int 类型的数值的含义。 
   If the array contains multiple elements with the specified value, there is no guarantee which one will be found. 
   而且当查找数小于数组中任何一个数时返回-1 ,当查找数大于数组中任何一个数时返回 -(array.length+1) 

System.out.println(Arrays **) 
如果参数类型是char[],那么输出是由数组元素组成的字符串,如果是其他类型的数组,则是随机字符串。

2.static方法调用

下面这个非常简单,但是很多有经验的Java开发者都会中招。闲话少说,看代码:

NullTest myNullTest = null;
System.out.println(myNullTest.getInt());

当看到这段代码时,很多人会以为会出现NullPointerException。果真如此吗?看看其余代码再说:

class NullTest {
     public static int getInt() {
         return 1;
     }
}

永远记住,类变量和类方法的使用,仅仅依赖引用的类型。即使引用为null,仍然可以调用。从良好实践的角度来看,明智的做法是使用NullTest.getInt()来代替myNullTest.getInt(),但鬼知道什么时候会碰上这样的代码。

原文地址:https://www.cnblogs.com/tianlanliao/p/4065526.html