Results 1 to 10 of 10

Thread: toString

  1. #1
    luckyleaf95 is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default toString

    what does the toString method do?
    i saw one that looks like the following,
    public String toString() {
    ....
    return something;
    }

    This looks like a getter method because it returns something, but I thought getter methods should start with the name "get" just so its easy for others to read. Can someone explain what toString means and whether it can have explicit parameters?

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Yeah toString is just a naming convention for JAVA. You can call it getString if you wish. Usually if you create lets say a name object, instead of testing what output the object contains, you test each variable.

    Oh yeah and from my experience it also allows returning integers, so in that return statement you can include variables that are declared as int.

    Java Code:
    public String toString()
    {
        return fName + lName; // Testing each variable
    }
    Please correct me if I am wrong.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Well, toString() method only returns the string representation of your object. Ex:
    Java Code:
    public class MyClass {
           int a, b;
           public MyClass(int a, int b) {
                    this.a = a;
                    this.b = b;
           }
           public String toString() {
                 String ret = a+" "+b+"\n";
           }
           public static void main(String[] args) {
                  MyClass a = new MyClass(1,2), b = new MyClass(3,4);
                  System.out.println(a+""+b);
           }
    }
    Since you have a toString() method for MyClass, it gets called automatically, when you try to print out an object of that class. The output would be:
    1 2
    3 4

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    It's better to do toString() than getString() so you can override Object's toString() method. Example:

    Java Code:
    public class MyClass {
           int a, b;
           public MyClass(int a, int b) {
                    this.a = a;
                    this.b = b;
           }
           public String getString() {
                 String ret = a+" "+b+"\n";
           }
           public static void main(String[] args) {
                  MyClass a = new MyClass(1,2), b = new MyClass(3,4);
                  System.out.println(a+""+b);
           }
    }
    Now on a driver program, if you did:
    Java Code:
    MyClass test = new MyClass(1,2);
    System.out.println(test); //invalid
    If you had toString instead of getString, it would be valid.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Who said anything about a setter or getter? I did notice my toString() was missing a return statement, but that was just an oversight.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It was the OP who described toString() as a getter. Sometimes people use the term "accessor" also.

    The getString() posted won't compile for lack of the return statement. But, more important, is the fact the line labelled "//invalid" is perfectly valid. For the very reason Lil mentioned: that toString() is a method of all Object instances (it's overridden).

    By overriding toString() you can ensure that methods like println() do something useful. println() is designed to use the toString() method when it produces its output and there are a lot of methods that do this.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    As a side note - the output generated by Object.toString() (so fore any object that does not have toString overridden) is this:
    Java Code:
    MyClassName @ mem_address
    or something similar - i.e. it gives you nothing useful to work with when debugging, as memory management is done for you by the JVM (apologies, C++ mem management has been driving me wild - and there addresses are useful.)

    Hope this helps,
    SingingBoyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    It's the hashcode of the object...not the address.

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by Tolls View Post
    It's the hashcode of the object...not the address.
    My apologies. The Object#toString method converts the hashcode to a hexadecimal string, which is what got me. (Memory addresses are hexadecimal integers)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    I only remember that because waaay back I used to think it was the memory address...only to be thoroughly confused when two instances of a class that should have had different references seemed to have the same address.
    It is now burned into my brain...
    :)

Similar Threads

  1. Formatting a toString
    By MooNinja in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 07:32 PM
  2. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 11:43 PM
  3. toString question
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 07:41 PM
  4. Arrays.toString
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:35 PM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:32 PM

Posting Permissions

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