• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is this behaviour?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic