1. java
  2. android
  3. c#
  4. .net
  5. javascript
  6. php
  7. jquery
  8. html
  9. sql

How to Compare Object references?

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. I do not participate on all the forums I visit. But I found this interesting. Most of them provided good answers for the questions. That is 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. After reading every post I have thought of creating my own thread.

Here In this post I might want to clarify you about Comparing Object references. I hope that everybody will like this post. I know this is simple subject but I still want to share my point of view here. And I believe that most of the programmers have experience with using this operator. That is utilized with the == and! = Operators. These are the two operators that can be utilized with object/item references. Also, these are essentially utilized for looking at for equality (==) and inequality (! =). These operators compare two values. This is chiefly to check whether they refer to a similar item. Still this examination is quick. It is frequently not what you need. 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 genuine just if name is a reference to a similar object that "Mickey Mouse" alludes to. This will be false if the String in name was perused from information or figured (by assembling strings or taking the substring); despite the fact that name truly has precisely 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. I hope that you will find this interesting. And you are going to share your reviews here. Please do not be rude! Share your thoughts here. I am also going to share this board with my friends. So let them also participate here. Have a wonderful day ahead! Waiting for your suggestions! Enjoy!! Read more

  1. Você vai ver essas setas em qualquer página de pergunta. Com elas, você pode dizer se uma pergunta ou uma resposta foram relevantes ou não.
  2. Edite sua pergunta ou resposta caso queira alterar ou adicionar detalhes.
  3. Caso haja alguma dúvida sobre a pergunta, adicione um comentário. O espaço de respostas deve ser utilizado apenas para responder a pergunta.
  4. Se o autor da pergunta marcar uma resposta como solucionada, esta marca aparecerá.
  5. Clique aqui para mais detalhes sobre o funcionamento do GUJ!

0 resposta

Não é a resposta que estava procurando? Procure outras perguntas com as tags java ou faça a sua própria pergunta.