Skip to content

Commit

Permalink
fix class == comparasion
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelliao committed Nov 4, 2024
1 parent f1c79dd commit b75f3e7
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion source/books/java/50-reflection/10-class/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ boolean b1 = n instanceof Integer; // true,因为n是Integer类型
boolean b2 = n instanceof Number; // true,因为n是Number类型的子类

boolean b3 = n.getClass() == Integer.class; // true,因为n.getClass()返回Integer.class
boolean b4 = n.getClass() == Number.class; // false,因为Integer.class!=Number.class
Class c1 = n.getClass();
Class c2 = Number.class;
boolean b4 = c1 == c2; // false,因为Integer.class != Number.class
```

`instanceof`不但匹配指定类型,还匹配指定类型的子类。而用`==`判断`class`实例可以精确地判断数据类型,但不能作子类型比较。
Expand Down

0 comments on commit b75f3e7

Please sign in to comment.