Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is this behaviour?  RSS feed

 
saran sadaiyappan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i create an instance of String class and a local class by doing

String s = new String("Hello");
LocalClass c = new LocalClass();

But,when I print the String instance its giving the value while the LocalClass instance gives me the address?Why is this behaviour?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using System.out.print will call the toString() method of the object it is printing. For a String, that method prints the string itself. If you define a method "public String toString()" in LocalClass, it will be called and you can have it return anything you like.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to expand on what Ulf posted...

it's pretty obvious what a String should output when you call it's toString() method. Sun defined the function for you.

But your LocalClass could be ANYTHING. How is anybody but you supposed to know what it's toString() method should output. Maybe it should just output a string. Maybe it should output a Name, address, city and state.

Only YOU know. To keep things from breaking, every object inherits a plain, simple, always-gonna-work toString method, even though it's not very useful most of the time. but your code won't blow up.

it is therefore up to you to override the toString() method in your class in a way that makes sense to you (and hopefully everyone else).
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, code such as the following...

String s = new String("Hello");

...is a bit wasteful, as it creates an unnecessary String object. There is rarely a need to use the String constructor; you'll probably be better off simply assigning directly from the String literal. Example:

String s = "Hello";


Regards...
 
Nila dhan
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance of a string will print the string object coz when using the + operator the toString() is called.

Whereas c is an instance of the LocalClass. And when we print the instance using System.out.println("The string is"+c);
the + operator calls the toString() method of the Object class which is the parent class of all the classes in java.And the toString() method contains the following code which is self-explanatory


public String toString()
{
return getClass().getName()+"@"+Integer.toHexString(hashCode());
}

Hope this gives the core of printing an object
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!