I can compare two different objects to know whether they belong to same class or different using both == operator and equals method also. But which one is the better approach and how? Have a look on both approach that i followed. What is better way to compare class of two objects
//r2 and r3 are two different objects
//first approach Using == operator
boolean firstApproach = ((Object)(r2.getClass()) ==((Object)r3.getClass()));
//second approach Using equals method to compare
boolean secondApproach = ((Object)(r2.getClass())).equals(((Object)r3.getClass()));
System.out.println("...firstApproach ..."+firstApproach +"...secondway.."+secondway );
java.lang.Classinherits itsequals()implementation fromjava.lang.Object:So it doesn’t matter which way you compare classes. Also, class loaders are irrelevant as the behaviour will stay the same.