В настоящее время я работаю над оптимизацией кода, используя try..finally Block для уважения моих объектов.
Но я не понимаю, как управляется возврат объекта когда я создаю нулевую ссылку на объект в моем блокеfinally. ??
При возврате объекта в блоке try будет ли он создавать предварительно скомпилированный оператор во время компиляции? или создать новую ссылку в куче, пока дело доходит до оператора возврата? или просто вернуть текущую ссылку на объект?
Ниже приведен мой исследовательский код.
Код: Выделить всё
public class testingFinally{
public static String getMessage(){
String str = "";
try{
str = "Hello world";
System.out.println("Inside Try Block");
System.out.println("Hash code of str : "+str.hashCode());
return str;
}
finally {
System.out.println("in finally block before");
str = null;
System.out.println("in finally block after");
}
}
public static void main(String a[]){
String message = getMessage();
System.out.println("Message : "+message);
System.out.println("Hash code of message : "+message.hashCode());
}
}
Inside Try Block
Hash code of str : -832992604
in finally bolck before
in finally block after
Message : Hello world
Hash code of message : -832992604
I am very surprised when I see both returning object and calling object have same hashcode.
So I am confused about object reference.
Please help me to clear this fundamental.
Источник: https://stackoverflow.com/questions/277 ... internally