isAssignableFrom

假设有两个类Class1和Class2。 Class1.isAssignableFrom(Class2)表示:

  1. 类Class1和Class2是否相同。
  2. Class1是否是Class2的父类或接口

    调用者和参数都是java.lang.Class类型。

instanceof

用来判断一个对象是否是一个类及其子类或接口及其子接口的的实例。

格式:object instanceof TypeName

第一个参数是对象实例名,第二个参数是具体的类名或接口名

举例

public class TestCase {
public static void main(String[] args) {
TestCase test = new TestCase();
test.testIsAssignedFrom1();
test.testIsAssignedFrom2();
test.testIsAssignedFrom3();
test.testInstanceOf1();
test.testInstanceOf2();
} public void testIsAssignedFrom1() {
System.out.println(String.class.isAssignableFrom(Object.class));
} public void testIsAssignedFrom2() {
System.out.println(Object.class.isAssignableFrom(Object.class));
} public void testIsAssignedFrom3() {
System.out.println(Object.class.isAssignableFrom(String.class));
} public void testInstanceOf1() {
String ss = "";
System.out.println(ss instanceof Object);
} public void testInstanceOf2() {
Object o = new Object();
System.out.println(o instanceof Object);
} }

打印结果:

false
true
true
true
true

参考连接:

https://lucky16.iteye.com/blog/1631253

Class.isAssignableFrom与instanceof的区别的更多相关文章

  1. isAssignableFrom与instanceof的区别

    1.isAssignableFrom针对的是class对象: 2.instanceof是实例. isAssignableFrom是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的 ...

  2. instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系

    instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系          编程的时候可能会遇到一个不知道它属于哪个类的 ...

  3. call()与apply()区别typeof和instanceof的区别

    摘自 http://www.cnblogs.com/qzsonline/archive/2013/03/05/2944367.html 一.方法的定义 call方法: 语法:call(thisObj, ...

  4. 类方法 isAssignableFrom、instanceof 和 asSubclass

    类方法 isAssignableFrom.instanceof 和 asSubclass Spring 框架 CollectionFactory 的 asEnumType 方法使用 "类.a ...

  5. typeof()和instanceof()用法区别

    typeof()和instanceof()用法区别: 两者都是用来判断数据类型的 typeof()是能用来判断是不是属于五大类型Boolean,Number,String,Null,Undefined ...

  6. typeof和instanceof的区别

    typeof和instanceof的区别: typeof typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型.typeof 一般只能 ...

  7. instanceof,isinstance,isAssignableFrom,asSubclass的区别

    1,isAssignableFrom():是字节码对象的方法 是用来判断一个类的字节码对象和另一个类的字节码对象是否相同或是子类或接口. assignable英 [ə,sainəbl]美 [ə,sai ...

  8. isAssignableFrom与instanceof

    isAssignableFrom()方法与instanceof关键字的区别总结为以下两个点: isAssignableFrom()方法是从类继承的角度去判断,instanceof关键字是从实例继承的角 ...

  9. JS中typeof与instanceof的区别

    JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: Typeof typeof 是一个一元运算,放在一个运算数之前 ...

随机推荐

  1. [Swift]LeetCode781. 森林中的兔子 | Rabbits in Forest

    In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...

  2. [Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  3. [Swift]LeetCode927. 三等分 | Three Equal Parts

    Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts ...

  4. [Swift]LeetCode977. 有序数组的平方 | Squares of a Sorted Array

    Given an array of integers A sorted in non-decreasing order, return an array of the squares of each ...

  5. 浅谈React

    浅谈react react是什么?其官网给出了明确定义:A JavaScript library for building user interfaces,一个用于构建用户界面的JavaScript库 ...

  6. Python——day14 三目运算、推导式、递归、匿名、内置函数

    一.三目(元)运算符 定义:就是 if...else...语法糖前提:简化if...else...结构,且两个分支有且只有一条语句注:三元运算符的结果不一定要与条件直接性关系​ cmd = input ...

  7. 微信小程序入门(五)

    24.MINA框架讲解 MINA框架架构 25.小程序运行机制 小程序在首次打开的时间会比较长,后续再打开启动会很快,那么小程序是如何启动的呢? 运行机制-启动 冷启动 热启动 热启动:假入用户已经打 ...

  8. 【纯·技术干货】更 App 化的小程序开发

    2018 年 10 月13 日,由又拍云和知晓云联合主办的 Open Talk 丨2018 小程序开发者沙龙系列活动广州站拉开帷幕,糗事百科前端负责人宋航在沙龙上做了<更App化的小程序开发&g ...

  9. Struts2与Spring整合

    前言 本博文主要讲解Spring怎么与Struts2框架整合... Struts2和Spring的整合关键点: action对象交给Spring来创建 搭建环境 进入jar包 引入jar文件: 1)引 ...

  10. leetcode — single-number

    /** * Source : https://oj.leetcode.com/problems/single-number/ * * * Given an array of integers, eve ...