• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

String object with operation

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Can anyone please explain to me why above code output is

Hello, StringObject@<hascode>
Hello, World

I understand StringBuilder value but not value from StringObject

Thanks



 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claire Chan wrote:I understand StringBuilder value but not value from StringObject


What did you expect as output for StringObject object?
 
Ranch Hand
Posts: 116
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you try to print and object, the toString() method is called, but your class StringObject doesn't override the toString() method, so it uses the one inherited from Object, which prints this pattern of the type of the object plus @ plus hash code, as you can see in the Java documentation.

If you override the method toString, you can have another result, like this:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!