Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Grant Young   on Oct 10 In Java Category.

  
Question Answered By: Fuzairah Neeman    on Oct 10

Sorry? Why? To quote Sun ...

"In the java  programming language, you can use + to concatenate Strings
together"

and goes on to say "... behind the scenes the compiler uses StringBuffers to
implement concatenation ..."

Source
java.sun.com/.../stringsAndJavac.html

Why would you use long-hand coding like your example when the compiler is
quite capable of handling the much friendlier concatenation operator?

Quoting further from Sun, their example ...

String cat = "cat";
System.out.println("con" + cat + "enation");

apparently compiles to

String cat = "cat";
System.out.println(new StringBuffer().append("con").
append(cat).append("enation").toString());

which would probably be more efficient than the multiple statement way you
have described.

In an overwhelming proportion of cases, the actual efficiency of code is
immaterial anyway, and the readability/maintainability of the code is much
more important.

Share: 

 

This Question has 9 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Problem in Fetching Values Or get search suggestion and latest updates.


Tagged: