Creating New Strings
Earlier we promised to talk more about the subtle differences between the various methods of creating a String. Let's look at a couple of examples of how a String might be created, and let's further assume that no other String objects exist in the pool:
String s = "abc"; // creates one String object and one
// reference variable
In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc"); // creates two objects,
// and one reference variable
In this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.
Tell the difficulties that i am difficult.
That is not what the article says at all.saloni jhanwar wrote:. . . hence s1==s2 should be true . . .
Campbell Ritchie wrote:
That is not what the article says at all.saloni jhanwar wrote:. . . hence s1==s2 should be true . . .
Tell the difficulties that i am difficult.
Campbell Ritchie wrote:That is not what the article says at all.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Ralph Cook wrote:Try thinking of it this way: s1 is an object that contains a reference to "abc". s2 is a reference to "abc".
The == operator only returns true if s1 and s2 point to the same object, and they do not. Both these objects do have the same value.
Tell the difficulties that i am difficult.
saloni jhanwar wrote:I know how equals and == works very well...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
saloni jhanwar wrote:I know how equals and == works very well...
I'm afraid that clearly you don't, because new String("abc") is not == "abc", for the reasons that Ralph tried to explain.
Winston
Tell the difficulties that i am difficult.
Tell the difficulties that i am difficult.
saloni jhanwar wrote:Ok now what i am getting i am writing here String s1=new String("abc") creates two objects first on heap another on pool and both objects haven't any relation except than same content.now String s2="abc" it will look for literal "abc" in pool as because of there is a literal "abc" in pool provided by first statement so s2 will refer that second object on pool not first object on heap.Am i correct ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Well, first: All objects are created on the heap. The "pool" is simply a structure (very possibly a HashMap of some sort) that contains references to the literals defined in your source code. This is simply a convenience to so that duplicated literals don't result in multiple objects being created.
However, whenever you use the 'new' keyword, Java always creates a new object, regardless of type; and a new object will never be '==' to any other.
Winston
Tell the difficulties that i am difficult.
Winston Gutkowski wrote:
Campbell Ritchie wrote:That is not what the article says at all.
I have to admit, this business of caching Strings seems to cause more problems than it solves.
So as to avoid any confusion caused by this discussion going on here and on that other thread referred to, I am closing that other thread. I might re‑open it if I remember.saloni jhanwar wrote: . . . You are wrong in in your post.
Seetharaman Venkatasamy wrote:disagree, sorry . confusion will be there even at simple thing. if there is no pool concept, there could be a lot of redundant objects...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Are you quite sure about that? A String in the constant pool and a String elsewhere on the heap are identical in everything except location.Nitish Bijalwan wrote: . . . * First of all object in string pool is different from java object in heap.
I think the String "abc" is loaded into the String pool when the class is loaded. The assignment to s occurs later, when the code is executed.* When we write String s ="abc" ,the string object is created in string pool
This assumes s has not been re‑assigned. The compiler cannot check that s is not re‑assigned, but if s is marked final, it becomes a compile‑time constant.. . . If i write String s2 = "abc" then s2 will refer to the same object as s is refering.
Nitish Bijalwan wrote:Welcome to Java Ranch family saloni jhanwar.
Actually you didn't get what she[Kathy] had tried to said.
* First of all object in string pool is different from java object in heap.
* When we write String s ="abc" ,the string object is created in string pool
* when we write String s1 = new String("abc") a string object is create on java heap. So both refer to two different
object.
* Now if I write String S2 = new String("abc") another new object created but If i write
String s2 = "abc" then s2 will refer to the same object as s is refering.
Tell the difficulties that i am difficult.
saloni jhanwar wrote:and by the way i got what she said...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Don’t teach your grandmother to suck eggs.saloni jhanwar wrote: . . . Do you understand importance of statements sequence ? . . ..
Campbell Ritchie wrote:
Don’t teach your grandmother to suck eggs.saloni jhanwar wrote: . . . Do you understand importance of statements sequence ? . . ..
As a general rule, [S; T]Q ≠ [T; S]Q, but the frames of the two statements are disjoint, so it just so happens that in the code you quoted, the two statement pairs do turn out to be equivalent to each other. In generalised substitution language, those two statement pairs can be reduced to the multiple assignmentAs I said, that only works because the frames of the two statements are disjoint, and they are in fact completely independent of each other. Were there any relation between the two statements, then changing the sequence would almost certainly not work. Because "abc" is a literal, its double use does not constitute a relationship between the two statements. In fact, in GSL, its very existence is not essential to the correct operation of those two statements. As Winston said, it is only an implementation detail.
Tell the difficulties that i am difficult.
Campbell Ritchie wrote:
Don’t teach your grandmother to suck eggs.saloni jhanwar wrote: . . . Do you understand importance of statements sequence ? . . ..
Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Campbell Ritchie wrote:
Don’t teach your grandmother to suck eggs.saloni jhanwar wrote: . . . Do you understand importance of statements sequence ? . . ..
Tell the difficulties that i am difficult.
Aniruddh Joshi wrote:
Campbell Ritchie wrote:
Don’t teach your grandmother to suck eggs.saloni jhanwar wrote: . . . Do you understand importance of statements sequence ? . . ..
saloni jhanwar wrote:
Campbell Ritchie wrote:
Don’t teach your grandmother to suck eggs.saloni jhanwar wrote: . . . Do you understand importance of statements sequence ? . . ..
Yes, i know you are grandmother but you should look carefully i did quote a greenhorn not you grandmother.
Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
saloni jhanwar wrote:The matter was how String objects are being created on pool and heap when these two statements execute in different order that's it.And these two statement will behave totally different while they will execute,and this matter i am telling him.Is anything wrong in it ?
Stuart A. Burkett wrote:
saloni jhanwar wrote:The matter was how String objects are being created on pool and heap when these two statements execute in different order that's it.And these two statement will behave totally different while they will execute,and this matter i am telling him.Is anything wrong in it ?
As Campbell has said, those two lines will behave exactly the same. A String object containing the value "abc" will be created in the String pool when the class is loaded. That string will exist even if those two lines of code are never executed.
Therefore the line will simply cause s to reference the already existing String from the pool and the line will create a new String object which will be referenced by s1 and will have the same value as the String in the pool.
Therefore the order of excution is irrelevant
Stuart A. Burkett wrote:That string will exist even if those two lines of code are never executed.
Tell the difficulties that i am difficult.
Gaurangkumar Khalasi wrote:I think there is a misinterpretation of the statement: " Java will create a new String object(for String s = new String("abc");) in normal (non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool"; given in the K&B.
If you will read it carefully, you can realize that s will only refer to the object created at normal(heap) memory and not to the literal placed in the pool.
So, for the following example:
and for another case
Tell the difficulties that i am difficult.
saloni jhanwar wrote:s1 and s are not static variables here
saloni jhanwar wrote:String s ="abc"; statement will look first for "abc" literal on pool but as there is no any presently ,so it will create an object s on pool and will do place a "abc" literal on pool.
Stuart A. Burkett wrote:
saloni jhanwar wrote:s1 and s are not static variables here
Irrelevant.
You said
saloni jhanwar wrote:String s ="abc"; statement will look first for "abc" literal on pool but as there is no any presently ,so it will create an object s on pool and will do place a "abc" literal on pool.
That is wrong.
If the classloader finds any String literals (which is what "abc" is) in the class code when it loads it, it will create a String object in the string pool during the loading of the class. Therefore before any line of code that uses the literal "abc" is excuted, the String will already exist in the string pool.
Tell the difficulties that i am difficult.
saloni jhanwar wrote:
Stuart A. Burkett wrote:
saloni jhanwar wrote:s1 and s are not static variables here
Irrelevant.
You said
saloni jhanwar wrote:String s ="abc"; statement will look first for "abc" literal on pool but as there is no any presently ,so it will create an object s on pool and will do place a "abc" literal on pool.
That is wrong.
If the classloader finds any String literals (which is what "abc" is) in the class code when it loads it, it will create a String object in the string pool during the loading of the class. Therefore before any line of code that uses the literal "abc" is excuted, the String will already exist in the string pool.
so what i were saying ? are you telling something different ? and what is wrong you are also saying same thing. and it doesn't make any sense that it will create at loading phase or not.that is irrelevant.
Henry Wong wrote:
saloni jhanwar wrote:
Stuart A. Burkett wrote:
saloni jhanwar wrote:s1 and s are not static variables here
Irrelevant.
You said
saloni jhanwar wrote:String s ="abc"; statement will look first for "abc" literal on pool but as there is no any presently ,so it will create an object s on pool and will do place a "abc" literal on pool.
That is wrong.
If the classloader finds any String literals (which is what "abc" is) in the class code when it loads it, it will create a String object in the string pool during the loading of the class. Therefore before any line of code that uses the literal "abc" is excuted, the String will already exist in the string pool.
so what i were saying ? are you telling something different ? and what is wrong you are also saying same thing. and it doesn't make any sense that it will create at loading phase or not.that is irrelevant.
Saloni,
Stuart is just trying to correct you -- admittedly on a subtle point, but he is correct.
And whether or not you are / were saying the same thing, your previous post did come across as otherwise.
Henry
Tell the difficulties that i am difficult.
Stuart A. Burkett wrote:So do you have your answer ? If not, you may want to ask it again (possibly in a new thread) as it seems to have got lost in this thread.
Tell the difficulties that i am difficult.
saloni jhanwar wrote:...and important thing is here that these both statements will execute together at same time not only literal statement.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
saloni jhanwar wrote:...and important thing is here that these both statements will execute together at same time not only literal statement.
No they won't. They will be executed sequentially, and in the order they appear. What's almost certain though is that "abc" will be loaded into the pool before either statement is executed (indeed, very possibly before anything is initialized).
Winston
Tell the difficulties that i am difficult.
saloni jhanwar wrote:but i would like to tell whole story.
String s1="abc";
String s=new String("abc");
you told when class loader will find any String literal in class then class loader will load that literal on pool and an object would be instantiate.fine
but what about second half story about next statement ? no one is telling second half.You all are repeat half story.
Stuart A. Burkett wrote:A String object containing the value "abc" will be created in the String pool when the class is loaded. That string will exist even if those two lines of code are never executed.
Therefore the line will simply cause s to reference the already existing String from the pool and the line will create a new String object which will be referenced by s1 and will have the same value as the String in the pool.
Therefore the order of excution is irrelevant
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|