1

I was wondering how equals method in Object class works. This is the implementation of the method.

public boolean equals(Object obj) {
    return (this == obj);
}

where its evident that equals method is using ==. So,now when I am comparing two objects a and b, if a happens to be null, it doesn't throw any exception. But a.equals(b), throws NPE, why?

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
javaAndBeyond
  • 520
  • 1
  • 9
  • 26

3 Answers3

1

When you call method on null object, it even doesn't call the method but gives null pointer error on same movement. Same is applicable to equals.

When invoke a.equals(b);

It will give null pointer exception as you are calling equals on a which is null.

For example, if you create method blank.

public Class MyClass{
    public voidblank() {

    }
}

and now even if you call blank on null object it will give you null pointer, nothing to do with method implementation.

MyClass a = null;
a.blank();

This will also give null pointer as a is null.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
Panther
  • 3,312
  • 9
  • 27
  • 50
1

But a.equals(b), why does it throw NPE?

Because you can't invoke a method on null (methods are invoked by the referene variable in Java). When a is null, there is simply nothing to call equals on (or to de-reference). All that Object (non-primitive) Java variable types can hold is the value of a reference (or null, which by definition is not a value). That's why Java (pass by value), has the functionality of pass by reference. The value of Object(s) are references.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Yes, null cannot be invoked. I understod. But, I didn't understand the last line, where you said java has pass by reference functionality. I know that Java is pass by value, but could you explain the last line more. – javaAndBeyond Nov 18 '15 at 05:12
  • @user2296988 When you pass an instance between methods, its' state can be modified (because it's passed by value, but the value is a reference). For example, [`Arrays.sort(int[])`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-int:A-). Arrays are `Object`(s), and it sorts a passed in array (and returns `void`). `StringBuilder` methods that allow chaining, return `this` to do so. – Elliott Frisch Nov 18 '15 at 05:18
  • hmm... i got what you meant in those examples, that is ,modifying the reference value passed in a method, without the need of return. Btw, is it the same in other OOP languages? Is null not a value in other languages as well? – javaAndBeyond Nov 19 '15 at 02:56
  • @user2296988 Modifying the referent value, the reference value doesn't change (you can't modify the caller's reference). In **most** languages anyway, I don't know about ***all*** languages. Erlang, Haskell, Smalltalk, LISP or Scheme *could* have different semantics. Java does have [`Optional`](http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html). – Elliott Frisch Nov 19 '15 at 02:58
  • what i meant was, when we call `Collections.sort(List list)`, the method doesn't have a return value. It modifies the value that 'list'(reference variable) is referring to. Callers value is being changed here, so does the reference. – javaAndBeyond Nov 19 '15 at 03:18
  • @user2296988 Yes. And what I was trying to clarify, is that a method that assigns a new reference to `list` (`list = new ArrayList();`), has created a new `list` locally - but the caller's `list` will not change. And yes, the instance being refered to is the *referent*. – Elliott Frisch Nov 19 '15 at 03:21
0

a.equals(b) throws null pointer exception because a is null, so when you try to call the instance method on a null object it gives a null pointer exception. For more information please refer to the documentation below for null pointer exception. http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

For these types of situation you should first check whether a is null and then should use the method on this object.

if(a != null) {
    a.equals(b);
}
ashisahu
  • 357
  • 3
  • 9