• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to String

 
Rovart Shrivastava
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
lin qun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic