class Miner{
static int x=7;
static String s = null;
public static void getWeight(Mineral m){
int y=0/x;
System.out.println(s+"haha");
}

public static void main(String ... args){
Mineral[] ma = {new Mineral(),new Gem()};
for(Object o:ma){
getWeight((Mineral)o);
}
}
}
class Mineral{}
class Gem extends Mineral{}

输出nullhaha,我怎么理解不了 

解决方案 »

  1.   

    static String s = null;
      

  2.   

    打印是这样的吧
    nullhaha
    nullhaha
    数组中有两个元素啊 ,循环两次,每次都打印 s+"haha"  ,就是 nullhaha,有什么不理解的
      

  3.   

    你的s=null,而print的时候会将对象转换成字符串拼接起来显示的,null就变成了"null",再拼接一个haha,当然打出来的结果就是nullhaha
    如果你定义int s=1然后,println(s+"haha"),最终打出来的就是1haha。道理很简单,强制转换而已
      

  4.   

    String 类的toString() , null 输出为 'null'
      

  5.   

    就是通过循环调用了两次的getWeight方法
    结果就是
    nullhaha
    nullhaha