Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Difference between String variable and String Class definition

Reply
Thread Tools

Difference between String variable and String Class definition

 
 
vahid.xplod@gmail.com
Guest
Posts: n/a
 
      03-27-2006
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 ?

 
Reply With Quote
 
 
 
 
Duane Evenson
Guest
Posts: n/a
 
      03-27-2006
On Mon, 27 Mar 2006 13:41:45 -0800, vahid.xplod wrote:

> 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 first line allocates memory for the literal "java". It then points the
variable to this location.
The second line allocates space for the literal. It then creates a new
String class. It copies the string from the first place to the new place
in memory. Finally, it points the variable to that location.
All you do in the second example is create more work for the computer
and use up more memory (until the next garbage collection).

Consider what
String var - new String(new String(new String("java")))
would do.
....
A series of memory allocations would occur with "java" being copied
from one place to the next.

 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-28-2006
Duane Evenson wrote:
....
> Consider what
> String var - new String(new String(new String("java")))
> would do.
> ...
> A series of memory allocations would occur with "java" being copied
> from one place to the next.
>


I've taken a look at the String source code, and in practice the actual
character array containing 'j', 'a', 'v', 'a' will not be copied, but
shared by the series of String objects, because the String needs the
whole of the character array.

This is, of course, an implementation detail, not part of the interface.

Patricia
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-28-2006
On Mon, 27 Mar 2006 23:34:41 GMT, Duane Evenson <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>It then creates a new
>String class.


you mean a new string Object.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Tom Fredriksen
Guest
Posts: n/a
 
      03-29-2006
Duane Evenson wrote:
> On Mon, 27 Mar 2006 13:41:45 -0800, vahid.xplod wrote:
>>
>> 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 first line allocates memory for the literal "java". It then points the
> variable to this location.
> The second line allocates space for the literal. It then creates a new
> String class. It copies the string from the first place to the new place
> in memory. Finally, it points the variable to that location.


Are you saying that the first example does not produce a String object
with the data "java" as its content? I would think it does, doesn't it?
So basically that means that the second example just creates an
additional object which it uses to set up the first object. I would have
thought its was syntactic sugar, nothing more. How one can be fooled.

But a question then is why the need for two seemingly similar
statements, but which has different effects. I do conceive there is a
reason, but is there a real need? generally I see many such things as
just an attempt to be clever than a real need, I could be wrong though.

/tom

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      03-29-2006


Tom Fredriksen wrote On 03/29/06 18:05,:
> Duane Evenson wrote:
>
>>On Mon, 27 Mar 2006 13:41:45 -0800, vahid.xplod wrote:
>>
>>>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 first line allocates memory for the literal "java". It then points the
>>variable to this location.
>>The second line allocates space for the literal. It then creates a new
>>String class. It copies the string from the first place to the new place
>>in memory. Finally, it points the variable to that location.

>
>
> Are you saying that the first example does not produce a String object
> with the data "java" as its content? I would think it does, doesn't it?


Up to this point, I think I understand your question and
can answer it. The whole process goes something like this:
The compiler sees the literal "java" in the source code, and
generates a corresponding string constant in the class file.
When the class gets loaded, the JVM takes that string constant
and makes a String object out of it. The JVM also arranges
to make just one String object per unique string constant
value, so if you write "java" several times (perhaps in several
different .java files), the JVM folds them all together into
just one String object with the value "java". When the code
is executed (well, the code as given won't even compile, but
let's imagine that we've fixed it), it sets the value of a
reference variable to point to this String object.

All this verbiage is in response to the word "produce,"
because it seems you're puzzled by where things come from.
The important points: All identical literals get turned into
a single String object by the JVM, and an assignment like
`refvar = "java"' causes the reference variable to refer
to that String object. You don't get a new String each time
the assignment is executed; you just keep recyling the old one.

> So basically that means that the second example just creates an
> additional object which it uses to set up the first object. I would have
> thought its was syntactic sugar, nothing more. How one can be fooled.


This part baffles me; I don't know what you mean.

> But a question then is why the need for two seemingly similar
> statements, but which has different effects.


Maybe similarity is in the eye of the beholder, but the
two don't look very similar to me. One of them uses the
`new' operator and passes an argument to a constructor, the
other does not. `x = y' and `x = new X(y)' look different
to me, and I'm not surprised they do different things.

> I do conceive there is a
> reason, but is there a real need? generally I see many such things as
> just an attempt to be clever than a real need, I could be wrong though.


"Is there a real need" ... for what? There's certainly
a need for the `new' operator, if that's the question. I
suppose you could design an O-O language without constructors
by using only factory methods, but I think it would be pretty
clumsy, so perhaps constructors count as "needed," too.

The String(String) "copy constructor" doesn't seem to be
very useful. It may have some use as a space optimization
when extracting short substrings from long containing strings
whose remains will be discarded, and it may have use in some
esoteric circumstances where you are using String values as
"tokens" that will never be == to each other even if their
contents are identical. Maybe its principal use is as the
basis of "Gotcha!" questions in Java exams ...

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

 
Reply With Quote
 
Tom Fredriksen
Guest
Posts: n/a
 
      03-30-2006
Eric Sosman wrote:
>
>> Tom Fredriksen wrote On 03/29/06 18:05,:

>
> All this verbiage is in response to the word "produce,"
> because it seems you're puzzled by where things come from.
> The important points: All identical literals get turned into
> a single String object by the JVM, and an assignment like
> `refvar = "java"' causes the reference variable to refer
> to that String object. You don't get a new String each time
> the assignment is executed; you just keep recyling the old one.
>
>> So basically that means that the second example just creates an
>> additional object which it uses to set up the first object. I would have
>> thought its was syntactic sugar, nothing more. How one can be fooled.

>
> This part baffles me; I don't know what you mean.


Sorry for being a bit unclear, what I mean is:
Are both statements semantically correct, meaning are the first
statement just a short form of the second. In other words, do they
produce the same byte code?

Half of the answer seems to be already given, the second produces two
string objects, while the first only produces one? So except for that
they are then the same, or am I missing something here?

Lets assume I have got it right, then the question was why dont they
both compile to the bytecode of the first statement? This was the point
of seemingly similar, as in similar byte code, not syntax.

Hope that clear it up a bit.


/tom
 
Reply With Quote
 
James McGill
Guest
Posts: n/a
 
      03-30-2006
On Mon, 2006-03-27 at 13:41 -0800, (E-Mail Removed) wrote:
>
> in two example above, i want to know that every two definition are
> same
> as each other about memory allocation or not ?
>


Sun javac appears to intern the string "java" in the class file, so the
constuctor for the new String("java") version, also uses the same text
"java" from the first declared version. Also, a constructor is called
for the second version, but not the first.

So there's something going on with String var = "java"; that's
distinct from String var = new String("java");

It bothers me a little that the javadoc for String says:


String str = "abc";


is equivalent to:

char data[] = {'a', 'b', 'c'};
String str = new String(data);



In a practical sense they are equivalent, but the resulting bytecode is
different. Does it matter?



 
Reply With Quote
 
James McGill
Guest
Posts: n/a
 
      03-30-2006
On Mon, 2006-03-27 at 23:34 +0000, Duane Evenson wrote:
>
> Consider what
> String var - new String(new String(new String("java")))
> would do.


It will make me check the bytecode, and see that sure enough, the
compiler isn't smart enough to optimize this out

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-30-2006
On Wed, 29 Mar 2006 18:39:31 -0700, James McGill
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>In a practical sense they are equivalent, but the resulting bytecode is
>different. Does it matter?


It only matters if for some strange reason you want two distinct
String objects. I have never run into a practical case where interning
would hurt anything. The biggest problem is using == and having it
work. You get lulled into trusting it. IntelliJ inspector warns you
of any == use on Strings. It works MOST of the time. But you can only
trust it if you are sure all the strings you are comparing are
interned.

See http://mindprod.com/jgloss/interned.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
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
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
behavior difference for mutable and immutable variable in function definition jianbing.chen@gmail.com Python 5 05-05-2007 08:05 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
Difference between inline initialization and definition. Gianni Mariani C++ 4 12-07-2003 12:00 AM



Advertisments