在C/C++ 回傳區域變數問題
想請問一下我下面反轉字串例子,
String resultStr = ""; 是 local 變數,
這樣回傳回去是把內容 copy 一份回去main() 函數的result?
應該不是ref? String resultStr = ""; 離開reverse函數應該就回收這塊記憶體
如果我今天改成 String resultStr = new String();
傳回是把address 回傳去讓 result 參考同一塊記憶體嘛?
我查網路Java 無法印address 來判斷有人說用hashCode, 但是看到
hashcode :
只要求相同對象的 hashcode 一樣
並不保證不同對象的 hashcode 不一樣
"即,有可能不同對象的 hashcode 是一樣的"
public static String reverse(String originalStr)
{
String resultStr = "";
... 處理字串反轉存到 resultStr
return resultStr;
}
public static void main(String[] args) throws IOException {
String s = new String("hello world");
String result = reverse(s);
System.out.println(Integer.toHexString(result.hashCode()));
System.out.println(result);
}
完整程式碼
https://gist.github.com/shihyu/e6acfc206928b2cbb3011ea193fe1c4f
謝謝