This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

conversions

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic