For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2006 > difference between String variable and String class definition









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author difference between String variable and String class definition
vahid.xplod@gmail.com

2006-03-27, 10:01 pm

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

2006-03-27, 10:01 pm

On 27 Mar 2006 13:42:58 -0800, vahid.xplod@gmail.com wrote, quoted or
indirectly quoted someone who said :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Eric Sosman

2006-03-27, 10:01 pm



vahid.xplod@gmail.com wrote On 03/27/06 16:42,:
> 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 ?


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.

--
Eric.Sosman@sun.com

weironghai@gmail.com

2006-03-28, 4:07 am

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

2006-03-28, 4:07 am

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".
> 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


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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Patricia Shanahan

2006-03-28, 4:07 am

Roedy Green wrote:
> On 27 Mar 2006 20:25:20 -0800, "weironghai@gmail.com"
> <weironghai@gmail.com> wrote, quoted or indirectly quoted someone who
> said :
>
>
>
>
> 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.


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

2006-03-28, 7:04 pm

On Tue, 28 Mar 2006 06:25:52 GMT, Patricia Shanahan <pats@acm.org>
wrote, quoted or indirectly quoted someone who said :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
vahid.xplod@gmail.com

2006-03-28, 7:04 pm

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

2006-03-28, 7:04 pm



vahid.xplod@gmail.com wrote On 03/28/06 12:33,:
> 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.


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."

--
Eric.Sosman@sun.com

Roedy Green

2006-03-28, 7:04 pm

On 28 Mar 2006 09:33:03 -0800, vahid.xplod@gmail.com wrote, quoted or
indirectly quoted someone who said :

>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.


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

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

2006-03-29, 8:02 am

Eric Sosman <Eric.Sosman@sun.com> wrote in news:e09pql$cjg$1
@news1brm.Central.Sun.COM:

> String x = "java";
> String y = "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?


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

2006-03-29, 8:02 am

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 29 Mar 2006 12:51:40 +0000, Martin Gerner wrote:

> 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFEKpB2i2ZgbrTULjoRAq6JAJ9OMdi3XUBa
iMkRJbDqxLUXzCFSXQCgiGRz
LGqZRvxCWT6jFfPINybvz9M=
=CN3l
-----END PGP SIGNATURE-----

Oliver Wong

2006-03-29, 7:04 pm


"Martin Gerner" <martin.gerner@gmail.com> wrote in message
news:Xns9795972F9FA55martindotgerneratgm
a@129.16.222.141...
> Eric Sosman <Eric.Sosman@sun.com> wrote in news:e09pql$cjg$1
> @news1brm.Central.Sun.COM:
>
[...][color=darkred]
>
> 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

2006-03-29, 7:04 pm

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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com