While working on hyperbolic differential equation solver, a question arose. Being a curious sort of fellow, I decided to investigate, and was able to satisfactorily reach a conclusion (which turned out not to be very surprising or exciting). Here is the experiment (it’s pretty self explanatory):
public class test1
{
public test2 a;
public int level;
public test1()
{
a = new test2();
level = a.level+1;
}
}
public class test2
{
public test1 a;
public int level;
public test2()
{
a = new test1();
level = a.level+1;
}
public test2( int k)
{
a = new test1();
level = k;
}
public static void main(String[] args)
{
test2 lol = new test2(0);
}
}
I was pretty surprised that it actually compiled. The output is
C:\HURRDURRPATH>java test2
Exception in thread "main" java.lang.StackOverflowError
at test2.(test2.java:8)
at test1.(test1.java:8)
at test2.(test2.java:8)
at test1.(test1.java:8)
at test2.(test2.java:8)
at test1.(test1.java:8)
at test2.(test2.java:8)
at test1.(test1.java:8)
at test2.(test2.java:8)
at test1.(test1.java:8)
at test2.(test2.java:8)
... (1024 lines of this)
In hindsight, this is probably the most reasonable outcome. Well, since I’m never going to get those 5 minutes back, I thought I might as well put this up in case it will be of interest to any of you…
Edit: dear wordpress, hint: the purpose of having code tags is not just to convert to a monospaced font. You’re supposed to leave the whitespace as is. Kindly suicide kthxbye~
Posted by frymetothemoon