Java小的的知识点

little knowledge
1)return只能返回一个值,但是如果返回的是对象的形式,就可以返回多个值
2)java中任何类默认引入java.lang.*包,默认继承Object类,其中常用的默认方法有:
[Class getClass()]、[String toString()]、[boolean equals(Object that)]、[int hashCode()],自己设计的类有可能需要重写其中的某些类,以Data类中的equals方法为例:

public boolean equals(Object x){
  if(this==x) return true;
  if(x==null) return false;
  if(this.getClass()!=x.getClass()) return false;
  Date that = (Data)x;
  if(this.day!=that.day) return false;
  if(this.month!=that.month) return false;
  if(this.year!=that.year) return false;
  return true;
}

3)字符串与数字的相互转化
数字转字符串很简单,关于字符串转数字可能用到API,其中Integer.parseInt(字符串)、Integer.ValueOf(字符串)、Double.parseDouble(字符串)、Double.ValueOf(字符串)中传入的实参为字符串,parseInt返回int类型,valueOf返回Integer类型。
4)转载自http://blog.csdn.net/randyjiawenjie/article/details/7603427
Integer.valueOf()方法实现如下:

public static Integer valueOf(int i) {  
  final int offset = 128;  
  if (i >= -128 && i <= 127) { // must cache   
    return IntegerCache.cache[i + offset];  
  }  
  return new Integer(i);  
} 

Integer.valueOf()方法基于减少对象创建次数和节省内存的考虑,缓存了[-128,127]之间的数字,此数字范围内传参则直接返回缓存中的对象,在此之外,直接new出来

private static class IntegerCache {  
  private IntegerCache(){}  
  static final Integer cache[] = new Integer[-(-128) + 127 + 1];  
  static {  
    for(int i = 0; i < cache.length; i++)  
    cache[i] = new Integer(i - 128);  
  }  
} 

测试代码:

Integer i1 = Integer.valueOf(12);  
Integer i2 = Integer.valueOf(12);  
Integer i3 = Integer.valueOf(129);  
Integer i4 = Integer.valueOf(129);  
System.out.println(i1==i2);  
System.out.println(i3==i4);  
System.out.println(i1.equals(i2));  
System.out.println(i3.equals(i4));

返回值:
true
false
true
true

你可能感兴趣的:(Java小的的知识点)