This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling toString method

 
srujana maredugu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ExpImpl1 extends Object
{
public static void main(String argsd[])
{
ExpImpl1 exp=new ExpImpl1();
System.out.println("in main "+ exp);
}
}

I am trying to print ExpImpl1 object but it is calling Object toString() method.Can you please let me know how it is calling Object toString() method.

Thanks & Regards
Srujana
 
Amir Alagic
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class extends Object and it was not necessary to write that. Also you didn't override toString() method in you class (ExpImpl1) and therefore toString() method from Object is called. When you implement your own toString() method (public String toString()) in your class (ExpImpl1) you will override Object toString() method and your implementation will be called when you call toString() on ExpImpl1.

http://www.javabeginner.com/java-tostring.htm
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class doesn't have its own toString() method, so it inherits the toString() method of class Object.

Inheritance is a basic object-oriented programming concept. See the Inheritance lesson in Sun's Java Tutorial for more information.
 
srujana maredugu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are correct.It is inheriting Objects toString() method so it is calling. what my doubt is,we are not explicitly calling Object's toString() method.How it is calling toString() method.
 
Arivazhagan Arutchelvam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use primitive data types such as int,float, double etc., in your System.out.println() statement the value of the data is printed. But when you use an object reference in the System.out.println() statement the object toString() method is called and output of the toString() method(String) is printed in the console.
[ February 19, 2008: Message edited by: Arivazhagan Arutchelvam ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the + operator to concatenate a string and an object, like this: "in main " + exp
then the compiler translates this behind the scenes to something like this:

StringBuffer sb = new StringBuffer("in main ");
sb.append(String.valueOf(exp));
sb.toString();

Note that String.valueOf(exp) will call toString() if exp is not null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic