Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

difference between String variable and String class definition

Thread view: 
vahid.xplod@gmail.com - 27 Mar 2006 22:42 GMT
hi ,
can anyone tell me what is difference between String variable and
String class definition.
for example :

String variable ---> String = "java";
String Class ---> String = new String("java");

in two example above, i want to know that every two definition are same
as each other about memory allocation or not ?
Roedy Green - 27 Mar 2006 23:27 GMT
>String variable ---> String = "java";
>String Class ---> String = new String("java");

the second makes an extra copy of the string "java". The first points
to the interned copy possibly already extant. There is almost never a
need for the second form.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Eric Sosman - 27 Mar 2006 23:43 GMT
vahid.xplod@gmail.com wrote On 03/27/06 16:42,:
> hi ,
> can anyone tell me what is difference between String variable and
[quoted text clipped - 6 lines]
> in two example above, i want to know that every two definition are same
> as each other about memory allocation or not ?

   The two samples above have identical effect: They
both cause the Java compiler to emit an error message
and reject the program ...

   I know you're trying to ask something, but I'm not
at all sure what it is you're asking.  This is only a
guess at what your question might be; if I've guessed
wrong, please try phrasing your question more precisely.

   Perhaps you are asking about

    String x = "java";
    String y = "java";
    String z = new String("java");

   The first line creates "by magic" a String whose
value consists of the four characters j a v a, and causes
the reference variable x to point to that String.  The
second line does the same, and in fact y will point to
the exact same String instance x does.  One String object,
two reference variables both pointing to it.  Clear so far?

   The third line gets hold of that same four-letter
String and passes it to a constructor as an argument.
The constructor does its duty, which is to create and
initialize a brand-new String object, and the reference
variable z is set to point to this new String.  This
particular constructor initializes its new String by
copying the contents of an existing String, so the new
String will also contain the four letters j a v a -- but
it is not the same String that x and y point to.  It is
a distinct String that just happens to have the same
content as the original.  (There are five pennies in my
pocket and five pennies in your pocket; the pockets'
contents are equal but the pockets are distinct.)

   You can demonstrate this odd state of affairs by
printing the results of a few simple tests:

    System.out.println(x.equals(y) + "\t" + (x == y));
    System.out.println(x.equals(z) + "\t" + (x == z));
    System.out.println(y.equals(z) + "\t" + (y == z));

Try to predict the output, then compile it and run it and
see whether you were right.

   ... and if that's not what you were asking about,
try asking again.

Signature

Eric.Sosman@sun.com

vahid.xplod@gmail.com - 28 Mar 2006 18:33 GMT
consider to this code below :

String x = "java";
String y = "java sun";

i want to know that is there a difference between x and y ?, i mean
that x and y point to different places in memory and also memory is
allocated two different palce in memory for these two variables. or
these two variables point to same place in memory in another way again.
Eric Sosman - 28 Mar 2006 18:58 GMT
vahid.xplod@gmail.com wrote On 03/28/06 12:33,:
> consider to this code below :
>
[quoted text clipped - 5 lines]
> allocated two different palce in memory for these two variables. or
> these two variables point to same place in memory in another way again.

   There are two String objects, one containing four
characters and pointed to by x, the other containing
eight characters and pointed to by y.  The JVM might
store all twelve characters explicitly, or it might
be clever enough to store just eight characters and
"recycle" four of them.  That's the JVM's business,
not yours.  In fact, there's no way your code can tell
how it stores the data characters, short of using nasty
reflective tricks to examine the internals of the
String class.  (And any conclusion you might draw from
such an examination might not hold for the next JVM
your code runs on.)

   No matter how the JVM stores the characters, it
will still be the case that x==y is false: the two
String objects are distinct even if they happen to
share some resources "under the covers."

Signature

Eric.Sosman@sun.com

Roedy Green - 28 Mar 2006 20:30 GMT
>String x = "java";
>String y = "java sun";
[quoted text clipped - 3 lines]
>allocated two different palce in memory for these two variables. or
>these two variables point to same place in memory in another way again.

there will be a char[] objects containing the letters j a v a
The string object  pointed to by x by will point to it.

There will be another char[] object containing the letter j a v a   s
u n

The string object pointed to by y will point to it.

This will all be much clearer if you study the source code for String
in src.zip

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Martin Gerner - 29 Mar 2006 13:51 GMT
Eric Sosman <Eric.Sosman@sun.com> wrote in news:e09pql$cjg$1
@news1brm.Central.Sun.COM:

>      String x = "java";
>      String y = "java";
[quoted text clipped - 5 lines]
> the exact same String instance x does.  One String object,
> two reference variables both pointing to it.  Clear so far?

How come x and y reference the same object here? Why not two different
objects? And what is the purpose for the JVM to create only one object?
Kenneth P. Turvey - 29 Mar 2006 14:49 GMT
> How come x and y reference the same object here? Why not two different
> objects? And what is the purpose for the JVM to create only one object?

String is immutable so it doesn't matter if it points to the same
reference or not, you won't be able to change it either way.  It saves a
bit of memory by using the same instance, and you can do

if (x == y) {
..

and it will do what you expect.  

- --
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>

XMPP  IM: kpturvey@jabber.org
Yahoo IM: kpturvey2
Oliver Wong - 29 Mar 2006 16:25 GMT
> Eric Sosman <Eric.Sosman@sun.com> wrote in news:e09pql$cjg$1
> @news1brm.Central.Sun.COM:
>
>>      String x = "java";
>>      String y = "java";

[...]

> How come x and y reference the same object here? Why not two different
> objects? And what is the purpose for the JVM to create only one object?

   That's just the way the language was designed. Possibly they did this to
save memory, but the only people who'll know for sure are the language
designers at Sun themselves.

   - Oliver
Roedy Green - 29 Mar 2006 19:04 GMT
On Wed, 29 Mar 2006 12:51:40 +0000 (UTC), Martin Gerner
<martin.gerner@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>How come x and y reference the same object here? Why not two different
>objects? And what is the purpose for the JVM to create only one object?

see http://mindprod.com/jgloss/interned.html

It saves ram.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

weironghai@gmail.com - 28 Mar 2006 05:25 GMT
i my opinion.the 2nd String b=new String("hhhh"); made a copy of
"hhhh".
do this code
 String a="hhhh";
String b="hhhh";
String c=new("hhhh");

? a==b  .it will print true
but b==c,it will print false;

because a and b have the same Quote
Roedy Green - 28 Mar 2006 07:21 GMT
On 27 Mar 2006 20:25:20 -0800, "weironghai@gmail.com"
<weironghai@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>i my opinion.the 2nd String b=new String("hhhh"); made a copy of
>"hhhh".
[quoted text clipped - 7 lines]
>
>because a and b have the same Quote

that demonstrates there is a new String object, but does not prove
there the internal char[] is not shared.  You might be able to
determine that by allocating big strings and measuring free space.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Patricia Shanahan - 28 Mar 2006 07:25 GMT
> On 27 Mar 2006 20:25:20 -0800, "weironghai@gmail.com"
> <weironghai@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 15 lines]
> there the internal char[] is not shared.  You might be able to
> determine that by allocating big strings and measuring free space.

It's much easier to just read the String source code. If the size of the
new string is less than the length of the char[], the needed piece of
the char[] is copied to a new one. If the char[] length and the String
size are the same, the new String just references the char[] from the
old one.

Patricia
Roedy Green - 28 Mar 2006 18:26 GMT
>It's much easier to just read the String source code.

An even easier way would be to read the String and substring entries
in the Java glossary.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.