File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString()

 
Justin Porter
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rick Portugal
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rick Portugal
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. Which way is preferred?:

Integer.toString(p);

or

String.valueOf(p);
 
Justin Porter
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic