![]() ![]() Public class TestClass extends java.lang. ![]() String strI = "" + javac TestClass.java & javap -c TestClass ![]() To answer Grodriguez's comment: ** No, the compiler doesn't optimise out the empty string in this case - look: cat TestClass.java Here’s an example: int number 65 // ASCII value for 'A' String asciiString Character.toString ( (char) number) In this example, the int value 65 is cast to a char and then converted to a String using Character.toString (). It's slightly less efficient ( sb.append() ends up calling Integer.getChars(), which is what Integer.toString() would've done anyway), but it works. To convert an int to a String in Java ASCII, you can use the Character.toString () method. That means every Java class is directly or indirectly inherited from the Object class and all Object class methods are available for all Java classes. Java has special support for the + operator when used with strings (see the documentation) which translates the code you posted into: StringBuilder sb = new StringBuilder() Īt compile-time. Java convert int to string using Integer.toString(int) Object class is a root class in Java. The concatenation will work, but it is unconventional and could be a bad smell as it suggests the author doesn't know about the two methods above (what else might they not know?). Normal ways would be Integer.toString(i) or String.valueOf(i). 6 Answers Sorted by: 1018 There are multiple ways: String.valueOf (number) (my preference) '' + number (I don't know how the compiler handles it, perhaps it is as efficient as the above) Integer. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |