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.
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).
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
...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:
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