-1

How the equals function knows s1 having the null reference to the String.

alexbt
  • 16,415
  • 6
  • 78
  • 87

3 Answers3

0

You are getting a null pointer because you are saying null.something....this is a null pointer exception..You cant call a method on a null

Josh Engelsma
  • 2,636
  • 14
  • 17
0

Its a runtime exception. As you know equals is a instance method so when it is actually called at runtime, it should has object of the String. So when there is null instead of real object it throws null pointer exception.

Jatin Malwal
  • 5,133
  • 2
  • 23
  • 26
0

The equals() method is not able to execute.

When you call s1.equals(s2), it will try to execute equals() method of s1 object, but since s1 is null, therefore you get a nullPointerException.

For more details check Documentation

The equals() method provided by Object tests whether the object references are equal—that is, if the objects compared are the exact same object.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51