|
Answer» This is my first visit to this forum board. And I am very much happy to be part of this forum. I have found many interesting topics in this forum. Most of them provided good answers for the question SHARED by other users. Java this is one of my favorite programming language. After my bachelor degree in computer applications, I have done 6 month training on java. The experience with java is too good. Here In this post I would like to explain you about Comparing Object references. Hope everyone will like this post. That is used with the == and! = Operators. These are the two operators that can be used with object references. And these are mainly used for comparing for equality (==) and inequality (! =). These operators compare two values. This is mainly to see if they refer to the same object. Still this comparison is very fast. It is often not what you want. Hope you are following my words. Generally you would like to know if the objects have the same value. And not whether two objects are a reference to the same object. Let US CONSIDER the below as an example, If (name == "Mickey Mouse") // Legal, but ALMOST SURELY WRONG This is true only if name is a reference to the same object that "Mickey Mouse" refers to. This will be false if the STRING in name was read from input or computed (by putting strings together or taking the substring), even though name really does have exactly those characters in it. Many classes (eg, String) define the equals () method to compare the values of objects. Comparing Object values with the equals () Method Use the equals () method to compare object values. The equals () method returns a Boolean value. The PREVIOUS example can be fixed by writing: if (name.equals("Mickey Mouse")) // Compares values, not references. Because the equals () method makes a == test first, it can be fairly fast when the objects are identical. It only compares the values if the two references are not identical. Hope the above explanations helped you somehow. If possible please share your opinion on my post. So I can improve it or edit it again. This is the first time I am posting this type of question. Have a wonderful day ahead! Waiting for your suggestions!
|