RFR: 8258422: Cleanup unnecessary null comparison before instanceof check in java.base

Chris Hegarty chegar at openjdk.java.net
Tue Dec 15 18:58:07 UTC 2020


On Sat, 31 Oct 2020 19:37:10 GMT, Stuart Marks <smarks at openjdk.org> wrote:

>> I believe this changes is useful and still actual:
>> 1. improve code to make it easier to read.
>> 2. performance should be improved a bit too
>
> I’ll see if I can get somebody to take a look at this.

This seems like a reasonable change, which improves readability.

My preference is to wait a little longer (hopefully no more than a couple of weeks), until [JEP 394](https://openjdk.java.net/jeps/394) - "Pattern Matching for instanceof" is finalised, then we can remove the explicit casts in many of these cases. For example:

--- a/src/java.base/share/classes/java/io/File.java
+++ b/src/java.base/share/classes/java/io/File.java
@@ -2191,8 +2191,8 @@ public class File
      *          {@code false} otherwise
      */
     public boolean equals(Object obj) {
-        if ((obj != null) && (obj instanceof File)) {
-            return compareTo((File)obj) == 0;
+        if (obj instanceof File file) {
+            return compareTo(file) == 0;
         }
         return false;
     }

-------------

PR: https://git.openjdk.java.net/jdk/pull/20


More information about the nio-dev mailing list