Java经典重现之方法重载导致java.lang.StackOverflowError异常

由于代码写的不够严谨,经常认为万无一失的地方却出现了重大问题,比如说简单的一个方法重载导致的栈溢出。这种问题已经看到好几次了,虽然代码不是自己写的,还是应该引起重视。

目录

定义接口和实现

错误日志


定义接口和实现

接口:

package com.boonya.stackerror;

public interface Geometry {
    
    /**
     * 工作
     */
    void doWork();

}

实现一:

package com.boonya.stackerror;

public class LineString implements Geometry{

	public void doWork() {
		// TODO Auto-generated method stub
		System.out.println("LineString do work.");
	}

}

实现二:

package com.boonya.stackerror;

public class Polygon implements Geometry{

	public void doWork() {
		// TODO Auto-generated method stub
		System.out.println("Ploygon do work.");
	}

}

 服务方法多重载:

package com.boonya.stackerror;

public class GeometryService {
	
	/**
	 * 栈溢出方法
	 * 
	 * @param geometry
	 * @param polygon
	 */
	public static void  check(LineString geometry,Polygon polygon){
		check(geometry, polygon);
	}
	
	/**
	 * 目标方法
	 * @param geometry
	 * @param geometry2
	 */
    public static void  check(Geometry geometry,Geometry geometry2){
    	geometry.doWork();
    	geometry2.doWork();
	}

}
package com.boonya.stackerror;

public class GeometryService {
	
	/**
	 * 栈溢出方法
	 * 
	 * @param geometry
	 * @param polygon
	 */
	public static void  doWork(LineString geometry,Polygon polygon){
		doWork(geometry, polygon);
	}
	
	/**
	 * 目标方法
	 * @param geometry
	 * @param geometry2
	 */
    public static void  doWork(Geometry geometry,Geometry geometry2){
    	geometry.doWork();
    	geometry2.doWork();
	}

}

 

测试类:

package com.boonya.stackerror;

public class StackErrorTest {

	public static void main(String[] args) {
		LineString lineString = new LineString();
		Polygon polygon = new Polygon();
		GeometryService.check(lineString, polygon);

	}

}
package com.boonya.stackerror;

public class StackErrorTest {

	public static void main(String[] args) {
		LineString lineString = new LineString();
		Polygon polygon = new Polygon();
		GeometryService.doWork(lineString, polygon);

	}

}

 

错误日志

栈溢出的日志会一直打印下去,直到程序被关闭:

Exception in thread "main" java.lang.StackOverflowError
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.check(GeometryService.java:12)
Exception in thread "main" java.lang.StackOverflowError
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)
	at com.boonya.stackerror.GeometryService.doWork(GeometryService.java:12)

 

这样我们就重现了一个栈溢出的情形。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值