• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Have a question about convert int to string

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
int x = 1;
String sx = (String)x;
The preceding code compile error.
Why can't use "(String)" to cast?
Thank you.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int is a primitive data type,whereas String is an object.A cast from a primitive to an object does not make sense.Inorder to perform conversions like this,java provides a wonderful set of classes called wrapper classes.The wrapper class objects wraps around primitives to make them behave similar to objects.The conversion you tried can be done like this....

Hope this helps...
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works too:

Brian
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The valueOf method in String can take any primitive value, no wrapper class required!
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the easiest way to convert an int to a String is:
int x = 1;
String sx = (x+"");
by concatenating any primitive with an empty string you cast it as a String
double y = 1.2;
String sy = (y+"");
Other casting examples:
public class Conversions
{
public static void main(String[] args)
{
String strQuantity = "5"; //simulating the customer has entered 5 into a text field
//to perform a calculation, string must be converted to an int.
int quantity = Integer.parseInt(strQuantity);
String strPrice = "25.5";
double price = Double.parseDouble(strPrice);
//perform a calculation
double total = quantity*price;
//convert total to a string
String strTotal=total+"";
System.out.println(strTotal);
}
}
Tom Boyce
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic