Jacob Nikom wrote:
> 
> Hi,
> 
> This is not really Java-Linux, but more like Java question.
> May be it is different for Java Linux implementation, that is
> why I decided to ask.
> 
> My friend and I recently tried to figure out which String
> implementation is more efficient and correct:
> 
> String a = "bbbbbb"
> 
> or
> 
> String a = new String("bbbbbb");
> 
> He said that the second one is a copy constructor which is less
> efficient. It uses more memory and requires to copy the array
> from one place to another. I said that they are the same and it is
> simply different syntax. Who is right?
> 

Syntactically your friend is right. In the first case you are defining a
constant "bbbbbb" and assigning 'a' with a reference to "bbbbbb".
In the second case you are defining a constant "bbbbbb", constructing a
new String object from this constant and assigning 'a' with a reference
to this new String object.

So here you have at least one extra constructor call and one extra
object.

I say syntactically because this is the strict translation of your code.
A good compiler could optimize away this difference. The compilers have
special handling of String object in a few places anyway.

Now the second bad news, if you decompile the code generated with either
javac or jikes you can see that the constructor call is really compiled
into the code.

Sorry for the bad news but maybe compilers will know about this special
case in the future.

Uli Luckas


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to