Powerofviva Viva wrote:So, String calls concatination method and creates a new object for each and every + , because it is immutable.
That is why
String st = "hi";
String st1= st;
St == st1 is false, thay are pointing to diffrent refrence, diffrent memory address and a new obj is created.
The first code may be more efficient because any optimisation available will be used. In fact the concatenation will be applied at compile time because those two literals are compile time constants.Powerofviva Viva wrote:. . . String str= "ggg" + "hhh"+ ...
Stringbuilder strb = new StringBuilder().append("ggg").append(...
No. the JLS = Java® Language Specification disagrees with you. Whatever method is called is an implementation detail and is expressly hidden in case it changes in more recent versions. Anyway, you only get problems with creating new String objects if you use + repeatedly in a loop.So, String calls concatination method and creates a new object for each and every + , because it is immutable.
Once we get rid of the spelling mistakes, the use of that == operator returns true.
That is why
String st = "hi";
String st1= st;
St == st1 is false . . .
Does it?But the same test with stringbuilder returns true. . . .
No, you should use a StringBuilder for concatenating multiple Strings at runtime, nor literalsthose reasons are clear to use stringBuilder when going to build a string with concatinating alot of literals. . . .
Because it says in the JLS that,And why still some people say use + and compiler will do amagic to create a bytecode like string builder! . . .
…and in §15.29 it says that a String constant is automatically interned.That JLS page wrote:The String object is newly created (§12.5) unless the expression is a constant expression (§15.29).
Powerofviva Viva wrote:So, String calls concatination method and creates a new object for each and every + , because it is immutable.
Powerofviva Viva wrote:And why still some people say use + and compiler will do amagic to create a bytecode like string builder!
Powerofviva Viva wrote:Moreover, if at the end, compiler will generate byte code to use stringbuilder, why do not use it from the beginning?
No. The concatenation in the code you showed will be done at compile time and there will be no objects to delete from memory.Powerofviva Viva wrote:. . . After each + the previous string obj will be garbage collected right? (In runtime)
Stop looking at implementation details which you don't need to know.Moreover, if at the end, compiler will generate byte code to use stringbuilder, why do not use it from the beginning?
Which bytecode isn't the same as what?Also their bytecode is not 100% the same.
Campbell Ritchie wrote:Stop looking at implementation details which you don't need to know.
Campbell Ritchie wrote:Which bytecode isn't the same as what?
That's a pleasureBjörn Björnsen wrote:Thanks everyone . . .
And I disagree. You will not improve the performance by explicitly using a StringBuilder. You will get better performance with a StringBuilder only if the concatenation occurs in multiple statements, usually in a loop. It is worth using a StringBuilder if you are using a loop, or if you find its code easier to read.I agree with you, it is better to use StringBuilder . . .
Change line 1 to make the reference final and try again.My last question is , since:
First statement, none. The concatenation is performed at compile time.how many objects will be created in two examples below . . .
Probably never.and how many times garbage collation will be performed? . . .
Björn Björnsen wrote:how many objects will be created in two examples below (lets say concatenating about 50 string literals) and how many times garbage collation will be performed?
If they think it worthwhile, which it probably isn't, they might introduce such an optimisation into a future version. Of course, I haven't checked whether changing the semantics of + like that would be permissible.Stephan van Hulst wrote:. . . The Java designers could have forced a similar optimization for the concatenation operator . . .
Stephan van Hulst wrote:As has been pointed out by others, using StringBuilder directly only becomes efficient when you're concatenating many strings that are NOT compile-time constants, or when you're concatenating strings in separate expressions.
There are two package‑private classes one for “Latin” Strings and one for other Strings. It is pretty easy to iterate a char[] to check that all its elements are < 0x80. It must be reasonably easy to interconvert the two types of array, otherwise it would have a serious performance himdicap.Paul Clapham wrote:. . . It used to be that a String contained an array of chars , , , now if those chars can all be represented within a byte then the String contains an array of bytes instead. Presumably there's a flag . . . ..
In UK, we would say, “swings and roundabouts.”Mike Simmons wrote:. . . if there is any non-Latin-1 content, it's nice to encounter it early on . . . .
If the compiler cannot determine that the two resultant Strings will have the same contents, then the concatenation will take place at runtime and different objects will be created. If you mark the variable in line 1 final, it becomes a compile‑time constant, officially called a constant expression. The JLS (=Java® Language Specification) will tell you all the gory details. Whether the variable in line 1 is final or not determines whether the expression to the right of the = in line 2 is or isn't a compile‑time constant. Whether they are or are not compile‑time constants determines when the String objects are created.Björn Björnsen wrote:. . . Does it mean it creates a new obj for + if it is no final?
Björn Björnsen wrote:It means line 2 will be a compile time constant without any change in line 2.
for the second one, it adds the value of str2 to string pool, so return true.
the third one, still is not clear to me, hmmm... by making the first line final, it will be compile time constant, and line 2 , even when it calls str1, still knows it is compile time constant and concatinate like two literals, so the result will be true?
This particular call to intern() simply finds the value that was already cached in the string constant pool and returns it. It doesn't actually add anything to the pool.
Björn Björnsen wrote:So you mean this particular .intern is redundant.
Björn Björnsen wrote:so concatenating with + does something here on str2
by making str1 final, compiler understand in line 2 that a call to local variable str1 is a call to a compile time constant and for the same reason, it returns true.
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|