Results 1 to 18 of 18
  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Basic Employee example

    The toString employee function executes even without calling the toString() method. How is that possible?

    Employee.java
    Java Code:
    public class Employee {
       
    private int id;
       private String firstName; 
       private String lastName;   
       private int salary;  
    
       @Override
    public String toString() {
    	return "Employee [id=" + id + ", firstName=" + firstName + ", lastName="
    			+ lastName + ", salary=" + salary + "]";
    }
    public Employee() {}
       public Employee(String fname, String lname, int salary) {
          this.firstName = fname;
          this.lastName = lname;
          this.salary = salary;
       }
    }
    ManageEmployee.java
    Java Code:
    class ManageEmployee
    {
    	public static void main(String args[])
    	{
    		Employee e1=new Employee("ABC","DEF",1000);
    		System.out.println(e1);
    	}
    }

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Basic Employee example

    Magic.

    No really - its built into the Java language itself. System.out.println() expects a String and so Java will automatically invoke the toString() method of the object to 'fulfill the contract'. This also works:

    Java Code:
    System.out.println("HI " + e1);
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Basic Employee example

    Quote Originally Posted by gimbal2 View Post
    No really - its built into the Java language itself. System.out.println() expects a String and so Java will automatically invoke the toString() method of the object to 'fulfill the contract'.
    That is partly true; it's the overloaded print(Object obj) method that calls the toString() method on its parameter.

    kind regards,

    Jos (<--- nitpicker)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Basic Employee example

    Quote Originally Posted by JosAH View Post
    That is partly true; it's the overloaded print(Object obj) method that calls the toString() method on its parameter.

    kind regards,

    Jos (<--- nitpicker)
    Also partly true; the overloaded print(Obejct obj) calls the overloaded static method Strnig.valueOf(Object obj) and that returns eoither the String "null" or the value returned from toString().

    db <--- picks nits from the armpits of nits
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Basic Employee example

    You are right, but sometimes its just easier to understand what is going on when you don't go into the gritty details ;) I correct myself a little:

    "No really - its built into the Java language and API itself. System.out.println() expects a String and so will automatically invoke the toString() method of the object to 'fulfill the contract'."

    Edit: come to think of it, I'm getting a deja vu. And crud, I used the word "automatically". I shouldn't have done that either. Sorry Jim.
    Last edited by gimbal2; 08-12-2013 at 03:15 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Basic Employee example

    Quote Originally Posted by DarrylBurke View Post
    Also partly true; the overloaded print(Obejct obj) calls the overloaded static method Strnig.valueOf(Object obj) and that returns eoither the String "null" or the value returned from toString().
    That is sooo irrelevant compared to what I wrote ;-)

    kindest regards,

    Jos (<--- the important guy)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Basic Employee example

    Why is that in some cases we explicitly need to call toString() and in some cases it gets called automatically. Why can't they make it uniform everywhere?

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Basic Employee example

    Because programming is hard.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Basic Employee example

    Quote Originally Posted by gimbal2 View Post
    Because programming is hard.
    Why is it called software instead of hardware then?

    kind regards,

    Jos (<--- utterly confused)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Basic Employee example

    'Confused' being a code word for 'maniacal'. Ha, always wanted to use that word.

    Henceforth, I shall use the word difficult as to not confuse the poor Jos. I know, its DIFFICULT that words can have a double meaning, if only English was as clean and precise as Java.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Basic Employee example

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Why is that in some cases we explicitly need to call toString() and in some cases it gets called automatically. Why can't they make it uniform everywhere?
    So we don't get confused by "automatically", there is nothing automatic about the above. As explained by Darryl and Jos the println() functionality is coded to call toString() (eventually). It's not magic. If it was something the compiler did (similar to how string concatenation works) then I would agree it was automatic...but it isn't.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Basic Employee example

    Quote Originally Posted by Tolls View Post
    So we don't get confused by "automatically", there is nothing automatic about the above. As explained by Darryl and Jos the println() functionality is coded to call toString() (eventually). It's not magic. If it was something the compiler did (similar to how string concatenation works) then I would agree it was automatic...but it isn't.
    So if I understood correctly, if I am using sop, then there is no need to call the toString() function?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Basic Employee example

    No, there is no need for you to write code to call toString because the code underlying println() for a non-String object already calls it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Basic Employee example

    Quote Originally Posted by gimbal2 View Post
    Edit: come to think of it, I'm getting a deja vu. And crud, I used the word "automatically". I shouldn't have done that either. Sorry Jim.
    Quite alright (especially since I agree with you). I saw that and decided to stay out of the fray. However, even the JDK documentation uses the word automatic to describe behavior that is nothing more than a method call (similar to what toString() does). See PrintStream (Java Platform SE 7 ). As I said before, it is simply a matter of semantics.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Basic Employee example

    Quote Originally Posted by gimbal2 View Post
    Henceforth, I shall use the word difficult as to not confuse the poor Jos.
    Which is why I never refer to fundamental problems in Java as BASIC problems. (Notice the title of this thread)

    As Jim said:
    Quote Originally Posted by jim829 View Post
    it is simply a matter of semantics.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Basic Employee example

    Yeah I was disappointed too. You'd expect a good old Basic program and then its boring old Java again.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Basic Employee example

    Quote Originally Posted by gimbal2 View Post
    Yeah I was disappointed too. You'd expect a good old Basic program and then its boring old Java again.
    Yeah no GOTOs and GOSUBs :(

    And no line numbers :( :(
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Basic Employee example

    Someone pls help me. Implicitly toString is called if it is used in a SOP statement but otherwise we have to explicitly call it. Please let me know if I am correct or not.

Similar Threads

  1. Jsp Project on Employee Details
    By 82rathi.angara in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 01-20-2014, 09:34 AM
  2. Employee Program
    By gusten in forum New To Java
    Replies: 1
    Last Post: 07-03-2013, 04:43 AM
  3. employee availability
    By mikerada6 in forum JDBC
    Replies: 3
    Last Post: 12-20-2010, 11:18 AM
  4. employee
    By srish in forum Suggestions & Feedback
    Replies: 7
    Last Post: 07-21-2010, 04:52 AM
  5. search employee id
    By raman_v in forum Java Servlet
    Replies: 2
    Last Post: 12-24-2009, 08:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •