THE COMPARISON OPERATOR
The Java programming language offer some operators to assign, compare and execute math operations. Considering the object of study in this paper, the operator ”==’ is used in the Java language in order to compare only primitive objects. For instance, if you execute the procedure ‘2 == 2’ or ‘2d == 2d’, the result will be simply TRUE, because both of the operations use primitive types in Java. In the case of using object comparisons, the operator ‘==’ will not be a good resource to use, except if you are trying to compare whether the object references in the memory are the aspect in the comparison subject.
COMPARING OBJECTS IN JAVA
To compare objects in Java, the $object.equal method is available in all the java object classes, including Strings, which in Java , String is a class, not a primitive type. The String class is part the JSE library, and it is accessible by any Java program.
One very interesting thing in Java s about Strings. A good illustration, according to Horstman & Cornell, 2008:
String greeting = "Hello"; //initialize greeting to a string if (greeting == "Hello") . . . // probably true if (greeting.substring(0, 3) == "Hel") . . . // probably false
“If the virtual machine would always arrange for equal strings to be shared, then you could use the == operator for testing equality. But only string constants are shared, not strings that are the result of operations like + or substring” (SANCHEZ & CANTON, 2002).
In spite of Java to have its particularities, I personally believe the way the Strings and comparison in Java are a good and extensible way of implementing it. Programmers are able to override the equals method, in order to define what really is equality for their own objects and the language offers many ways and possibilities in order to provide good performance when using comparison between primitives and objects.
Horstmann & Cornell, 2008. Core Java – Volume 1 fundamentals. 8th ed. California, USA: Sun Microsystems Press.
SANCHEZ & CANTON, 2002. Java programming for engineers. 1st ed. Florida, USA: CRC Press LLC.