Saturday, February 8, 2014

Comparisons

comparisons


            Read also  instanceof,  interning,  equals(...)

■  To test if an object belongs to a class (or to one of its superclasses), to an interface, or whether it is an Array, use instanceof.  i.e.

String s = "S";  
if ( s instanceof Object )  is true
if ( s instanceof String )  is true

■  To test if two object references point to the same object or if an object reference is null, use == (double equals). 
For object references, !=  means does not point to the same object.  i.e.

String s1 = "S";  
String s2 = "T"; 
if ( s1 == s2 ) is false.
. . . .
String s1 = "S";  
String s2 = "T"; 
if ( s1 != s2 ) is true.
. . . .
String s1 = null;  
if ( s1 == null ) is true.

■  Remember that the toString( ) method always creates a new object.  i.e.

if ( samething.toString( ) == samething.toString( ) ) will always be false because they’re always different objects.

■  The == operator doesn’t differentiate between floating point's primitive positive zero, normal zero, or negative zero.  i.e.

if ( –0.0 == +0.0 ) is true.  So are any other == comparisons between 0.0. +0.0, and -0.0.

■  For objects only, use object.equals( anotherobject ) to see if the actual contents of two objects match. 

■  You cannot use == to compare objects of different types, as a compile error will result. i.e.

String s1 = "S";  
Character c1 = new Character( 'S' ); 
if (  s1 == c1 ) System.out.print( "True" );   will not compile.

■  To test if two objects are of the same class type (or supertype) use Object’s getClass( ) method with == .   The getClass( ) method returns a Class object. i.e.
if (object1.getClass( ) == object2.getClass( )) then do something. 

.equals(...)  method


■   Compare objects.     equals(...)  does a byte-by-byte comparison on contents, ignoring the object's location.  It is the opposite of == , which ignores contents and compares location pointers.

■  Unless you override the equals(...) method in an object's class, equals(...) is the same as == , which compares just pointers, not contents. Without an override you will default to using the equals(...) method from Object, which does just a   ==  comparison. The wrappers and collections classes, plus String,  all overrideequals(...) so they provide actual content comparisons. 

■ You cannot use equals(...) for primitives or literals.  i.e.

                        Double D = new Double( 5 );  
                        if ( D.equals( 5 ) ) will not compile due to the literal 5.

                        Double D = new Double( 5 ); 
                        int d = 5; 
                                if ( D.equals( d ) )   will not compile due to the presence of the primitive d.

■  You cannot get true results on objects of different types using equals(...). It is only for objects of the same type.  i.e.

                        Character C = new Character( 'A' );  
                        Object O = new StringBuffer( "A" );  
                        String S = "A";
                        if ( C.equals( O ) )             is false
                        if ( S.equals( C ) )             is false
                        if ( S.equals( O ) )             is false

■ You cannot use equals(...) between different wrapper types as it will always returns a false.  i.e.

                        Double D = new Double ( 2.0 );
                        Float F = new Float( 2.0 );
                        if (D.equals( F) )     is false due to different object types

■  You cannot use equals(...) for StringBuffer, as StringBuffer does not provide an override.   i.e.

                        StringBuffer s1 = new StringBuffer( "ABC" );
                        StringBuffer s2 = new StringBuffer( "ABC" );
                        if ( s1.equals(s2) )                 is false

                        However...
                        String s1 = new String( "ABC" );
                                String s2 = new String( "ABC" );
                                if ( s1.equals( s2 ) )               is true

■ You cannot ever check an object for null with equals(...)
That’s because with null there's no actual null object to which to compare. 
You must say if (obj == null) instead.  i.e.

                        if ( object.equals( null ) ) runs and it returns false if the object is real.
                        But it gives a NullPointerException if the object in question was actually null.

■  Unlike the == operator, the equals(...) method treats two wrapper NaNs as equal.   i.e.

                        Float F1 = new Float( Float.NaN ); 
                        Float F2 = new Float( Float.NaN ); 
                        if ( F1.equals( F2 ) )                       returns true
                        if ( F1 == F2 ) )                             returns false

■  Unlike the == operator, which treats all manners of floating point zeroes as unequal, equals(...) treats wrapper contents of floating point +0.0 as equal to 0.0. i.e.

In wrappers:

= = returns
equals(...) returns
+0.0 to 0.0
unequal
equal
+0.0 to -0.0
unequal
unequal
0.0 to -0.0
unequal
unequal

No comments: