In the book, I read that casting int to String using concatenation, for example, where а is int
String s = a + ""; this is a bad way. But
String s = Integer.toString(a); Or
String s = String.valueOf(a); These are good ways. Why? 
In the book, I read that casting int to String using concatenation, for example, where а is int
String s = a + ""; this is a bad way. But
String s = Integer.toString(a); Or
String s = String.valueOf(a); These are good ways. Why? 
No, concatenation is not bad. It looks more visually and in most cases, javac able to optimize it using java.lang.StringBuilder . Also, internment works, which allows not to create a lot of objects. For example,
String value = "hello world" + 1; System.out.println(value == "hello world1"); will lead
true
And the Integer.toString and String.valueOf identical, since the latter delegates the call to the second. But they have a feature - with each call a new line is created.
String is the same object. You use reference comparison operator for primitives. Maybe all the same equals ? - FlippyWell, maybe, if there is not just a number, but an expression, it is not immediately obvious what the result will be. For example, the following code:
System.out.println(6 + 4 + ""); System.out.println("" + 4 + 6); The first line displays 10, and the second - 46.
This is bad because Java is a strongly typed language, and implicit type conversion is a violation of the language paradigm.
If you have a small application, in which no one except you will ever get into the code, you can run your own way, but imagine that this is a complex project that will have to be corrected by some trainee.
Two code options:
String s = a + ""; String s = Integer.toString(a); The second is a completely normal and standard pattern that will not cause any difficulties, but at first it will stumble and stupid for a couple of minutes, trying to understand what is happening there and why they decided to do this.
StringBuffer.append() and StringBuffer.append() through toString() Implicit! = Invisible - Flippyint + string is an implicit cast of int to string. This is not prohibited, it is the described behavior of the language, but ideally incorrectly trite. Than the “dangerous” implicit ghost was written in his answer by Kirill Malyshev. If it comes to, for example, PHP with dynamic typing, then such an expression is perfectly normal and will not cause any complaints to anyone, and in Java, where the programmer is sure that the types are brought only consciously, it can lead to a long search for errors from scratch. The issue of hygiene can be said. - rjhdbyWhen using concatenation, several unnecessary actions are performed:
Integer.toString(a) is called implicitly;
StringBuffer is StringBuffer for concatenation where the empty string "" and the number translated to the string are copied.
StringBuffer converted to string
The implementation may be different and the compiler may optimize this, but it is better not to use such a method and explicitly call converting a number to a string.
Let's write a test class with two ways of casting a number to a string:
public class TestString { public void intToStringConcat(int value){ String stringValue = value + ""; } public void intToStringValueOf(int value){ String stringValue = String.valueOf(value); } } Baytkod for them will be the following:
public void intToStringConcat(int); Code: 0: new #2 // class java/lang/StringBuilder 3: dup 4: invokespecial #3 // Method java/lang/StringBuilder."<init>":()V 7: iload_1 8: invokevirtual #4 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder; 11: ldc #5 // String 13: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 16: invokevirtual #7 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 19: astore_2 20: return public void intToStringValueOf(int); Code: 0: iload_1 1: invokestatic #8 // Method java/lang/String.valueOf:(I)Ljava/lang/String; 4: astore_2 5: return Looking at bytecode, we can conclude that in the case of concatenation of a number with a string, a StringBuilder object is StringBuilder , the append() method is executed two times and then toString() .
The StringBuilder.append(int value) and Integer.toString(int values) methods ultimately call the Integer.getChars() method. However, for Android, the Integer.toString(int values) method Integer.toString(int values) optimized by caching values ​​in the range of -100..100. I don’t know what c version this optimization was added to, but nevertheless it is.
From all this we can conclude that converting a number to a string by concatenating a number with an empty string is a bad practice, especially critical in Android, and it doesn’t look very nice.
If you need to convert a number to a string, it is better to use the Integer.toString(int values) method. If you need to form a string of several words and numbers, then concatenation is not a bad option if it is not executed in a loop. A concatenation in a loop is generally a bad idea.
Source: https://ru.stackoverflow.com/questions/620995/
All Articles