Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics):
The Type Comparison Operator instanceof
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
The following program, InstanceofDemo, defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println('obj1 instanceof Parent: ' + (obj1 instanceof Parent));
System.out.println('obj1 instanceof Child: ' + (obj1 instanceof Child));
System.out.println('obj1 instanceof MyInterface: ' + (obj1 instanceof MyInterface));
System.out.println('obj2 instanceof Parent: ' + (obj2 instanceof Parent));
System.out.println('obj2 instanceof Child: ' + (obj2 instanceof Child));
System.out.println('obj2 instanceof MyInterface: ' + (obj2 instanceof MyInterface));
}
}
class Parent{}
class Child extends Parent implements MyInterface{}
interface MyInterface{}
Output:
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true
When using the
instanceof
operator, keep in mind that
null
is not an instance of anything.
分享到:
相关推荐
This book proposes such a model, one that enjoys several advantages over other approaches, not the least of which it is that it’s fully compatible with the well known relational model of data. ...
比较简短的C语言教程,英文资料 1.0 Introduction 1.1 The main() Function 1.2 Include Files 1.3 Whitespace 1.4 The Preprocessor 1.4.1 The #define Directive ...11.0 Differences between Java and C
The C programming Language 第二版英文版 內容列表 Table of Contents Preface.......................................................... Preface to the first edition.......................................
1 THE BASICS A1 1 1.1 The Scala Interpreter 1 1.2 Declaring Values and Variables 4 1.3 Commonly Used Types 5 1.4 Arithmetic and Operator Overloading 6 1.5 More about Calling Methods 8 1.6 The apply ...
The checked and unchecked Operators 134 The is Operator 135 The as Operator 136 The sizeof Operator 136 The typeof Operator 136 Contents Operator Precedence 137 Type Safety 137 Type Conversions 138 ...
7.10 Relational and type-testing operators 197 7.10.1 Integer comparison operators 197 7.10.2 Floating-point comparison operators 198 7.10.3 Decimal comparison operators 199 7.10.4 Boolean equality ...
Restriction Operators Projection Operators Partitioning Operators Join Operators Concatenation ...Equality Operators Element Operators Generation Operators Quantifiers Aggregate Operators
Non linear equality and inequality constrained PSO(非线性等式与不等式约束PSO)利用粒子群算法求解非线性等式和不等式约束的最小值。包括matlab完整代码。
基于渐进和重抽样二项分布的等价性检验,李杰,邓丽君,本文讨论了k(k>2)个独立二项分布比例的等价性检验问题。主要讨论了似然比,Socre和Wald统计量在MaxT,MinP和Step-down方法下的第一类错误的概�
programming inequality constraints to the equality constraint ,and that make the solving process more simple. This study focus on how to use the open source Hadoop cloud computing systems to build a...
script src =" date-equality.js " > </ script > 节点/ RequireJS // Typescript/ES6 import * as DE from 'date-equality' ; // ES5 var DE = require ( 'date-equality' ) ; 原料药 sameDate 如果两...
7.3 Idiomatic natural language command and question interfaces 8. Prototyping with Prolog 8.1 Action specification for a simple calculator 8.2 Animating the 8-puzzle (§5.2) using character graphics ...
As every C++ programmer knows, the language has many powerful features, but this power brings with it complexity, which in turn can make code more bug-prone and harder to read and maintain....
Operators And Special Methods Builtins Build and C API Changes Performance Porting To Python 3.0 What’s New in Python 2.7 The Future for Python 2.x Changes to the Handling of Deprecation ...
Spot the advantages of the dynamic and Expression types over reflection (see Items 42 and 43) Assess why query expressions are better than loops (see Item 8) Understand how generic covariance and ...
Chapter 1—The LISP Language Chapter 2—Emacs Lisp: A Short Description 2.1. GNU Emacs and Emacs Lisp 2.2. Lisp Lists 2.2.1. Parts of Lisp 2.3. Example: Two Plus Two 2.4. Evaluation 2.5. A ...
The methods are directly applicable to language implementation, to the development of logics for reasoning about programs, and to the formal verification language properties such as type safety....
2.7.2 Relational, Equality, and Logical Operators.. . 47 2.8 Statements......... . . 50 2.8.1 Assignment and Expressions..... . . . 51 2.8.2 The Compound Statement...... . 52 2.8.3 The if and if-else ...
The Equality Operator 120 Conversion Operators 121 Putting Operators to Work 121 7. Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...