Interviewer: If compareTo() method returns zero this means that the objects are always equal?
Me:Yes.
But I was wrong at that point its not necessary the example displaying the failure of this is java.math.BigDecimal class, whose equals method will return true is the value is equal in both value and scale for ex:- 6.0 and 6.00 will not be equal, but compareto() will return 0 if both objects are compared.
On doing some R&D I found that:
It is strongly recommended, but not strictly required that(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
This question can also be asked in an other way as:
Can we store BigDecimal Class in TreeSet?
Anser: NO because of the above reason.
I would like to add few more common questions from different sources so that it may be helpful for someone and myself also when some quick revision is required.
Top 20 Core Java Interview Questions and Answers asked on Investment Banks
Java Source Code Examples
Java Programming Exercises
Top 10 Algorithms for Coding Interview
Hunting Memory Leaks in Java
Me:Yes.
But I was wrong at that point its not necessary the example displaying the failure of this is java.math.BigDecimal class, whose equals method will return true is the value is equal in both value and scale for ex:- 6.0 and 6.00 will not be equal, but compareto() will return 0 if both objects are compared.
On doing some R&D I found that:
It is strongly recommended, but not strictly required that(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
This question can also be asked in an other way as:
Can we store BigDecimal Class in TreeSet?
Anser: NO because of the above reason.
I would like to add few more common questions from different sources so that it may be helpful for someone and myself also when some quick revision is required.
- Difference between long xl = 1000*60*60*36*365L and long x = 1000*60*60*36*365?
- Page on stackoverflow.com this page explains very well about one of the famous interview question.
- Avoiding "!= null" statements in Java? This explanation is very good.
- Easy interview question got harder: given numbers 1..100, find the missing number(s)
- StringBuilder and StringBuffer
- Page on stackoverflow.com Tells about concurrent HashMap
- Output of the following program:
- Integer i1 = 127;Integer i2 = 127;Integer i3 = 128;Integer i4 = 128;System.out.println(i1 == i2);System.out.println(i3 == i4); Output:true, false .Now the question is "why?!" Reason: Integer.valueOf() uses internal cache to retrieve the value.Only if the value is not in cache new instance of Integer is created.This cache is initialized from 0 to 127 inclusive so when we try to get the value lesser than 128 we get the same value from cache and in case of values greater than 127 it creates new instance.
Top 20 Core Java Interview Questions and Answers asked on Investment Banks
Java Source Code Examples
Java Programming Exercises
Top 10 Algorithms for Coding Interview
Hunting Memory Leaks in Java
Comments
Post a Comment