aspose file tools*
The moose likes Beginning Java and the fly likes toString() Vs (String) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString() Vs (String)" Watch "toString() Vs (String)" New topic
Author

toString() Vs (String)

Sirisha Gajulapalli
Greenhorn

Joined: Jun 12, 2003
Posts: 11
Can someone tell me if there is any difference in using a toString() method vs typeCasting to (String).. for example in the following 2 scenarios wud the result be any different in a specific case?

Scenario 1:
------------
public String getAValue(String aName)
{
return testMap.get("name").toString();
}

Scenario 2:
------------

public String getAValue(String aName)
{
return (String)testMap.get("name");
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Every class definition inherits the toString() method which allows you to specify a String representation. However, you cannot cast an object to String unless it's runtime type is String.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Casting to string and calling toString() are completely different.

Casting to String just tells the compiler to assume that an object reference that you'd previously typed as something less specific is actually a String. It doesn't actually call any methods or anything like that.

In contrast, toString() calls an overridable method on the object. The default implementation in Object returns some debug information. The version in String just returns the String. Other classes of object do other things.

In your specific example, if your Map actually contains objects that are all Strings, then both pieces of code will actually produce the same result. But don't think that means that casting to String and call toString() are generally the same, because they absolutely aren't.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1011
    
    3
Originally posted by Peter Chase:

...
In your specific example, if your Map actually contains objects that are all Strings, then both pieces of code will actually produce the same result. But don't think that means that casting to String and call toString() are generally the same, because they absolutely aren't.


Just to continue that thought...
If your Map actually contains objects that are all, say, Integers, then trying to typecast the return value from getAValue() will throw a ClassCastException. Calling toString(), however, will return a String representation of the retrieved Integer.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You should also consider the case when the parameter is not a key:

return testMap.get(aName).toString(); //throws NullPointerException

return (String)testMap.get(aName); //returns null

Is the OP assumes that the map values are Strings?


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString() Vs (String)