Re: Java blunders
On 7/28/2014 4:23 PM, Josip Almasi wrote:
On 07/24/2014 03:53 AM, Roedy Green wrote:
On Wed, 23 Jul 2014 19:55:09 -0400, Arne Vajh?j <arne@vajhoej.dk>
wrote, quoted or indirectly quoted someone who said :
But is it a problem in real world programming?
I have never seen a problem due to this.
The most common error is like this:
out.println ( "the answer is " +
a + b
+ ".");
you must write that as:
out.println ( "the answer is " + (a + b) + ".");
I think you guys missed worst thing with java concatenation, and it's
got nothing to do with operator.
It's about how operator gets interpreted:
out.println ( new StringBuffer("the answer is ").append(new
StringBuffer(a + b)).append(new StringBuffer("."));
Why do you think this is so? javac 1.8.0_11 compiles it to
the equivalent of
out.println(new StringBuilder()
.append("the answer is ")
.append(a + b)
.append(".")
.toString());
.... and I think all javac's have done so for a long time (but I
don't have them available for checking).
It's three new objects, all synchronized.
... for suitable values of "three" (i.e., 2) and "all" (0).
OK I haven't read the spec for a while but that's it IIRC, spec requires
it that way.
StringBuilder replaced StringBuffer for this purpose in Java 1.5,
just under ten years ago. I guess that counts as "a while." ;-)
And if you want to avoid the garbage, you need to
StringBuilder shit = new StringBuilder();
shit.append("the answer is ");
shit.append(a + b);
shit.append(".");
and another line to get the string.
Um, er, no.
--
esosman@comcast-dot-net.invalid