不是这样的啊。。。
返回了一个对象啊。。。
比如这样:return new Object();
此时没有引用指向这个对象啊。。。
方法的返回值,只要类型一样就OK了,因为jvm会自己判断,当你返回引用和返回对象都不会报错的。。。。
public class Test {
public static String show(){
String str ="hello";
return str;
}
public static void main(String[] args) {
show();
System.out.println(show());
}
或者这样:
public class Test {
public static String show(){
return "hello";
}
public static void main(String[] args) {
show();
System.out.println(show());
}
2个结果都一样的。。在第一个代码中,你又不能获取函数中返回的str引用,不管怎么样都需要重新来一个引用。。
public class Test {
public static String show(){
return "hello";
}
public static void main(String[] args) {
String str = show();
System.out.println(str);
}
java里面不是有多态么?在一个函数中,当你的形参是父类的引用时,不管你传入的是子类的引用,还是子类的new Object();jvm不是都可以识别。。。这是一样的。。。
应该是jvm也许在你返回一个对象时,临时的创建一个引用。。。。
可能是这样的,猜的,,,,,
我已经发到你私信上了,度娘把我的回答抽了,是“牙刷的悲伤”那个号。。。。