This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question required " Watch "question required " New topic
Author

question required

Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
I think your wondering why toString is called. When you called print or prinln on an object the toString method of that object is called.
Here is the doc


print

public void print(Object obj)

Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
obj - The Object to be printed
See Also:
Object.toString()


here is the link to the doc
http://java.sun.com/j2se/1.3/docs/api/java/io/PrintStream.html


MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
class A {
public String toString () {
return "4";
}
}
class B extends A {
public String toString () {
return super.toString() + "3";
}
}
public class test {
public static void main(String[] args) {
System.out.println(new B());
}
}
Compilation succeeds and 43 is printed.
But why ?
Thanks !!
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
return super.toString() + "3"

Tells the class to call the super (A) toString method, which returns "4". "4" + "3" = "43"


SCJP 1.4 SCWCD 1.4
natarajan raman
Greenhorn

Joined: Apr 10, 2004
Posts: 26
Thanks a lot!!!That's really helpful Inuka.....


nata.r<br />**********************************<br />To win is not always success<br />And to lose is not always failure.<br />**********************************
VINCE CARTER
Greenhorn

Joined: Sep 26, 2004
Posts: 13
Thanks!!!
 
 
subject: question required