欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 中的instanceof用法詳解及instanceof是什么意思(推薦)

 更新時(shí)間:2017年11月10日 11:48:02   作者:nextnj  
instanceof 是 Java 的保留關(guān)鍵字。它的作用是測(cè)試它左邊的對(duì)象是否是它右邊的類的實(shí)例,返回 boolean 的數(shù)據(jù)類型。接下來(lái)通過(guò)本文給大家介紹java 中的instanceof用法詳解及instanceof是什么意思,需要的朋友參考下吧

好,應(yīng)大家的要求先給大家說(shuō)下在JAVA程序中instanceof是什么意思

instanceof是Java的一個(gè)二元操作符,和==,>,<是同一類東東。由于它是由字母組成的,所以也是Java的保留關(guān)鍵字。它的作用是測(cè)試它左邊的對(duì)象是否是它右邊的類的實(shí)例,返回boolean類型的數(shù)據(jù)。

instanceof運(yùn)算符用法

運(yùn)算符是雙目運(yùn)算符,左面的操作元是一個(gè)對(duì)象實(shí)例,右面是一個(gè)類.當(dāng)左面的對(duì)象是右面的類創(chuàng)建的對(duì)象時(shí),該運(yùn)算符運(yùn)算的結(jié)果是true,否則是false

說(shuō)明:

  (1).一個(gè)類的實(shí)例包括本身的實(shí)例,以及所有直接或間接子類的實(shí)例

  (2).instanceof左邊操作元顯式聲明的類型與右邊操作元必須是同種類或有繼承關(guān)系,

    即位于繼承樹的同一個(gè)分支上,否則會(huì)編譯出錯(cuò)     

 double obj=1;
 if(obj instanceof Double){
 System.out.println("true");
 }

  報(bào) "Incompatible conditional operand types double and Double" 錯(cuò)誤

      obj 必須是對(duì)象的實(shí)例。不能是基礎(chǔ)數(shù)據(jù)類型?!?/p>

  String obj=1.0+"";
 if(obj instanceof Double){
 System.out.println("true");
 }

       報(bào) "Incompatible conditional operand types String and Double" 錯(cuò)誤

   String 和 Double 不是位于繼承樹的同一個(gè)分支上?! ?/p>

 if(null instanceof Object){
 System.out.println("true");
 }else{
 System.out.println("false");
 }
 String obj=null;
 if(obj instanceof Object){
 System.out.println("true");
 }else{
 System.out.println("false");
 }

      打印都為 false.  null用操作符instanceof測(cè)試任何類型時(shí)都是返回false的。

 if(obj instanceof null){
 System.out.println("true");
 }else{
 System.out.println("false");
 }

編譯出錯(cuò)。報(bào)"Syntax error on token "null", invalid ReferenceType" 錯(cuò)誤。

public class Test {
 public static void main(String[] args){
 System.out.println(new Student() instanceof String); //compile time error
 System.out.println(new Student() instanceof Exception); //compile time error
 System.out.println(new Student() instanceof Object); //compilation and output true
 System.out.println(new Student() instanceof List); //compilation and output false
 System.out.println(new Student() instanceof List<?>); //compilation and output false
 System.out.println(new Student() instanceof List<String>); //compile time error
 System.out.println(new Student() instanceof List<Object>); //compile time error
 System.out.println(new String() instanceof List); //compile time error
 System.out.println(new String() instanceof List<?>); //compile time error
 System.out.println(null instanceof Object); //compilation and output false 
 
 }
}
class Student{ 
}

   看到上面的測(cè)試結(jié)果可能會(huì)有這樣那樣的疑惑,為什么Student對(duì)象測(cè)試String的時(shí)候是編譯錯(cuò)誤,而測(cè)試List的時(shí)候又能夠通過(guò)(難道是instanceof對(duì)接口在編譯時(shí)不作檢查呢,還是由于List類型本身在編譯時(shí)不確定具體類型導(dǎo)致的),但是后面你又會(huì)發(fā)現(xiàn)如果是List<String>的時(shí)候編譯就不通過(guò)了(看來(lái)前面的猜測(cè)是錯(cuò)誤的,他對(duì)接口是要做檢查的),可是往后面看,你又會(huì)納悶String測(cè)試List還是List<?>的時(shí)候,直接在編譯期就報(bào)錯(cuò)了,這跟Student對(duì)象測(cè)試List和List<?>的結(jié)果大不同,可能此時(shí)你有點(diǎn)相當(dāng)不解了。我們這個(gè)時(shí)候翻看java對(duì)instanceof操作符的說(shuō)明時(shí),發(fā)現(xiàn)instanceof的這些表現(xiàn)都是跟cast操作符是有點(diǎn)關(guān)系的,就是說(shuō)當(dāng)我們?cè)趇nstanceof方法時(shí),編譯器會(huì)檢查這個(gè)對(duì)象能夠cast到右邊的類型,如果不能cast則直接報(bào)錯(cuò),如果不能確定類型,則通過(guò)編譯,具體看運(yùn)行時(shí)定。

  這里可能還有一個(gè)疑惑,我們Student已經(jīng)確定類型了啊,List類型也是確定的啊,為什么能夠編譯通過(guò)呢,而String卻不行呢(難道是自己定義的類和系統(tǒng)定義的類在編譯處理上有不同),這里java沒有做特別的說(shuō)明,但是我想可能是因?yàn)閒inal關(guān)鍵字的區(qū)別造成的,我們發(fā)現(xiàn)不論String、Integer、Long等都是最終類,他們的處理類似于編譯器對(duì)常量的處理,因?yàn)榫幾g器知道這個(gè)類在運(yùn)行期間是不會(huì)有改變的,故而編譯器在編譯期間認(rèn)為他自己能夠確定這個(gè)類的最終類型。而對(duì)于自己定義的類呢(我試過(guò)系統(tǒng)的非最終類Hashtable、HashMap等,測(cè)試和我們自定義的類的結(jié)果一樣,可以通過(guò)編譯),由于不是最終類,可能編譯器認(rèn)為他可能在運(yùn)行期間會(huì)有改變的可能,故而不把他作最為確定的類型處理,故而可以通過(guò)編譯。其實(shí)當(dāng)我們?cè)谧远x的類前面加上final關(guān)鍵字的時(shí)候,其表現(xiàn)就跟String、Integer、Long這些最終類測(cè)試instanceof表現(xiàn)的一樣了。

好,下面通過(guò)實(shí)例代碼看下java中instanceof用法

java 中的instanceof 運(yùn)算符是用來(lái)在運(yùn)行時(shí)指出對(duì)象是否是特定類的一個(gè)實(shí)例。instanceof通過(guò)返回一個(gè)布爾值來(lái)指出,這個(gè)對(duì)象是否是這個(gè)特定類或者是它的子類的一個(gè)實(shí)例。

 用法:

result = object instanceof class

參數(shù):

Result:布爾類型。
Object:必選項(xiàng)。任意對(duì)象表達(dá)式。
Class:必選項(xiàng)。任意已定義的對(duì)象類。

說(shuō)明:

如果 object 是 class 的一個(gè)實(shí)例,則 instanceof 運(yùn)算符返回 true。如果 object 不是指定類的一個(gè)實(shí)例,或者 object 是 null,則返回 false。

示例代碼如下:

package com.instanceoftest;
 interface A{}
 class B implements A{
 }
 class C extends B {
 }
 class instanceoftest {
 public static void main(String[] args){
 A a=null;
 B b=null;
 boolean res; 
 System.out.println("instanceoftest test case 1: ------------------");
 res = a instanceof A; 
 System.out.println("a instanceof A: " + res);
 res = b instanceof B;
 System.out.println("b instanceof B: " + res);
 System.out.println("/ninstanceoftest test case 2: ------------------"); 
 a=new B();
 b=new B();
 res = a instanceof A; 
 System.out.println("a instanceof A: " + res);
 res = a instanceof B;
 System.out.println("a instanceof B: " + res);
 res = b instanceof A;
 System.out.println("b instanceof A: " + res);
 res = b instanceof B;
 System.out.println("b instanceof B: " + res);
 System.out.println("/ninstanceoftest test case 3: ------------------");
 B b2=(C)new C();
 res = b2 instanceof A;
 System.out.println("b2 instanceof A: " + res);
 res = b2 instanceof B;
 System.out.println("b2 instanceof B: " + res);
 res = b2 instanceof C;
 System.out.println("b2 instanceof C: " + res);
 }
}
/*
result:
instanceoftest test case 1: ------------------
a instanceof A: false
b instanceof B: false
instanceoftest test case 2: ------------------
a instanceof A: true
a instanceof B: true
b instanceof A: true
b instanceof B: true
instanceoftest test case 3: ------------------
b2 instanceof A: true
b2 instanceof B: true
b2 instanceof C: true
*/

總結(jié)

以上所述是小編給大家介紹的java 中的instanceof用法詳解及instanceof是什么意思,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 詳解Spring Boot 使用slf4j+logback記錄日志配置

    詳解Spring Boot 使用slf4j+logback記錄日志配置

    本篇文章主要介紹了Spring Boot 使用slf4j+logback記錄日志配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • 關(guān)于@Autowierd && @Resource 你真的了解嗎

    關(guān)于@Autowierd && @Resource 你真的了解嗎

    這篇文章主要介紹了關(guān)于@Autowierd && @Resource的具體使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java實(shí)現(xiàn)控制小數(shù)精度的方法

    Java實(shí)現(xiàn)控制小數(shù)精度的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)控制小數(shù)精度的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Spring學(xué)習(xí)筆記之RedisTemplate的配置與使用教程

    Spring學(xué)習(xí)筆記之RedisTemplate的配置與使用教程

    這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)筆記之RedisTemplate配置與使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • spring?aop?pointcut?添加多個(gè)execution方式

    spring?aop?pointcut?添加多個(gè)execution方式

    這篇文章主要介紹了spring?aop?pointcut?添加多個(gè)execution方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程

    spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程

    Spring Boot支持容器的自動(dòng)配置,默認(rèn)是Tomcat,當(dāng)然我們也是可以進(jìn)行修改的。這篇文章給大家介紹了spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程,需要的朋友參考下吧
    2018-01-01
  • Spring Cloud重試機(jī)制與各組件的重試總結(jié)

    Spring Cloud重試機(jī)制與各組件的重試總結(jié)

    這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機(jī)制與各組件的重試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • java獲取包下被指定注解的類過(guò)程解析

    java獲取包下被指定注解的類過(guò)程解析

    這篇文章主要介紹了java獲取包下被指定注解的類過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java開發(fā)學(xué)習(xí) Eclipse項(xiàng)目有紅感嘆號(hào)解決之道

    Java開發(fā)學(xué)習(xí) Eclipse項(xiàng)目有紅感嘆號(hào)解決之道

    這篇文章主要為大家詳細(xì)介紹了完美解決Eclipse項(xiàng)目有紅感嘆號(hào)問題的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • MyBatis執(zhí)行SQL的兩種方式小結(jié)

    MyBatis執(zhí)行SQL的兩種方式小結(jié)

    本文主要介紹了MyBatis執(zhí)行SQL的兩種方式小結(jié),主要包括SqlSession 發(fā)送SQL和SqlSession獲取Mapper接口,通過(guò)Mapper接口發(fā)送SQL,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10

最新評(píng)論