File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes int to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int to String" Watch "int to String" New topic
Author

int to String

Rovart Shrivastava
Greenhorn

Joined: Feb 13, 2009
Posts: 4
How can an int be converted to a string?
For example:
int k = 9:
String Message;
Can we make something like:
Message = "Test int is " + k?

It is working with java 5 , why so?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


In Java, when an int is added to a String -- the result is a string. (and it is treated as a string concat)

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
lin qun
Greenhorn

Joined: Feb 20, 2009
Posts: 5
Rovart Shrivastava wrote:How can an int be converted to a string?
For example:
int k = 9:
String Message;
Can we make something like:
Message = "Test int is " + k?

It is working with java 5 , why so?


you can also do it like this:String convertStr=String.valueOf(k); or String convertStr=""+k;
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
If you have problems with ints, you should always look at the Integer class, and you find lots of useful methods. You will find several methods with similar names to "toString" and one of them will doubtless serve your purposes, probably the one called static toString.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int to String