This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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!
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
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 ]
SCJP 1.4, SCWCD in process
Joined: Dec 17, 2002
Interesting. Which way is preferred?:
Joined: Nov 20, 2004
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!
Joined: Jan 29, 2003
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?
Joined: Mar 13, 2004
Which way is preferred?:
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.