由於嵌套的構造函數調用,我有以下代碼及其引發的StackOverflowException,並且我知道只有在構造函數成功運行時才分配內存。爲什麼這段代碼拋出StackOverflowException而不是OutOfMemoryException?
package interview;
public class StackOverflow {
int i;
StackOverflow()
{
System.out.println(" i= "+i);
System.out.println("in Constructor");
StackOverflow sOf = new StackOverflow();
sOf.i=5;
System.out.println(" i= "+i);
}
public static void main(String[] args) {
System.out.println("in main");
StackOverflow s = new StackOverflow();
s.i=10;
System.out.println(" i= "+s.i);
}
}
所以我懷疑這裏發生了什麼'i'的價值?它被存儲在棧或堆的某個地方嗎? 在這種情況下,上面的代碼可以拋出以下異常?
OutOfMemoryException
http://ankackoverflow.com/questions/11435613/whats-the-difference-between-stackoverflowerror-and-outofmemoryerror – Trying
@ankit - 你的問題標題和描述的實際問題是完全不同的。此外,如果您在回答發佈到您的問題後更改問題,它會使答案海報混淆不清。請不要這樣做。 – SudoRahul
@ R.J感謝您的建議,我會照顧到這一點。但在最初的問題中,我也提到過我的價值 – ankit