Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   String literal and String Object (http://www.velocityreviews.com/forums/t126091-string-literal-and-string-object.html)

Prakash Prabhu 08-27-2003 04:17 PM

String literal and String Object
 
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


Joona I Palaste 08-27-2003 04:36 PM

Re: String literal and String Object
 
"E.C. Bäck" <ecb29@cornell.edu> 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 (palaste@cc.helsinki.fi) ---------------------------\
| 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

?????? 08-27-2003 05:10 PM

Re: String literal and String Object
 
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" <prakash_prabhu_java@yahoo.com> wrote in message
news:qS43b.17$84.170@news.oracle.com...
> 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
>




John C. Bollinger 08-27-2003 09:18 PM

Re: String literal and String Object
 
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
jobollin@indiana.edu




All times are GMT. The time now is 02:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.