aspose file tools*
The moose likes Beginning Java and the fly likes toString() 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()" Watch "toString()" New topic
Author

toString()

Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Hey, I was trying to turn my integer "p" into a String and was having trouble doing so. I had the simple statement:
toString(p);
and the compiler complained with:
toString() in java.lang.Object cannot be applied to (int)

Now isn't that the purpose of the "toString()" function? To turn an integer (or similar number) into a String?

Help would be appreciated! Thanks!


Want a free iPod, digital camera, or $300 cash? Completely legit offer at <a href="http://www.geocities.com/pricklymustard/freestuff.html" target="_blank" rel="nofollow">http://www.geocities.com/pricklymustard/freestuff.html</a> . I am looking for referrals which is why I need YOU!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
toString() is a method available on all objects, so we have to get an object of some sort into your statement. It might be tempting to try

but "int" is a primitive type, not an object so that won't compile. Here's an option ... make an object instance of class Integer, which is provided just to "wrap" a primitive int into a real object.

The Integer class has one more trick up its sleeve - an overloaded toString that takes an argument of int.

This looks a little funny. We're using a toString method but it's not on an object of Integer type, it's on the Integer class. toString(int) is a static method (also called a class method) on the Class Integer.

I started out saying a method has to be run on an object instance. How can we run one on the class Integer? Under the covers when you reference a class the JVM stores the information about that class in an object, and that's the object we're working on now.

This topic gets a bit confusing with objects that describe other objects. Lemme know if that helped or just made things fuzzier.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
Nicely said.

To convert an int to a String you can do this:The "Integer" class has a method that converts ints to Strings. Check out http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html

This is a bit confusing. Hope that helps.
[ November 20, 2004: Message edited by: Rick Portugal ]

IBM 286, SCJP, SCWCD, EIEIO
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You have the general idea, but the details are wrong.

Primitive types like int don't have toString() or any other method.

One simple way to make an int, p, into a string is
pString = String.valueOf(p);"

If you just want to print it, try System.out.println("p = " + p );

The toString() method is called on an object like this:
String pString = pObject.toString();
Notice how nothing goes in the parentheses.


The catch is that you must have an object. You can make an object out of an int like this:
Integer pObject = new Integer(p);

It's now even easier to go between an int primitive and an Integer object, using autoboxing in Java 5. You might want to check that out.
[ November 20, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
Interesting. Which way is preferred?:

Integer.toString(p);

or

String.valueOf(p);
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Thanks guys, awesome quick answers! I ended up using Integer.toString(p) in my program, but thanks for all the other answers as well! They provided me with useful information that I can utilize later on! Definitely going to post here with more questions in the future if I have them!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
And there's also the way of less OO beauty ...

String s = "" + i;

Then the JVM picks one of those conversions and does it for you. I guess I really ought to learn to read bytecode and see if there is any problem with this. Extra string creation? Am I caring?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Rick asks:
Which way is preferred?:

Integer.toString(p);

or

String.valueOf(p);


It doesn't really matter.

String.valueOf( int i ) uses Integer.toString( int i )

The creaters of Java are big on putting code in one place and calling it when needed. We should do likewise.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString()