Kushan Athukorala

Ranch Hand
+ Follow
since Aug 09, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
6
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 Kushan Athukorala

Hi Alex,

I would like to clear some of your misunderstanding on Java concepts.

Case I :

'==' is and operator in java. This operator internally use the equals() method which is defined in Object class to compare whether the two object references are pointing to the same object instance.

If you define Strings as in Case I above, you are creating one "Hello" string in the String Constant Pool Memory with two s1 and s2 references. This is merely because of performance reasons in java.

This is the reason why you see the '==' operator gives the 'true' result on comparison.

Case II:

'equals()' is an method define in Object class. This method can be used to compare the two objects for meaningful equivalence. If you want to make two objects be meaningfully equal you have to override the equals() method in your class.

If you use 'new' to define a String, such strings will be created on heap memory, not in the pool memory. Therefore you can see two s1, s2 references pointing to two String objects in the heap. This is the reason why you see '==' operator giving 'false' on comparison in this scenario.

Then the question is why you get equals() comparison 'true' in such circumstances.

Let me explain.

As I mentioned earlier equals() method of Object class can be override to check for meaningful equivalence. The developer who did code the String class for us, has been overridden the equals() method so that we can see two String objects are meaningfully equivalent.

I hope now everything is clear to you.

Thanks,
Kushan

10 years ago
Hi Jan,

This is an arguable question raised in our forum in 2008.

https://coderanch.com/t/417925/java-programmer-SCJP/certification/SCJP-Chapter-GC

However I believe only the answer given by Henry Wong in the above link.

The reason is that the references are not eligible for GC, only Objects get GCed.

Thanks,
Kushan
Hi Marlo,

Nice score, congratulations

Keep contributing to our forum,

Thanks,
Kushan
10 years ago
Hi Pete,

a==c also returns true. To explain this we have to consider the String Constant Pool.

It is a part of memory area defined specially for Strings. Whenever you create different strings it is added to the pool but if you define same strings, it doesn't create new strings but take the string from the pool memory. So the a==c should return true in your explanation.

Thanks,
Kushan

10 years ago
This is well explained in Kathy and Bert ' s SCJP book at the page 246 as well.

Thanks,
Kushan
Hi Remo.

Don't be exited.. Have a good sleep and attend to the exam.

Wish you all the best

Regards,
Kushan
Hi Ankur,

Can you tell us what you understand from this code first?

Then we can help you to figure out where you go wrong.

Thanks,
Kushan
Hi Swaraj,

As you see in the Case I, we are narrowing the object types that can be added to the unsafe list instance.

But my question is, what is the reason the compilation should to be failed if you are not using the list in your code. In Case I, compiler waits till you do some incorrect operation(list1.add("some string")) on the list, but if you do correct operations(list1.add(10)) it will accept as it is.

Thus the compiler does the best to us without giving unwanted failures

Regards,
Kushan
Hi Ankur,

If you study the Kathy's book it is more than enough for the Exam.

If you understand the concepts properly you don't need to remember all api methods.

Remembering some api's might be useful in the exam. I personally got one question in the exam.

That is I had to select whether the below code get compiled or not




Regards,
Kushan

Hi Adolfo,

It gets widens from short to long.
It does not box in to Object.

Regards,
Kushan
Hi Swaraj,

Key objective of generics in java is to achieve compile time type safety.

Case I:
Case II:

Q1: Why above two cases compiled successfully with unsafe warnings?
In case I you try to tell the compiler that you are creating a type safe reference to an unsafe ArrayList object.
In case II you try to tell the compiler that you are creating an unsafe reference to a type safe ArrayList object.

Because of the type un-safety, both cases does not cause the compilation to be failed and you will see the successful compilation, but with warnings.

Q2: What is the difference in Case I and Case II?
Since we are talking about compile time only as I mentioned initially , compiler is unable to see the actual objects. So the compiler make decisions using the object references not using the actual objects in the heap.

So you cannot do the following invocation without compile error in Case I

But you can do the same invocation in Case II.

That is

Also note that after everything type-erasure erases all generic declarations at runtime

Regards,
Kushan