Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Is "String s = "abc";" equal to "String s = new String("abc");"?

Reply
Thread Tools

Is "String s = "abc";" equal to "String s = new String("abc");"?

 
 
Bruce Sam
Guest
Posts: n/a
 
      11-18-2004
In my opinion,"String s = "abc";" has only created a reference s not a
object."String s= new String("abc");" has created a new object and its
reference is s.Is it right?If I'm right,I still have a question.When I
can use first one not need the second one?

 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      11-18-2004
Bruce Sam wrote:

> In my opinion,"String s = "abc";" has only created a reference s not a
> object."String s= new String("abc");" has created a new object and its
> reference is s.Is it right?


Not really. Both declare a reference, and while the first doesn't
explicitly *create* an object at runtime, there still *is* an Object.
It's part of the constant pool of the class and created when the
class is loaded.
 
Reply With Quote
 
 
 
 
kaeli
Guest
Posts: n/a
 
      11-18-2004
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) enlightened us with...
> In my opinion,"String s = "abc";" has only created a reference s not a
> object."String s= new String("abc");" has created a new object and its
> reference is s.Is it right?If I'm right,I still have a question.When I
> can use first one not need the second one?
>
>


It is my understanding that if you don't specify the "new" keyword, if
another string object already has a reference to the value "abc", a new
object is not created (no more memory is allocated) - instead, it points to
the same spot in memory. If nothing does, it does create a new object (memory
allocation). If you specify the new keyword, a new object is always created
(memory allocation), regardless.
Mostly the difference is seen with the equals method and '=='.

See this page for a better explanation:
http://www.jchq.net/certkey/0502certkey.htm

--
--
~kaeli~
You feel stuck with your debt if you can't budge it.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
kaeli <(E-Mail Removed)> wrote:
> It is my understanding that if you don't specify the "new" keyword, if
> another string object already has a reference to the value "abc", a new
> object is not created (no more memory is allocated)


It's a little different than that. There is a pool of String objects
maintained by the JVM. Some String objects are in that pool, and others
are not. When you say:

String a = "abc";

then the JVM arranges for "abc" to be in that pool, and then produces a
reference to it when executing that code. When you write:

String a = new String("abc");

then the JVM still arranges for "abc" to be in that pool, and still
provides a reference to it, but that reference is then passed into a
constructor to create a NEW String object that's NOT in the pool and
copies the first one. It's that second String object which ends up
being assigned to the reference.

This is different from your statement above. If a String object exists
that contains the same contents -- abc -- but the object is NOT in the
pool (if, for example, it was read from a file or database), then the
literal "abc" will resolve to a new String object that's different from
that existing one.

It's also a little imprecise to says that the pooled String objects are
created by the code above. Instead, it's better to say that the code
above ensures that at some point, the JVM will arrange for the String
object to have been created when that code executes. However, that
object will be created only once, regardless of how many times the code
is executed, or even how many times the literal "abc" appears in the
code.


> Mostly the difference is seen with the equals method and '=='.
>


Yes. Use of '==' is, in fact, the only direct way to see the
difference. Indirectly, of course, you could observe the memory usage
of the application after a large number of repetitions of the code.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      11-18-2004

"Michael Borgwardt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bruce Sam wrote:
>
>> In my opinion,"String s = "abc";" has only created a reference s not a
>> object."String s= new String("abc");" has created a new object and its
>> reference is s.Is it right?

>
> Not really. Both declare a reference, and while the first doesn't
> explicitly *create* an object at runtime, there still *is* an Object.
> It's part of the constant pool of the class and created when the
> class is loaded.


The second creates two objects, since the constant String "abc" and the
results of "new String()" must be distinct.


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      11-19-2004

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
>
> Yes. Use of '==' is, in fact, the only direct way to see the
> difference. Indirectly, of course, you could observe the memory usage
> of the application after a large number of repetitions of the code.


At an intermediate level of directness, you could create a thread that locks
"abc" and observe that a different thread can lock the new-ed String.


 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      11-19-2004
Mike Schilling wrote:
>>Not really. Both declare a reference, and while the first doesn't
>>explicitly *create* an object at runtime, there still *is* an Object.
>>It's part of the constant pool of the class and created when the
>>class is loaded.

>
>
> The second creates two objects, since the constant String "abc" and the
> results of "new String()" must be distinct.


But the literal isn't created when the line is executed, it's created when
the class is loaded.
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      11-19-2004
"Mike Schilling" <(E-Mail Removed)> wrote in message
news:c09nd.23033$(E-Mail Removed). com...
>
> "Michael Borgwardt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Bruce Sam wrote:
> >
> >> In my opinion,"String s = "abc";" has only created a reference s not a
> >> object."String s= new String("abc");" has created a new object and its
> >> reference is s.Is it right?

> >
> > Not really. Both declare a reference, and while the first doesn't
> > explicitly *create* an object at runtime,


This is incorrect.
A String literal involves the runtime creation of an object.
In fact, two of the same String literals that are in different scopes may
create have separate objects created.
Here's the challenge for the reader: prove this beyond doubt (it can be
done).

> there still *is* an Object.
> > It's part of the constant pool of the class and created when the
> > class is loaded.

>


The 'constant pool' is an abstract concept that should not be thought of as
a physical thing - it simply does not physically exist.

> The second creates two objects, since the constant String "abc" and the
> results of "new String()" must be distinct.
>


Specifically, the object created by the new operator must not exist anywhere
else at that time.

--
Tony Morris
http://xdweb.net/~dibblego/



 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      11-19-2004
"Michael Borgwardt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Schilling wrote:
> >>Not really. Both declare a reference, and while the first doesn't
> >>explicitly *create* an object at runtime, there still *is* an Object.
> >>It's part of the constant pool of the class and created when the
> >>class is loaded.

> >
> >
> > The second creates two objects, since the constant String "abc" and the
> > results of "new String()" must be distinct.

>
> But the literal isn't created when the line is executed, it's created when
> the class is loaded.


The literal, being a constant (JLS 15.2, is "created" at compile-time.
The object that is used is created at runtime.

--
Tony Morris
http://xdweb.net/~dibblego/



 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      11-19-2004
Tony Morris wrote:
>>>>Not really. Both declare a reference, and while the first doesn't
>>>>explicitly *create* an object at runtime, there still *is* an Object.
>>>>It's part of the constant pool of the class and created when the
>>>>class is loaded.
>>>
>>>
>>>The second creates two objects, since the constant String "abc" and the
>>>results of "new String()" must be distinct.

>>
>>But the literal isn't created when the line is executed, it's created when
>>the class is loaded.

>
>
> The literal, being a constant (JLS 15.2, is "created" at compile-time.


Actually, at the time the source code is written.

> The object that is used is created at runtime.


I meant "the object represented by the literal".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
driver irql not less or equal =?Utf-8?B?Y2VudHVyaWFu?= Wireless Networking 2 03-11-2005 08:30 PM
Load sharing between links where AS path length is equal Andrew Gideon Cisco 5 03-01-2005 11:55 PM
How to allocate an equal share of incoming web traffic to every user Anton Panyushkin Cisco 4 11-23-2004 02:41 PM
Re: OSPF Equal cost balancing in serie 2500 routers Aaron Woody Cisco 0 10-20-2003 02:42 PM



Advertisments