Jd Sierra

Greenhorn
+ Follow
since Jul 09, 2011
Jd likes ...
Eclipse IDE Java ME Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jd Sierra

Fred, nice explanation.

So,
after following statements,


total FOUR objects will be created. One after line 1, One more after line 2 and Two more after line 3. am I?
11 years ago

Ankur Kapoor wrote:

2. Now lets take a scenario where you would have used new String() pre String literal created in a POOL

String myString = new String("fun"); // this shall create a New Object in HEAP but not in POOL . [OBJECT CREATED] hence creating only single Object that is in HEAP

3. Now let's use intern() function of String to make the String Objects created using new operator available in the POOL

String myString = new String("fun").intern() // this shall create a New Object in HEAP and also place it in the POOL . [OBJECT CREATED]



I am not getting above statements because it contradicats with article of Strings in this forum as well as Book by Kathy Sierra. OR may be the reason is I am misunderstood somewhere and not getting the actual thing. So, here I am clearing all things and tell me where I am wrong with valid reason.

I would like to start with statements of Kathy Sierra's Book on page no. 434.

Kathy Sierra wrote:

In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, literal "abc" will be placed in the pool.



Now, I am concluding following things. Tell me where I am wrong.
Consider each and every of the following scenarios is independent and has no relationship with previous scenario.

1)

Only one object of String literal "abc" will be created here and it will be referred from String Literal Pool Table as well as it will be referred by Reference variable s.


2)

Here due to new keyword, one String object will be created in heap. Now we are passing String literal "def" to the String constructor. So, again one object of String literal "def" will be created and it will be referred from Strin Literal Pool Table just like in previous case but as we used new operator here, one extra object is created containg same literal "def" but it will not be reference from String Literal Pool and will be eligible for Garabage Collection. So, on the whole two objects are created in this case.

3)

now I want to know is how many object will be created in this case just and only because of line no.2?
After the end of first line, one object will be created as I mentioned in case 1.
Because of 2nd line, definately one object will be created containing String literal "ghi" due to new keyword which will not be referred from Strin Literal Pool Table. But I think here unlike in case 2, one more object containg String literal "ghi" will not be created which we are passing in String constructor because it's alreday available after line 1.
So, answer is only one new object is created just and only because of line no.2

4)
anybody can argue after reading my above statements that what is the use of intern() method when String is definately going to be referred from String Literal Table whether we use new keyword or not. I have following reasons for this:

i> If we have passed String literal while creating String using new operator, then there will be two objects in Heap containg same strings.One will be referred by reference variable and one will be referred from String Literal Pool. Now, if we apply intern() method on reference variable, then reference variable will also refer to the object which is being referred from String Literal Pool Table and another object will be eligible for garbage collection

ii> Another case where intern() method can be really helpful is when we do not pass String literal in String constructor and make String using array of characters or using any other valid arguments. At this time, as we have not passed String literal in an argument, only one object will be created which will not be permanent but after intern() method, it will be permanent as now it will be referred from String literal Pool.

Please tell me whereever I am wrong in any of above statements.
Regards
11 years ago

Jesper de Jong wrote:

Jd Sierra wrote:


This is not valid Java syntax. Did you mean:



ya, I am sorry. You are right Jesper. I meant

Jesper, I would like to have some words from you regarding to my doubt.
11 years ago
What is the Default value of String provided String is an instance variable?

> Any object type has 'null' as a default vale and String is also an object, so default value of String is null. O.k., that's what I was knowing and satisified with it but it takes U-turn when I come to following scenario.



output is:
null

true

So, it's very clear that when I create String using new operator, it's default value is not null, but it's "" i.e. empty String..
I am really surprised by this..What's the technical reason behind this..I want some explanation to inherit this thing.
Thanks Campbell Ritchie for such a nice explanation..
But still I have one doubt regarding to this which I want to make clear..

How many new objects will be created after following statement?


What I am assuming answer is..
1) Two objects will be created if "abc" is not referred from String Literal Pool
2) One object will be created if "abc" is already referred from String Literal Pool

so what I am thinking is..
if is already written somewhere previously in code, then will create only one new object, otherwise (if String s1="abc"; is not wriiten,then ) two new objects will be created.

Please tell me whether I am right or wrong? If wrong then why?
11 years ago
Rajeev,

hashCode() method is overridden in String class, so it will give same result whenever equlas() method returns true for two objects.
11 years ago
Siva has raised a very good question. In fact I am also confusing in this very much.
and I would like to thank Henry Wong especially for giving such a nice explanation..
but still I want to ask some questions to make this thing clear..

Henry Wong Wrote:
As before, unless start and end represent the orignal string back, you will get a new string. This new string is passed to the constructor of the string, and yet another string is created. This second new string is then assigned to the sub reference. Furthermore, since no other reference now points to the response from the substring call, it is eligible for garbage collection. And finally, this operation has no affect on the string pool.



So, this means when we create string object using new operator, two objects are created but none of them is referenced from String Literal Pool..am I? Two things I want to make clear, 1) when we use 'new', how many total objects are created? one/two? 2) when we use new, is any object referred from String Literal Pool?
About answers, what I am thinking is, answer to 1) is 'two' and answer to 2) is 'if we pass String literal in String constrctor to make String, then and then only one of the two objects created will be referred by String Literal Pool'.
Am I correct??

Now, If I am right (or wrong) till now, then what happens in following scenario:



Now, after first line, one object "abc" will be created and it will be referenced from String Literal Pool as well as it will be referenced by s1.
What exactly happens after line 2?
definately, "abc" will be referenced by s2, but which "abc"? will it create new one or just refer previously created on line 1? If it will create new object "abc", then will it be referenced from String Literal Pool?

article on http://www.javaranch.com/journal/200409/Journal200409.jsp#a1, posts by Henry and SCJP book by Kathy- all leads me to raise above questions which clearly indicates how confuse I am? Please solve it.

Last but not least,
Following question raised after reading conversation about substring in above posts.


I was thinking new object will be created after line 2 containing String "ell" , but after reading above posts, I'm confused a bit. Will it not create new object and just refer to the part of the main String created on line 1? If yes, then if s1 is set to null as on line 4, then at that time what will happen? new object will be created containg substring i.e. Steing "ell"?

Sorry for long post, but if my all of above doubts will be cleared, I will be very thankful to you people.
The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.



This will print child get1 child get2 as get() is in Child and it is invoked from there.
Thanks Henry,
You have given really nice explanation on very confusing topic..

Just wanted to know one thing..
If it is asked straight away that..'Where class level variables (Static variables) are stored? Stack or Heap?' ..What should be the answer?
I think it should be Heap,am I?
11 years ago
Henry Wong and Ankit Garg,

Finally after doing too many codding and reading different threads, I am almost near to conclusion. Only one thing I want to make clear as far as SCJP exam comes into the picture.

well, I am using Java 1.6.0_24 and
When I run program by using CMD, it allows only upto Short, So

Integer i=(byte)10; is not possible
but following are possible because they are upto Short and constants fit in the range of the target primitive wrapper type

Byte b=(short)10;
Short b=(byte)10;
Byte b=(char)15; and so on

But when I use Eclipse IDE, it allows same thing upto Integer so as you are saying
Integer i=(byte)10;
works..

Now my Question (probably last one regarding this topic ) is which one should I consider right from above two in exam? upto Short which I think I should or upto Integer by giving honour to IDE..

Please also correct me if I am going wrong at anywhere else
Henry,

I am still not getting it. Please solve my confusion as I am really eager to know where I am going wrong.

I am aware of Compile time constant but I a dont think here it comes into the picture as we are not using final keyword

well, let me explain where really I am not getting. I have tried following code and both are resulting in compile error.

and

I am really confused how these can work. definitely it gives me compile error when I try it but in above post it's written that both of above code works. how exactly?

Ok..got it.
THanks Rob and Campbell
11 years ago
Why it is good to treat constants as static and final inspite of only final?

When you want to declare constant, you mean that its value cannot be changed later on. This can be done very easily by
using final access modifier. That's good.

But once you are declaring variable as final, its value is never going to be changed. Every Object has to play with the value with which final variable is declared once. So it's very clear that when we create object, though each and every new object will get their own copy of final variable, they have nothing to do with that final variable except using it..I mean it can't be changed. So, why not to make it static and save memory? after all, each and every object is sharing same value. By making it static, we can save memory by just sharing one copy between all objects as well as we can have more clear logic. Moreover, even though variable is decalred static, we can use it by reference variable followed by dot operator. Compiler will automaticlly replace reference variable with the name of the class in which static variable lies. On the whole, there is nothing to loose by making constant static. In fact we are gaining something.

I hope I have cleared now why constants are static also with final. If I am wrong somewhere or anybody is not able to understand any of my point, feel free to reply.
Can anybody help me to understand following sentence posted by Steve Page?

"final class variables, or constants, have the same rules as instance ones, except there is no constructor"
11 years ago
Can anybody explain me the meaning of this sentence?

"Calling 'intern()' prevents the compiler from in-lining the value to the calling class while still giving you the advantage of using shared literal string values."

I have come across intern() first time.
11 years ago