RFR: 8371795: Improve documentation of Class.isInstance [v3]

Chen Liang liach at openjdk.org
Tue Dec 9 00:38:34 UTC 2025


> The 3 methods to determine conversions and subtyping on `java.lang.Class`, which are `isInstance`, `cast`, and `isAssignableFrom`, have their documentation from the earliest days of the Java Platform. During the language evolution, a lot of terms have become inaccurate, such as "assignment-compatible", which does not apply for primitive types, and the out-of-date instanceof analogy with the upcoming patterns, in `isInstance`; `isAssignableFrom` is not very clear about arrays; `cast` would also benefit from more detailed explanations.
> 
> In my facelift, I moved the subtyping description to `isAssignableFrom`, and left the conversion stuff in `isInstance` and `cast`.  I intentionally avoided linking to too many JLS chapters to reduce confusions. I believe in this shape, we have a good amount of easily comprehensible yet accurate specification for all 3 methods, and users are welcome to read the linked JLS chapters for more details and context.

Chen Liang has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains four additional commits since the last revision:

 - Non-null more straightforward than object
 - Merge branch 'master' of https://github.com/openjdk/jdk into doc/class-casts
 - Primitive type or void
 - Improve documentation of Class.isInstance

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/28684/files
  - new: https://git.openjdk.org/jdk/pull/28684/files/4a4f2867..5fd09962

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=28684&range=02
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=28684&range=01-02

  Stats: 12070 lines in 58 files changed: 8333 ins; 3439 del; 298 mod
  Patch: https://git.openjdk.org/jdk/pull/28684.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/28684/head:pull/28684

PR: https://git.openjdk.org/jdk/pull/28684


More information about the core-libs-dev mailing list