Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conversion

Status
Not open for further replies.

mn12

Programmer
Dec 22, 2002
52
0
0
IL
Hi All,

Can I use the toString() method in order to convert an object to a String variable? How?

Thanks
mn12
 
I'm not entirely sure what you are getting at here, but this is how toString() works :

The class Object has a toString() method which is :

Code:
   // taken from the Sun src from SDK 1.4.1_02
    public String toString() {
	return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

.. so basically the class name and the class hash code.
If you called this code on an abject that did not override the toString() method :

Code:
Object o = new Object();
String s = o.toString();

Your String s would contain something like "java.lanf.Object@ae5hh7".


Other objects may override the toString() method to output different info, eg :

Code:
public class MyClass {

   public String toString() {
      return "Hello, this is the toString() method overridden");
   }
}

In this case, if you did this :
Code:
MyClass o = new MyClass();
String s = o.toString();

Your String s would contain "Hello, this is the toString() method overridden".

See ?!

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top