Stupidest code of the day

E

emmaps

It's forcing an unnecessary conversion of an int into a String, the
Yes i understand its not handy to convert all the way through int and
string conversion... but i thought you where also referring to fx
System.out.println("int var is: "+intvar); being bad code...
 
D

Darryl L. Pierce

emmaps said:
Yes i understand its not handy to convert all the way through int and
string conversion... but i thought you where also referring to fx
System.out.println("int var is: "+intvar); being bad code...

That's a necessary conversion. You're right, I was only referring to
*unnecessary* conversions.
 
J

Joona I Palaste

Yes i understand its not handy to convert all the way through int and
string conversion... but i thought you where also referring to fx
System.out.println("int var is: "+intvar); being bad code...

While theoretically *ALL* + operations with Strings can be replaced
with StringBuffers, the StringBuffer.append() method and .toString() or
String.valueOf() methods, with no loss of performance, in practice
things like "int var is: " + intvar are good code. Things like "" +
intvar *ARE NOT GOOD CODE*, though. This is because "writing out" the
first (with a non-empty String literal) requires several method calls,
but "writing out" the second (with an empty String literal) only
requires one, so using the method call instead makes the code both
more intuitive *AND MORE EFFICIENT*.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 
P

Paul Tomblin

In a previous article said:
While theoretically *ALL* + operations with Strings can be replaced
with StringBuffers, the StringBuffer.append() method and .toString() or
String.valueOf() methods, with no loss of performance, in practice
things like "int var is: " + intvar are good code. Things like "" +

I was under the impression that the compiler was smart enough to replace
"int var is: " + intvar
with StringBuffer method calls?
 
J

Joona I Palaste

I was under the impression that the compiler was smart enough to replace
"int var is: " + intvar
with StringBuffer method calls?

Of course it is. I was talking about replacing the + operation with
StringBuffer method calls in the source code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top