|
If two circles' positions have the same x and y coordinates, but are not represented by the same single Point object, then == will evaluate to false. You need to use equals() for the Points. Likewise Colors. The == operator gives true only if two references point to the same object, or both are null. |
|
but when pass my own class objects in to this equals() method ,it is not returning true even though the object having same content . Equalstesting e1 = new Equalstesting(10); Equalstesting e2 = new Equalstesting(10); if(e1.equals(e2)) { System.out.println("both objects are equals"); } else { System.out.println("both objects are not equals"); } output is : both objects are not equals. why ,please explain ... |
|
|
Example: Integer I = new Integer(0); If (i.equals(0)) . As far as I know, equals(Object obj). Therefore, i.equals(0) should generate a compilation error. However, I teststed this with JCreator with the latest 1.6.0_03 jdk. It accepts it and treat the 0 as the actual value. Therefore, if I do: Integer I = new Integer(0); If (i.equals(0)) . i.equals(0) return true. Shouldn't ... |
|
I'm not sure if thats exactly what I want to do but here's my situation. I need to keep track of the number of times a String appears so I create an Entry object which contains the string, and the number of times it appears. As each string appears, I create an entry of it and check if the entry appears ... |
Hi, I want to compare two different sets with eachother. The Set contains objects with a number of strings and Integers, and some of these need to be equal in order to find a match between the two different Sets. This differs from the "natural order" or the actual equal-value for the two objects, so I dont want to implement Comparable ... |
I highly doubt that. That would be a rather huge glaring bug in the VM, and you'd have to provide code that would let others reproduce it. You're almost certainly not looking at what you think you are. * Something funky with your debuger or how you're using it. * Multiple threads. * Bogus print statement that's not printing what you ... |
|
It means if equals() will have different rules in subclasses than in the current class. For instance, the List interface documentation specifies that equals() will return true if the other object is a List and if it has the same elements in the same order. Therefore, List implementations have to use instanceof, so that, for example, an ArrayList can be equal ... |
You have to override the equals method when your class has a notion of logical equality, because in that case you need more that the default one (the default one return true only if the reference of the compared Object is the same). A good exemple is Date, where we consider that is two Dates have the same getTime() they are ... |
|
and my loop is below and won't compile unless I cast the (String):- int countMatches = 0; for (Iterator it = searchPrompts.iterator(); it.hasNext() && countMatches <= 5;) { String aPrompt = (String) it.next(); if(searchString.startsWith(aPrompt)) { System.out.println(aPrompt + " starts with " + searchString); matchingPrompts.add(aPrompt); countMatches++; } else { System.out.println(aPrompt + " does not start with " + searchString); } } |
hi all, consider this example, class A { public static void main(String args[]) { A a1 = new A(); A a2 = new A(); if(a1.equals(a2)) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } i know the usage of equals() method. but, there is no equals method defined in the class A. also i haven't use any libraries. can anyone ... |
I was just wondering, just to help me better understand object oriented, In the String class the equals method uses Object in its parameter. I am just wondering why the designers chose to use Object instead of a String object for the parameter as we would never compare a String to anything else anyway: boolean equals (Object anObject) Sorry I am ... |