日期:2014-05-19 浏览次数:20652 次
public static void main(String[] args) { Object msgObject=null; String msgString="HelloWorld"; Display(msgString); //永远返回false,不会改变msgString Display(msgObject); //永远返回false,不会改变msgObject System.out.println(msgString);// HelloWorld System.out.println(msgObject);//null System.out.println(msgString.toString());//HelloWorld System.out.println(msgObject.toString());//这个应该报空指针异常才对 }
------解决方案--------------------
public static void main(String[] args) { Object msgObject=null; String msgString="HelloWorld"; msgString = Display(msgString); Display(msgObject); System.out.println(msgString); System.out.println(msgObject); System.out.println(msgString.toString()); System.out.println(msgObject.toString()); } public static String Display(Object msg) { return "error"; } public static boolean Display(String msg) { String mString=new String("error"); msg="error"; return false; }
------解决方案--------------------
你return的是一个布尔值false。
你要想看到error这个字符串,可以上楼上一样。打印出来。或者函数的返回类型是String,return你的msg
public static String Display(Object msg) { String mString=new String("error"); msg= mString; return msg; } public static String Display(String msg) { String mString=new String("error"); msg="error"; return msg; }
------解决方案--------------------
public static void main(String[] args) {
Object msgObject=null;
String msgString="HelloWorld";
Display(msgString);//想让msgString变化成error
Display(msgObject);//想让msgObject变化成error
System.out.println(msgString);
System.out.println(msgObject);
System.out.println(msgString.toString());
System.out.println(msgObject.toString());
}