This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Software Teaming: A Mob Programming, Whole-Team Approach and have Woody Zuill and Kevin Meadows on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

String Q

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the += opertaor overloaded for the String class?
 
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes...Obviously...
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java provides special support for the String concatenation opeartor (+). String concatenation is implemented through the StringBuffer class and its append method.


if u concat two string like X= "Raj" + " Mohan";. Internally it creates a new string buffer and append each string, and then convert the contents of the string buffer to string. for example x = new StringBuffer().append("Raj").append(" Mohan");.
. += operator is overloaded and execute like binary + operator.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if u concat two string like X= "Raj" + " Mohan";. Internally it creates a new string buffer and append each string


No it doesn't.
The two operands are concatenated at compile-time to form a single literal String. A StringBuffer is never used. This is because both operands are constants (JLS 15.28).
Since 1.5, runtime concatenation is performed using a StringBuilder, not a StringBuffer.
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony
I am confused. Refer the following url.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raghu, in 1.4 using a StringBuffer for the implementation of + is optional for the compiler writer, as described in the JLS at JLS 15.18.1.2

15.18.1.2 Optimization of String Concatenation

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, using a StringBuffer (or now in 1.5, StringBuilder) is optional, yes. But that's not the main point of Tony's statement. He's pointing out that since both "Raj" and " Mohan" are compile-time constants, the expression

"Raj" + " Mohan"

is also a compile-time constant, and so it will be evaluated at compile time so that the class file contains a single constant, "Rajjava Mohan", rather than two separate ones. So at runtime, there's nothing to concatenate with a StringBuffer or StringBuilder; it's already been done.
reply
    Bookmark Topic Watch Topic
  • New Topic