Win a copy of Spring Boot in Practice this week in the Spring forum!

Ranch Hand
since Jun 14, 2009
Cows and Likes
Cows
0
In last 30 days
0
Total given
0
Likes
0
0
Total given
0
Given in last 30 days
0
Scavenger Hunt
Ranch Hand Scavenger Hunt
Greenhorn Scavenger Hunt

## Recent posts by Madhu Desai

Bob Wheeler wrote:
StringBuffer is final, so you can't sublclass it and create your own equals method.

cheers
Bob

Thanks...
13 years ago

Bob Wheeler wrote:

Then, how to check the values between two StringBuffers are equal?
13 years ago

Devaka Cooray wrote:Oh! This was discussed in many times here.

Devaka.

Oh! i didn't knew that.. This question is real brain twister....

I pray god and really hope, i don't get this type of question while i take exam.

Thanks.
13 years ago
Hi all

Below is question from Examlab - Practice 1 - Question 11

For above problem, i thought answer is '1Sub2'. But when compiled the answer is 'Sub1 2'. Although, Devaka Cooray has given explanation... i couldn't understand it properly.

Will some one help me through it... Thanks
13 years ago
Thank You all.. It was a real help...
13 years ago
Hi all

In DND questions, do we have to fill all the boxes, or will there be boxes which are put unnecessarily - just to confuse us.
13 years ago
Wow!!! Sridhar Gudipalli you are too fast... before i write and submit, your post was already posted..

vuthlarhi donald, you can ignore my post(its same as one with Sridhar Gudipalli)
13 years ago

vuthlarhi donald wrote:the question is I don't understand the output

0,2,4

Its seems you have confused how the for-loop works.. I will explain... see if you too think the same.

int i = 0;

i < 5 (0 < 5: true)
Print 0 //and then increment it to 1 (i++ in Print statement)
i++ (1++ = 2, the one in increment part of for-loop)

i < 5 (2 < 5: true)
print 2 //and then increment it to 3 (i++ in Print statement)
i++ (3++ = 4, the one in increment part of for-loop)

i < 5 (4 < 5: true)
print 4 //and then increment it to 5 (i++ in Print statement)
i++ (5++ = 6, the one in increment part of for-loop)

i < 5 (6 < 5: false)
exit from the for-loop

So the answer is 0 2 4

Hope its clear

13 years ago

vuthlarhi donald wrote:// I am loosing it here..if i=0 ; j = i++; that I know that j will be zero, but 4 that I am loosing it

where did j come from?
13 years ago

Henry Wong wrote:
But you also need to understand why this is true... The reason why -128 to 127 is a concern with the equality operator, is that autoboxing uses the valueof() method, which in turn uses a "cache" of objects.

Since values within this range is retrieved from a cache, they won't be GC'ed when they are no longer referenced, because the cache still has a reference to them.

Henry

Hmmm... I didn't knew that.

Thanks..
13 years ago

Lukas Smith wrote:In K&B 6 book there is Short = 200;
With Short = 5 I think the answer would be different.

I think it doesn't matter what value short has (in this example...). Because, short is a instance variable of that particular object, and every object that has been declared has to have its own copy. Which in turn means objects have to be physically created separately.

The only time, the concern of what the Wrapper object(Short and Integer only) is having like - is it within -128 and 127, is when you use equality operator like == to compare values between identical Wrappers. Where exactly will it store the values to save memory (if its between -128 & 127) is JVM's headache. We don't have to worry about that. Only thing to be concerned is, if the value is between -128 and +127, and you use == operator, it says both are same objects.

Remember, short has values between -32,768 to +32,767. You can store any value between it. And Wrappers are just primitives disguised as Objects, so that we can use them with objects. What remains in Wrappers are just plain primitives.

Hope my thoughts are correct...
13 years ago
What i understood is as follows

when the 1st line is executed

First 1 object is created in heap. This object contains reference variable 'story' of type 'Short'. Had it not be initialized(story), nothing would have happened. Since it is initialized to 200(or whatever), a new object 'Short' is created in heap, and its reference is stored in variable 'story'. So 'c1' has total of 2 objects in heap.

when next line is executed

same as above, 'c2' has 2 objects in heap.

when next line is executed

Before assigning any thing to 'c3', first 'go' method of c1 is executed with passing 'c2' to 'cb'. But immediately in next line, cb is made 'null' and the value of cb(which is null) is assigned to 'c3'. So 'c3' is now initialized to null. So nothing is created in heap by 'c3'.

now next line

2 objects created by 'c1' are toast.

remaining are the only 2 objects that are referred by 'c2'.
So as K&B said, only 2 objects(of c1) are eligible for GC.

Hope i am correct. Please clarify...

13 years ago
Thanks Mason Storm

I think this is correct representation...

Still...where exactly will class's static variable be? - Stack or Heap?
13 years ago

Mason Storm wrote:You've created quite a good picture! MSPaint?

Static variable is ONE to THE CLASS not to the instance. Even if you delete (null) all of the references you will have the reference to B1 object. STATIC IS NOT CONNECTED WITH INSTANCE! Am I right?

Oh yes definitely MsPaint.

I almost understood, but one small confusion... where exactly will class's static variable be? - Stack or Heap?
13 years ago
Hi all

Ref: K&B - Chapter 3 - Self Test - Question 11

Following is the problem with answer, followed by my graphical imagination.

When line 18 reached, how many objects will be eligible for GC?
A: 0
B: 1
C: 2
D: 3
E: 4
F: 5

Solution:
B is correct. It should clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2. What might be less clear is that you can still access the other Beta objects through the static variable a2.b1 - because it's static.

My Doubt:
1. Is this graphical imagination correct?
2. I thought, since a1, b1, b2 are 'null', object 'Beta 1' and object 'Alpha 1' are eligible for GC. Because a1=null, object 'Alpha 1' will become 'Island' object eligible for GC. So total of 2 objects (Beta 1 and Alpha 1) are eligible for GC.

But the answer from K&B says only 1 object is eligible for GC.

Will any body explain me how? and what is meant by the text in bold in solution part?

13 years ago