aspose file tools*
The moose likes Java in General and the fly likes String , number of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String , number of objects" Watch "String , number of objects" New topic
Author

String , number of objects

sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 74

hello everyone,
starting strings..
face this problem
how this code works , and what are the number of objects formed
what is the similarity between s and s1
and is there any way to know the number of objects ?
please help
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sparsh khandelwal wrote:
how this code works , and what are the number of objects formed


What do you think, and why?


and is there any way to know the number of objects ?


In general, no. And we don't need to know.
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 74
thanks for your reply
i am not confident , but it should be two , one in pool and other one in non pool
but don't get the concept of pool and non pool here

sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 74
and how s1 and s resembles ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

sparsh khandelwal wrote:and how s1 and s resembles ?

The only relation between them that I can see is that the two strings happen to store the same sequence of characters.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 74
fred rosenberger wrote:
sparsh khandelwal wrote:and how s1 and s resembles ?

The only relation between them that I can see is that the two strings happen to store the same sequence of characters.

i have doubt that might be both the string represents same object in String pool ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

any time you use the "new" operator/keyword/whatever it is, an object gets created in the heap. so s1 will point to the String in the heap containing "xyz". s will point to the String in the String pool that contains "xyz".

They are different objects, which you can test by using the == operator. They will NOT be equal, proving they are different objects.
(unless there is some kind of crazy optimization that goes on, which I don't believe happens)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sparsh khandelwal wrote:thanks for your reply
i am not confident , but it should be two , one in pool and other one in non pool


Yes.

To add a bit more detail...

When the class is loaded, any of its string constants that aren't already in the constant pool are placed there.

Executing the "s=" line gets a reference to the "xyz" String in the pool and assigns that reference to the variable.

Executing the "s1=" line also gets a reference to the "xyz" String in the pool. It then uses the new operator to create a new String object, and passes that reference to a String constructor. So a new String object is created that has the same sequence of characters as the String object in the constant pool. A reference to that new String is assigned to the s1 variable.

but don't get the concept of pool and non pool here


Any compile-time String constants in our class is placed into the pool when that class it loaded (if an equal String is not already in the pool). Additionally, and String on which we call intern() is placed in the pool (if an equal String is not already there).
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 74
String on which we call intern() is placed in the pool (if an equal String is not already there).

if we declare any string then automatically it should be in the pool ?
then what is the use of intern
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

String LITERALS are automatically placed in the string pool.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sparsh khandelwal wrote:
String on which we call intern() is placed in the pool (if an equal String is not already there).

if we declare any string then automatically it should be in the pool ?


No, that's not what I said. I don't know where you got that. I said all compile-time constant Strings go into the pool. The vast majority of Strings most programs deal with are not compile-time constants.

then what is the use of intern


It's for when you want to put a String that's not a compile-time constant into the pool. (Although in 15 years of Java programming I don't think I've ever used it.)
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 74

It's for when you want to put a String that's not a compile-time constant into the pool.

which String is compile time constant ?
might be the one which is given in the program itself ..
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sparsh khandelwal wrote:

It's for when you want to put a String that's not a compile-time constant into the pool.

which String is compile time constant ?


Roughly speaking, it's any String that is in your source code as a String literal, or constructed from only String literals and other compile-time constants, not requiring any runtime method calls to construct. The JLS has the formal definition.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String , number of objects