1. #1
    Field Marshal
    10+ Year Old Account
    Join Date
    Dec 2009
    Location
    Florida
    Posts
    91

    Java .toString() question

    Hey all,

    I'm working on a programming assignment for school. The whole thing runs just as intended, so I don't need help with any of the coding. But I do have a question as to why something works the way it does.

    Synopsis:

    I constructed 2 classes that take four integer arguments and store them. The first is EarthTime which accepts Minutes, Hours, Days, and Years. The second is an alien time, PsarTime, which accepts Sparens, Cies, Cromas, and Asurs. The latter units convert differently than EarthTime, and the task was to make functions for each class that can convert each time and add and subtract them in anyway, after which units are reduced to the lowest common factors (signs being preserved). Anyway, that's not the important part.

    To output to console, I called
    Code:
    System.out.println(EarthTime1);
    (EarthTime1 is an instance of class EarthTime).

    Previously in the class I had constrcuted a String toString() method as follows
    Code:
    public String toString(){
    		String time;
    		time = this.getYear() + "Y:" + this.getDay() + "D:" + this.getHour() + "H:" + this.getMinute() + "M";
    		return time;
    	}
    Anyways, running the print to console method, I was suprised to see that it printed following the rules of the toString() method that I had defined even though I had not called toString() anywhere in the execution, nor in any other methods (outside of defining the method itself).

    Does it just replace the java .toString() method? I'm curious if anyone can clue me in as to why it is behaving this way. Again, this is the desired result, I'd just like to understand why it happened. Thanks!

  2. #2
    Regarding Strings, an object reference as part of a String (i.e. if you said "EarthTime is: " + EarthTime2 + ".") will automatically take the toString() of that object and put it into the String. If you don't have a toString method in that class, it will use the toString method of the Object class, which will return something like "Classname@hashcode".

  3. #3
    The easiest way to explain it is that when given an Object argument instead of a primitive, println calls the toString method of the Object. That way println can handle anything - primitives it handles in a certain way, and everything else with a call to toString. Your class(es) and every class for that matter inherits from the Object class which is why you are overriding toString and not just creating it.

  4. #4
    Field Marshal
    10+ Year Old Account
    Join Date
    Dec 2009
    Location
    Florida
    Posts
    91
    Ah makes sense. That's kind of what I thought what was happening, but I just wanted verification (so that, later on, I don't mistakenly use any misinformation I might have assumed on my own). Thanks for the replies!

Posting Permissions

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