问题
When I write the following code
int a = "Java";the java compiler displays the following error :
cannot convert from String to int
So the constant
Javais considered as an object derived fromStringclass.- Is it true that each list of characters contained between
""is considered as an object derived from String class?When I try to have concatenation of two strings I used the two following examples:
String ch1 = "Java " ; String ch2 = "is cool"; String ch3 = ch1 + ch2;after I used the command
javap -con my compiled class I found that the compiler use an instance ofStringBuilderto append the two strings.but with the other example:
String ch = "Java " + "is cool";Although the two constants "Java " and "is cool" are both two objects derived from
Stringclass, the compiler doesn't use an instance ofStringBuilderto append the two strings.- So what's the approach used in the second example?
回答1:
From here.
'+' creates a new String object every time it concatenates something, except when the concatenation is done at compile time.
While this is not a reputable source, from what I remember from my textbooks this sounds correct. So basically, applying this to your code:
String ch = "Java " + "is cool";
Would be handled at compile time since you've defined two constants and concatenated them together, which implies that the result is also in fact a constant and thus can be treated as such and calculated at compile time. It would be interesting to see if you compiled that code then decompiled to see how that statement would read, I'd imagine it may read:
String ch = "Java is cool";
As for the other statement:
String ch1 = "Java " ;
String ch2 = "is cool";
String ch3 = ch1 + ch2;
Since ch3 is calculated from ch1 and ch2, it is done at runtime since ch1 and ch2 are variables instead of constants.
As for your first question, I can't find any references exactly, but from what I remember yes the "" implies a string, just like '' implies a character. I'm not exactly sure what you're trying to do with that statement, but I would imagine you could convert your string into a char array and then cast it to an int array.
回答2:
Not sure what your second question is, but for your first question: "Java" is a string and you're trying to store it as an int type. It is not an integer. The variable a needs to be of type string.
String a = "Java ";
回答3:
In the first example you have created 2 strings vars, ch1 and ch2 that could be used in other parts of the code so... when you init ch3 a new string in memory is created with the content of both vars (here is where StringBuilder do the work). This is needed because you can change the value of ch2 and ch3 will be not changed.
In the second example both strings are final and never used again further in the code, so Java simply create a unique String "Java is cool".
I don't know if I've explained it well.
回答4:
When you do the following line
String ch1 = "Java " ;
String ch2 = "is cool";
String ch3 = ch1 + ch2;
Both ch1 and ch2 are literal and the Compiler would create String instance using StringBuilder . In the earlier days ( when compiler is not smart enough to do this) we would be wasting memory for each execution by concat operation
but when you use this
String ch = "Java " + "is cool";
Compiler understood that String Literal "Java " + "is cool" cannot be modified at any time. So the compilers would translate this into
String ch = "Java is cool";
as byte code. and only one String literal would create run time.
来源:https://stackoverflow.com/questions/22919660/concatenation-of-two-java-strings