Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

StackOverflowError with 'this'

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all ,
when try to use 'this' with toString(), I got StackOverflowError..
There is no compile time error but at run time I'm getting error.
Is this happens because 'this'(current obj ref) also try to call toString() implicitly and end with StackOveflowError..then what about next line of error message.. java.lang.AbstractStringBuilder.append(Unknown Source)
UnKnown source why??
please discuss this situation..


class Base{
public String toString(){
return ""+this;
}

public Base returnBase(){
return this;
}


}
public class CheckThis{
public static void main(String [] args){
Base b=new Base();
System.out.println(b.returnBase());
// or simply call System.out.println(b); or b.toString();
}
}

/* run time error
Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Base.toString(CheckThis.java:3)
*/
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit! I will try to re-create your error.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This error mostly occurred when your program end up in an infite recursive loop and stack assigned for each method call gets exhausted (Hope I used correct word!) .

You are calling Base#toString() methods recursively w/o any exit condition !, See where might be the problem hidden (Hint : look, what happens when you concatenate the string (using + ) with string literal )

 
Quirino Gervacio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It seems that in line 5, appending "" to this causes the operation to call toString(), which in case your own toString(), which calls itself.
""+this and ""+this.toString() are the same.

I hope this helps.
[ August 09, 2008: Message edited by: Quirino Gervacio ]
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! great...The error is indeed in the line ""+this. The toString method is supposed to generate a new string and not depend on the ""+ to generate a string representation for the object..
I would have died in finding the reason for this problem...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Quirino is perfect.
Any object when concatenated with String, Object's reference is converted to string using the "toString" method in the form of ObjectName@someAddress. So here in your problem, when you are writing as 'return "" + this', here 'this' is called as this.toString().. and which toString(), its your method that you had overridden.

Just remove the toString method from your class, and then call returnBase(), you see that your reference is being printed, why? toString of the super class is called.

Thats it..
 
amit k prabhat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all java_lovers for their respective views n suggestions...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic