Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String literal and String Object

Reply
Thread Tools

String literal and String Object

 
 
Prakash Prabhu
Guest
Posts: n/a
 
      08-27-2003
Hi,

Is there any performance benefit of using a String literal instead
of creating a new String Object , like , for eg :

String str1 = "Hello I am here "; //String literal

instead of

String str3 = new ("Hello I am here "); //String Object

Don't both str1 and str2 point to objects on the heap , since in Java
most of the binding is done at run time?

Thanks,
Prakash

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      08-27-2003
"E.C. Bäck" <(E-Mail Removed)> scribbled the following:
> String are weird objects. As far as I know,


> str1 = "Thing"; and
> str2 = new String("Thing");


> are very different. The former makes one new string object and assigns a
> reference to str1. The latter makes a new string object, twice, and assigns
> a reference to str2. Strings are immutable; don't make too many of them.


The new operator *ALWAYS* creates a new instance, which takes up space
independently from the other instances, no matter what those instances
are.
If you type:

String[] foo = new String[1000];
for (int i=0; i<1000; i++) foo[i]=new String("Thing");

you are guaranteed to end up with 1000 different String objects. If, on
the other hand, you type:

String[] foo = new String[1000];
for (int i=0; i<1000; i++) foo[i]="Thing";

then depending on the JVM settings, you might end up with only one
String object, being referred to by 1000 references.

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
Reply With Quote
 
 
 
 
??????
Guest
Posts: n/a
 
      08-27-2003
literal will point to the same object ...
new will create a new object although the word is the same ...
so ... you should use new only you want to ...

"Prakash Prabhu" <(E-Mail Removed)> wrote in message
news:qS43b.17$(E-Mail Removed)...
> Hi,
>
> Is there any performance benefit of using a String literal instead
> of creating a new String Object , like , for eg :
>
> String str1 = "Hello I am here "; //String literal
>
> instead of
>
> String str3 = new ("Hello I am here "); //String Object
>
> Don't both str1 and str2 point to objects on the heap , since in Java
> most of the binding is done at run time?
>
> Thanks,
> Prakash
>



 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      08-27-2003
Joona I Palaste wrote:
> you are guaranteed to end up with 1000 different String objects. If, on
> the other hand, you type:
>
> String[] foo = new String[1000];
> for (int i=0; i<1000; i++) foo[i]="Thing";
>
> then depending on the JVM settings, you might end up with only one
> String object, being referred to by 1000 references.


If you get anything different in this case then your JVM is out of
compliance with the JLS (section 3.10.5).


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
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
convert string literal to object attribute BiraRai Python 2 10-31-2008 07:18 PM
OOP, object literal notation, and the 'new' operator - how to have multiple child objects of a parent object? iporter Javascript 4 10-19-2007 07:20 AM
compare string and "" string literal lovecreatesbea...@gmail.com C++ 10 01-24-2007 09:55 PM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
string literal as const string& parameter selder21@hotmail.com C++ 5 09-22-2003 11:50 AM



Advertisments