next up previous
Next: Exercise: modification of the Up: Unit 05 Previous: Note on comparisons: equality

Note on comparisons: comparison with null

We recall that a variable of type reference to object that does not refer to any object has value null.

The comparison with null can be used in the condition of an if-else statement. To compare with null we have to use == and not the equals() method.

Example: showInputDialog returns null if the user presses the ``cancel'' button:

String input = JOptionPane.showInputDialog("...");
if (input != null) { ... }


next up previous
Next: Exercise: modification of the Up: Unit 05 Previous: Note on comparisons: equality