0

So when we are comparing objects, we use equals() methods, or something similar in an if statement for example. If we have the following code

String a = "foo";
String b = "foo";
return a==b

we would get false returned to us because a and b refer to different objects. On the other hand,

String a = null;
return a == null

we would get true. Why is that?

29
  • 1
    @fangxing, I don't think it has anything to do with the java version. Commented Apr 21, 2017 at 2:00
  • 1
    @4castle - It is not a "compiler optimization". It is a mandatory behavior that is specified in the Java Language Specification. Any "java" implementation that doesn't give true is NOT JLS compliant. Commented Apr 21, 2017 at 2:21
  • 1
    @AlphaGamergate No. It must return true, in any version of Java. Eclipse has nothing do with it, and neither does 'by technicality'. Commented Apr 21, 2017 at 2:25
  • 1
    @AlphaGamergate it is not a 'technicality', as people keep trying to tell you. See this answer with language spec references. stackoverflow.com/questions/3451145/… Commented Apr 21, 2017 at 2:27
  • 1
    @AlphaGamergate - If that is what you meant to ask ... then you FAILed. Next time, check that your example is correct before you post it. I am voting to close this Question as "unclear". Commented Apr 21, 2017 at 2:46

4 Answers 4

5

Why does Object == null work?

This doesn't really mean anything. Objects aren't values in Java. You can't write that. All you can write is someObjectReference == null.

So when we are comparing objects

We aren't. See above. We are comparing references.

we use equals() methods, or something similar in an if statement for example. If we have the following code

String a = "foo";
String b = "foo";
return a==b

we would get false returned to us because a and b refer to different objects.

No we wouldn't, and no they don't. It will return true. Try it. String literals are pooled by Java. There is only one "foo" object.

On the other hand,

String a = null;
return a == null

we would get true. Why is that?

Because the value of the reference a is null, and so is the value of expression on the RHS of the == operator. Equal values => result of == is true. Note that a is a reference, not an object.

Sign up to request clarification or add additional context in comments.

1 Comment

a is a reference, not an object.. Well said.
4

Because two different references can point to the same content. Then the objects are equal, but the references are still different.

But when the references are the same, then we'll, there is only one reference.

Given your example:

Object o = new Object();
Object o2 = o;
return  o == o2

would result in?!

17 Comments

Ok, I understand some reference semantics. I get that the two objects can have the same value and thus equal, but the references are different and the == operand only compares the references. But null isn't a reference is it? And wouldn't two references to null be different? Also, in answer to your question, it would result in true since o2 points to o. I understand that.
@AlphaGamergate All null values are equal.
@AlphaGamergate I'm glad you asked. It's because the Java Language Specification says so. It's not specified how null is implemented, just that all references to it must be considered equal.
@AlphaGamergate That's not correct either. In C, null is a special value of a pointer, represented in the source code (but not necessarily at runtime) by zero. It doesn't point to anything, and there is no 'specific location in memory' for it. The situation in Java is exactly the same, with null instead of the zero.
Indeed, in a typical C implementation, the null pointer points to something that is NOT a valid memory location. That's why you get a SIGSEGV when you try to deference a null pointer.
|
2

I think null represents a variable that does not point to anything in the heap memory。So, if a = null, then a == null returns true is justified,because a does not point to anything, and also null.

1 Comment

Hmmm, ok this makes sense. So what's going on is that a == null means that a has no reference, so we are comparing an object that has no reference to another thing that has no reference?
0

Additionally,

String s1 = new String("test");
String s2 = "test";
String s3 = null;

Check:

s1 instanceof String // true, s1 is String object and we allocated a memory to it.
s2 instanceof String // true, s2 is String object and we allocated a memory to it.
s3 instanceof String // false, because we did not allocate memory to object of String s3

s1 == null           // false, we allocated a memory to the String object s1
s2 == null           // false, we allocated a memory to the String object s2
s3 == null           // true, because even though s3 is of reference type String, it is null

Note:

s3 instanceof null   // Invalid: as instanceof checks from class name. null is not a class

4 Comments

Yeah..... this doesn't answer my question at all. I think a lot of people are misinterpreting my question as a question about reference semantics, whereas I'm primarily concerned with null!
I understand. Hence, I thought of explaining it through my comments. :)
@Devendra I get what you were trying to do. I guess the answer is simply that null works that way just cause and we don't really know the underlying mechanism
It is straightforward. Let me know your what further questions you have about null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.