SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
OCAJP Java 8, OCPJP Java 8
Jude Niroshan wrote:But i feel... That's hw i think about this. Correct me if i am wrong.
OCAJP Java 8, OCPJP Java 8
Aaron Shawlington wrote:OP is talking about the String pool, and says "lets assume that doSomething() is called a million times. Thanks". So obviously he's asking from a performance perspective.
As far as I'm aware, they're both the same.
Dave Tolls wrote:
Well, I did say that:
"is ... compiled into the same code ... (s)o "better" has nothing ... to do with performance."
Vitaliy Gaydarenko wrote:For one string it is really not profitable, but I think it will be other manipulations with this String in code. And in this case, it has meaning.
Dave Tolls wrote: ... creating multiple StringBuilders, which is clearly inefficient.
Vitaliy Gaydarenko wrote:
Dave Tolls wrote: ... creating multiple StringBuilders, which is clearly inefficient.
Why it will be creating multiple StringBuilders? StringBuilder create one time, in time when class will be initialized.
Dave Tolls wrote:
the compiler creates a StringBuilder for each of those '+=' lines.
Aaron Shawlington wrote:
**edit** for interest:
Junilu Lacar wrote:
What version of Java is this though?
Vitaliy Gaydarenko wrote:I have a little test:
String s = "Start ";
for (int i = 0; i < 100000; i++) {
s += i;
}
System.out.println(s);
and
StringBuilder sb = new StringBuilder("Start ");
for (int i = 0; i < 100000; i++) {
sb.append(i);
}
System.out.println(sb.toString());
Speed of second (with Builder) is greater in thousands time.
And so, with Builder is much better than without it.
Vitaliy Gaydarenko wrote:I emulated two situations about what we speak. And compare results.
We talk about resources, not abut that it will be a same code.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|