Java中的这个 - 线程“main”中的异常java.lang.StackOverflowError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23334336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:59:57  来源:igfitidea点击:

This in Java - Exception in thread "main" java.lang.StackOverflowError

javastack-overflow

提问by Erran Morad

Why am I getting a stackoverflow error ?

为什么会出现计算器溢出错误?

My class -

我的课 -

public class Tester {

int id;

 Tester(int id){
  this.id = id;
 }

 public String toString(){

  String rep = "Hex: " + this + ", Id: " + this.id;
  return rep;
 }

}

The main method -

主要方法——

class Driver{

    public static void main(String [] args){

        Tester t = new Tester(123);
        System.out.println(t);

    }

}

Error -

错误 -

Exception in thread "main" java.lang.StackOverflowError
    at java.lang.String.length(Unknown Source)
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.<init>(Unknown Source)
    at com.examscam.model.Tester.toString(Tester.java:13)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)

---------REPEAT !!!

采纳答案by Parul S

You wrote:

你写了:

String rep = "Hex: " + this + ", Id: " + this.id;

In java simply writing thismeans that you are indirectly invoking this.toString().

在 Java 中,简单的写法this意味着您间接调用this.toString().

I believe you are trying to override the toString()method of Objectand inside your version of toString()you want to print the id you have passed along with the hashcodeof the object.

我相信你正试图重写toString()的方法Object和你的版本里面toString()你想打印已与一起传入的idhashcode的对象。

So to get the output replace

所以要得到输出替换

String rep = "Hex: " + this + ", Id: " + this.id;

with

String rep = "Hex: "+ this.getClass().getName().hashCode() +", Id: " + id;

and you will get the output as:

你会得到如下输出:

Hex: 1800024669, Id: 123

回答by Rahul

you are using thiskeyword.

您正在使用this关键字。

String rep = "Hex: " + this + ", Id: " + this.id;

This represent the current object. Your current object is being called again and again recursivley so you are getting

这代表当前对象。你当前的对象被一次又一次地递归调用,所以你得到

java.lang.StackOverflowError

java.lang.StackOverflowError

回答by MrP

You are appending "this". This calls the toString() method, which again calls toString(), ... It's an infinite recursion loop, which does not have an end.

您正在附加“这个”。这将调用 toString() 方法,该方法再次调用 toString(),...这是一个无限递归循环,没有结束。

回答by geoand

Because you are referencing thisin toString()

因为您this在 toString()中引用

That means that this.toString()is being called, therefor infinite recursion is occurring

这意味着this.toString()正在被调用,因此正在发生无限递归

回答by JB Nizet

Because

因为

"Hex: " + this

is equivalent to

相当于

"Hex: " + this.toString()

and you're doing that from the toString(), so toString()calls itself, which calls itself, which calls itself...

你是从toString(), 所以toString()调用它自己,它调用它自己,它调用它自己......

回答by Elliott Frisch

Your toString method is the culprit,

你的 toString 方法是罪魁祸首,

String rep = "Hex: " + super.toString() /* Not this */
   + ", Id: " + this.id;

回答by niiraj874u

thi line

这条线

String rep = "Hex: " + this + ", Id: " + this.id;

would become

会成为

String rep = "Hex: " + this.toString() + ", Id: " + this.id;

at run-time and will again call your class's toString..wi again..

在运行时,将再次调用您班级的 toString..wi..

回答by Vineet Singla

In the line String rep = "Hex: " + this + ", Id: " + this.id;

在行中 String rep = "Hex: " + this + ", Id: " + this.id;

  this

is equivalent to

相当于

 this.toString()

and calling it from the toString(), will again call toString and again...

并从 toString() 调用它,将再次调用 toString 并再次...

回答by Blue Ocean

I think you are overriding toString method and in your overrided method body your calling your method again! you are calling toString by writing this+"" in toString method

我认为您正在覆盖 toString 方法,并且在您覆盖的方法主体中再次调用您的方法!您通过在 toString 方法中写入 this+"" 来调用 toString

回答by Naveen

String rep = "Hex: " + this + ", Id: " + this.id;

equal to String rep = "Hex: " + this.toString() + ", Id: " + this.id;internally

等于 String rep = "Hex: " + this.toString() + ", Id: " + this.id;内部

this will leads to recursive method call that will result in

这将导致递归方法调用,这将导致

 java.lang.StackOverflowError