The instance of keyword

The keyword instanceOf in java programming language is a boolean operator that is used to test whether an object is of an specified type or not and returns the value accordingly.

The keyword instanceOf in java programming language is a boolean operator that is used to test whether an object is of an specified type or not and returns the value accordingly. Keywords are basically reserved words which have specific meaning relevant to a compiler. It takes an object reference as its first reference and a class or interface as its second operand and produces a result true or false based on the condition. It also lets the developer to create their own type classes having runtime behavior specified by the developer. aMoneyType, aDateType, aCheckingAccountID, etc are some to the examples of the such type of classes. 

Syntax: Here is the syntax for using an instanceOf keyword

if (node instanceof TreeNode){

/ write your code here

}

Where, condition (node instanceOf  TreeNode) returns true if the class node is an instance or is an instance of a subclass of TreeNode.

Example: Here is the example how the instanceOf operator is used.

public class MainClass {
  public static void main(String[] a) {
  String s = "Hello";
  if (s instanceof java.lang.String) { 
  System.out.println("is a String");
  }
  }
}

It returns the output "is a String" after checking the condition because the condition returns true.

Note: Here are some points which must be noted.

  • While applying the instanceOf operator on a null reference then it returns false.

转自:http://www.roseindia.net/help/java/i/instanceof-keyword.shtml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值