Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This line</p> <pre><code>System.out.println(stud); </code></pre> <p>is equivalent<sup>1</sup> to</p> <pre><code>System.out.println(stud.toString()); </code></pre> <p>Since <code>String</code> <a href="http://docs.oracle.com/javase/tutorial/java/IandI/override.html" rel="nofollow">overrides</a> the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString%28%29" rel="nofollow"><code>Object.toString</code></a> method you get something more meaningful than a bunch of characters and digits when printing strings.</p> <p>You could let your user-defined classes do this too. In your <code>Student</code> class it would look like this:</p> <pre><code>class Student{ int age; public void setAge(int age){ this.age=age; } public int getAge(){ return age; } @Override public String toString() { // Called for instance when return "Student with age " + age; // the student should be printed } } </code></pre> <p>Here's an <a href="http://ideone.com/5NLmB" rel="nofollow">ideone.com demo</a> for a run with your code where <code>Student</code> overrides <code>toString</code>.</p> <p><strong>Further reading:</strong></p> <ul> <li>Documentation for <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString%28%29" rel="nofollow"><code>Object.toString</code></a> (the fall-back method for classes that do <em>not</em> override it)</li> <li>Documentation for <a href="http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html#println%28java.lang.Object%29" rel="nofollow"><code>PrintStream.println</code></a></li> <li><a href="http://www.javapractices.com/topic/TopicAction.do?Id=55" rel="nofollow">Implementing toString</a></li> </ul> <p><sup>1) Unless stud equals null</sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload