通常是递归导致,或者死循环。
在方法里调用了自己,导致无限调用,很快就会报错StackOverflowError。
例如:有些初学者会犯如下错误
//这是service类
public void saveEntity(Emp emp){
this.saveEntity(emp);
}
老司机凭肌肉记忆都知道是这样写:
//这是service类
public void saveEntity(Emp emp){
empMapper.saveEntity(emp);
}
还有一些是不容易发现的:
public xxxClient getConnection(){
if(client != null){
return new xxxClient(xxx);
}
if(!client.isConnected()){
closeClient(); // 节点1
return new xxxClient(xxx);
}
}
public void closeClient(){
xxxClient connect = getConnection(); // 节点2
if(connect!=null && this.client != null){
this.client.close();
this.client = null;
}
}
程序会在节点1和节点2之间无限调用。