• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conversions

 
Theresa Duick
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Theresa Duick
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I always just say ""+object to convert to string. I guessed that just calls toString() with less typing. Wonder if that's true.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic