Integer.parseInt converts string to int. Is the reverse also true? Would String.parseString convert an int to a string? Would Long.parseLong convert a # or a string to a long? Would this example work with all of the primitive types?
Eric Edwards
Ranch Hand
Joined: Feb 12, 2000
Posts: 60
posted
0
Originally posted by Theresa Duick: Integer.parseInt converts string to int. Is the reverse also true? Would String.parseString convert an int to a string? Would Long.parseLong convert a # or a string to a long? Would this example work with all of the primitive types?
This will take an integer and turn it into a String. It is no longer a primitive type, it is now an object and must be treated as such. I have never tried it, but I guess this would work for any primitive type. int x = 543; String y = new Integer(x).toString(); System.out.println(y);
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
There's a static method that will do this a little quicker, without needing to instantiate an Integer just to use a method: <code><pre> int x = 543; String y = Integer.toString(x);</pre></code> Note that this is not the standard toString() inherited from Object - it's an overloaded version. There are similar methods in all the wrapper classes I believe.
"I'm not back." - Bill Harding, Twister
Theresa Duick
Greenhorn
Joined: Feb 16, 2000
Posts: 27
posted
0
Thanks, Jim! The problem is finding the similar methods. I'm just beginning to learn how to do the necessary research.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
The wrapper classes are Byte, Short, Character, Integer, Long, Float, Double, Boolean, and Void (sorta). Most (not all) of these have a static method toString(someArgument) - you can check in the API to find out which.
Leonahart Tuan Anh
Greenhorn
Joined: Nov 08, 2004
Posts: 1
posted
0
To convert a number to a String, use String.valueOf( ) methods. There're 9 overloads of the them. Eg. int var01 = 9; String strVar01 = String.valueOf(var01); This will result in strVar01 to be set to the string "9".
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
Hmm, I always just say ""+object to convert to string. I guessed that just calls toString() with less typing. Wonder if that's true.
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
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
Originally posted by Stan James: Hmm, I always just say ""+object to convert to string. I guessed that just calls toString() with less typing. Wonder if that's true.
I've heard that this technique is frowned upon. Supposedly, calling toString() directly s a Better Programming Practice.
Originally posted by Theresa Duick: Thanks, Jim! The problem is finding the similar methods. I'm just beginning to learn how to do the necessary research.
The Java API docs should be towards the top of your list when you research how to do something in Java. Learning to navigate them is a necessary skill, imho. In this case, you can scroll the upper-left frame down until you find the java.lang package. When the lower-left frame finishes loading, scroll down to the Integer, Double, Short, etc. class and click on it. The documentation for the class you chose appears in the main frame on the right.
HTH
Layne
The link I gave above is specifically for J2SDK 1.4.2. If you are using a different version, visit here to find the appropriate documentation. [ November 09, 2004: Message edited by: Layne Lund ]