Top 5

Sergey Gromov snake.scaly at gmail.com
Fri Oct 10 09:20:33 PDT 2008


Fri, 10 Oct 2008 10:37:28 -0500,
Andrei Alexandrescu wrote:
> Denis Koroskin wrote:
> > On Fri, 10 Oct 2008 19:19:45 +0400, Sergey Gromov 
> > <snake.scaly at gmail.com> wrote:
> > 
> >> Fri, 10 Oct 2008 19:10:19 +0400,
> >> Denis Koroskin wrote:
> >>> On Fri, 10 Oct 2008 18:33:43 +0400, Andrei Alexandrescu
> >>> <SeeWebsiteForEmail at erdani.org> wrote:
> >>>
> >>> > Sergey Gromov wrote:
> >>> >> I'd also like
> >>> >>   "foo" ~ text ~ "bar"
> >>> >> to become something like
> >>> >>   (new Array!(char)) ~= "foo" ~= text ~= "bar"
> >>> >> that is what Java does to string concatenation.  Sugar doesn't 
> >>> seem to
> >>> >> couple well with a purely library type.
> >>>
> >>> I doubt that. Java strings are immutable, their length can't be changed
> >>> either (i.e. they are a direct analog of a proposed invariant(char)[]).
> >>> Concatanating Java strings don't turn them into array, and it is dead
> >>> slow. Every sane person uses StringBuilder instead.
> >>
> >> Java compiler substitutes
> >>
> >> "foo" + text + "bar"
> >>
> >> with
> >>
> >> new StringBuilder().append("foo").append(text).append("bar").toString()
> >>
> >> all by itself because both String and StringBuilder are built-in
> >> classes.
> > 
> > Yes, you are right. From The Java Language Specification, 3rd edition:
> > 
> >> 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.
> 
> But that doesn't require StringBuffer to be part of the language. With 
> literals, the compiler can do whatever magic it wants.

They're no literals.  They're String objects.  Try this:

public class test {
 public static void main(String[] args) {
  String s = "foo" + args[0] + "bar" + args[1];
 }
}

$ javac test.java
$ javap -c test

Though you are right in the sense that the result of this expression is 
still String, StringBuilder can be replaced with anything else or not 
used at all.



More information about the Digitalmars-d mailing list