Java StackOverflowError code example
Example 1: stackoverflow error
It usually happens when the stack pointer exceeds the stack bound. Usually due to deep infinite recursions
Example 2: java stackoverflowerror
Number: 1
Number: 2
Number: 3
...
Number: 6262
Number: 6263
Number: 6264
Number: 6265
Number: 6266
Exception in thread "main" java.lang.StackOverflowError
at java.io.PrintStream.write(PrintStream.java:480)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
at java.io.PrintStream.write(PrintStream.java:527)
at java.io.PrintStream.print(PrintStream.java:669)
at java.io.PrintStream.println(PrintStream.java:806)
at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:4)
at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:9)
at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:9)
at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:9)
...
Example 3: java stackoverflowerror
public class StackOverflowErrorExample {
public static void recursivePrint(int num) {
System.out.println("Number: " + num);
if (num == 0)
return;
else
recursivePrint(++num);
}
public static void main(String[] args) {
StackOverflowErrorExample.recursivePrint(1);
}
}
Example 4: java.lang.StackOverflowError
public class Vehicle {
public void accelerate(float acceleration, float maxVelocity) {
}
}
public class SpaceShip extends Vehicle {
@Override
public void accelerate(float acceleration, float maxVelocity) {
this.accelerate(acceleration, maxVelocity);
}
}
Example 5: java stack overflow
class someClass{
public static void main(string[] args){
loop();
}
public static void loop(){
loop();
}
}
class someOtherClass{
public static void main(string[] args){
while(true){
Systen.out.println("STACKOVERFLOW");
}
}
}
class someOtherOtherClass{
public static void main(string[] args){
for (i = 1; i > 0; i++){
Systen.out.println("STACKOVERFLOW AGAIN");
}
}
}