java中的instanceof關(guān)鍵字詳細解讀
instanceof關(guān)鍵字
instanceof 是 Java 的保留關(guān)鍵字,它的作用是測試它左邊的對象是否是它右邊的類的實例,返回 boolean 的數(shù)據(jù)類型。
代碼中可能遇到的情況:
1、基本數(shù)據(jù)類型

如上圖,這種情況會報錯。將右邊的類型改為引用類型:

依舊報錯,改成特殊的null:

依舊報錯,由此得出:基本類型不能用于 instanceof 判斷。
為了驗證這一點,換一個基本數(shù)據(jù)類型double進行測試:

依舊報錯,可以驗證結(jié)論正確。
2、引用類型
創(chuàng)建如下關(guān)系的類和接口:


測試一:
public static void main(String[] args) {
Dog dog = new Dog();
Animal animal = new Animal();
Animal cat = new Cat();
System.out.println("dog instanceof Dog的結(jié)果是:" + (dog instanceof Dog)); // true
System.out.println("dog instanceof Big的結(jié)果是:" + (dog instanceof Big)); // true
System.out.println("animal instanceof Big的結(jié)果是:" + (animal instanceof Big)); // true
System.out.println("animal instanceof Dog的結(jié)果是:" + (animal instanceof Dog)); // false
System.out.println("cat instanceof Animal的結(jié)果是:" + (cat instanceof Animal)); // true
System.out.println("cat instanceof Cat的結(jié)果是:" + (cat instanceof Cat)); // true
}打印結(jié)果:
dog instanceof Dog的結(jié)果是:true
dog instanceof Big的結(jié)果是:true
animal instanceof Big的結(jié)果是:true
animal instanceof Dog的結(jié)果是:false
cat instanceof Animal的結(jié)果是:true
cat instanceof Cat的結(jié)果是:true
測試二:

- 基本類型完全不能用于 instanceof 判斷
- null 只能放在 instanceof 關(guān)鍵字的左邊
3、數(shù)組類型
延續(xù)引用類型示例,可以得到數(shù)組類型用來判斷時的情況:
Dog[] dog = new Dog[3];
Animal animal = new Animal();
System.out.println("dog instanceof Dog[]的打印結(jié)果是:"+(dog instanceof Dog[]));
System.out.println("dog instanceof Big[]的打印結(jié)果是:"+(dog instanceof Big[])); 打印結(jié)果:
dog instanceof Dog[]的打印結(jié)果是:true
dog instanceof Big[]的打印結(jié)果是:true
特別地,基本類型的數(shù)組也是可以用來判斷的:
int[] arr = new int[3];
System.out.println("arr instanceof int[]的打印結(jié)果是:"+(arr instanceof int[]));
打印結(jié)果:
arr instanceof int[]的打印結(jié)果是:true
4、應(yīng)用場景
instanceof 關(guān)鍵字一般用于強制轉(zhuǎn)換,在強轉(zhuǎn)之前用它來判斷是否可以強制轉(zhuǎn)換:
/**
*========================================
* @方法說明 : 空判斷 空返回true
* @param obj
* @return boolean
* @exception
*========================================
*/
public static boolean isEmpty(Object obj) {
if (obj == null || "null".equals(obj.toString()) || "".equals(obj.toString())) {
return true;
}
if (obj instanceof String) {
return ((String) obj).trim().length() == 0;
}
if (obj instanceof Collection) {
return ((Collection) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map) obj).isEmpty();
}
return false;
}到此這篇關(guān)于java中的instanceof關(guān)鍵字詳細解讀的文章就介紹到這了,更多相關(guān)instanceof關(guān)鍵字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java運算符與流程控制之全類型運算符用法及分支?/?循環(huán)語句實戰(zhàn)指南
這篇文章主要介紹了Java運算符與流程控制之全類型運算符用法及分支/循環(huán)語句實戰(zhàn)的相關(guān)資料,運算符包括算術(shù)、關(guān)系、邏輯、賦值和三元運算符,重點講解了它們的用法、優(yōu)先級和結(jié)合性,以及強制類型轉(zhuǎn)換的注意事項,需要的朋友可以參考下2026-01-01
spring/springboot整合curator遇到的坑及解決
這篇文章主要介紹了spring/springboot整合curator遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
JSP 開發(fā)之hibernate的hql查詢多對多查詢
這篇文章主要介紹了JSP 開發(fā)之hibernate的hql查詢多對多查詢的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
java中MultipartFile類型轉(zhuǎn)為File類型的4種方法
Spring提供了一個MultipartFile接口來處理文件上傳,但有時候我們需要將MultipartFile轉(zhuǎn)換為File來進行一些特定的操作,比如保存文件到本地或者進行文件的處理等,這篇文章主要給大家介紹了關(guān)于java中MultipartFile類型轉(zhuǎn)為File類型的4種方法,需要的朋友可以參考下2024-09-09
在CentOS7(有圖形化界面)上安裝maven和idea的詳細教程
這篇文章主要介紹了在CentOS7(有圖形化界面)上安裝maven和idea的詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

