Pradeep Kr

Greenhorn
+ Follow
since Feb 17, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Pradeep Kr

Do you really need to know the sequence of Class Load for this exam? I guess No.

Seetharaman Venkatasamy wrote:

if you are using generic it doesnt mean that you wont get ClassCastException. it depends on your programming



I understand why ClassCastException is coming. I am trying to make sense of this and trying to understand what is the ideal solution for this.

One more thing - ClassCastException comes only when assigning result of as.get() to C. If you execute as.get() and does not assign it to anything there is no ClassCastException. That means there is no casting being done if we don;t assign result to any variable?

This is my custom code based upon Question 62 from ExamLab Diagnostic Test.

Above code runs fine. But when I change line 15 from

to

Now it started throwing ClassCastException. Why is so? Is this due to generic code removed from byte code during compilation means generic code provide compile time safety not runtime?

Is this SCJP level of question?

Pablo Marmol wrote:

Well, how many i's I have?



Logically to me it looks like for every loop iteration new i (variable) is created and as soon as that iteration finish, i (variable) become out of scope. If you were asking how many i (variable) is created? one for each iteration on stack, but it is not like one on top of another. It is like one created then popped out then new created.

push(i) for first iteration
pop() as first iteration finish
push(i) for second iteration
..
..
and so on

I am not sure I answered your question but tried to put some thought to clear your doubt, If I did.
Jim, I agree if we were able to compile code, there would be no problem during run time.


But in order to run, we need a compiled code. and the problem with this code (in simplified code) is already mentioned by Ankit Garg



Above code is not compilable and hence there is no question of run-time.
Seriously, is there any link between subject line and your question?

this is adding number from 0 to 99 into total.

total = 0+1+2+3+4+5+..........................99

Abimaran Kugathasan wrote:

The Output : High



I think logically what Abimaran saying is correct. But ..

Lets assume, above code is not correct. There may be two error
1. Compile time Error
2. Runtime Exception (something like method not found)

Lets think about each for them one by one -
1. Compile time error, I don't think it make sense because it is perfectly valid to call private method of a class from same class's static method.
2. Runtime Exception, how would you explain this behavior, compiler said it is ok code, then suddenly at run time code starting to throw method not found exception.

So my guess is since there is not perfect solution for this problem, best approach would be what is current behavior of Java. I think that's why java designer went for this approach.

Abimaran Kugathasan wrote:


Just to make it clear, I think above Abimaran means RuntimeException not compilation error.
I think Abimaran Kugathasan explained it. But I will try if that helps you.

You need to understand how HashMap works. Please read relevant K&B book section one more time, it explain in better way.
While putting key-value pair in map, HashMap uses key's hashCode() method to determine which bucket to put in this key-value pair. If your key's hashCode() keep changing depending upon some internal state of object (in this case length of name variable), HashMap is not going to rearrange it self. It will still hold key-value pair in the same old bucket.

While getting objects from Hashmap, it two step process, finding correct bucket (using hashCode() of key) and finding correct object in that bucket(using equals() method of key).

Please try to go through the question once again now. It should clear up your doubt.
I understand your frustration but suggest you to use normal font size.

Prasad Kharkar wrote:(Object[]... o) is not the exact match because there was widening from String to Object


Why this reasoning is not applicable between String[]... and Object... methods. String[] is exact match. While Object... is using widening.

This question is not specific to SCJP exam. But It came up during reading one of the tread in this forum. Should I ask this in other forum?

Why it is printing "Object Var-Args" but not "String Array Var-Args"? both are var-args.

If I change it to

Now it is printing "String Array Var-Args". What is the reasoning for this?