File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String , number of objects

 
sparsh khandelwal
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and how s1 and s resembles ?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sparsh khandelwal
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String LITERALS are automatically placed in the string pool.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic