`
javafox
  • 浏览: 55853 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

阅读更多
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.
分享到:
评论

相关推荐

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    - **Relational and Equality Operators**: Checking relationships and equality between operands. - **Logical Operators**: AND, OR, and NOT operators for boolean expressions. - **Bitwise Operators**: ...

    C PROGRAMMING TUTORIAL

    - **Influences:** Influenced by earlier languages like BCPL and B, and later influenced languages like C++, Java, and Python. #### Why use C? C is chosen for its simplicity, efficiency, and ...

    Professional C# 3rd Edition

    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 ...

    The Godel Programming Language

    《The Godel Programming Language》是一本由Patricia Hill和John Lloyd撰写,于1994年由MIT出版社出版的书籍。书中深入探讨了逻辑编程语言Godel的特点、语法、语义和应用实例等。 逻辑编程(Logic Programming)是...

    JavaScript in 10 Minutes

    5. **Equality Operators**: Describes the differences between strict (`===`) and loose (`==`) equality comparisons and the potential issues with type coercion. 6. **Boxed vs. Unboxed Values**: Explains...

    Non linear equality and inequality constrained PSO(利用粒子群算法求解非线性等式和不等式约束的最小值)

    Non linear equality and inequality constrained PSO(非线性等式与不等式约束PSO)利用粒子群算法求解非线性等式和不等式约束的最小值。包括matlab完整代码。

    Testing the equality of several independent binomial proportions based on asymptotic and resampling method

    基于渐进和重抽样二项分布的等价性检验,李杰,邓丽君,本文讨论了k(k>2)个独立二项分布比例的等价性检验问题。主要讨论了似然比,Socre和Wald统计量在MaxT,MinP和Step-down方法下的第一类错误的概�

    Java 自学宝典 第二章 数据类型

    ##### 2.6.10 Compare integers using if statements, relational operators and equality operators 可以使用关系运算符和等号运算符来比较整数。 ```java int a = 10; int b = 20; if (a ) { System.out....

    Problem Solving with C++ (7th edition)

    - **Common Bugs with = and ==**: Explanation of common mistakes made by beginners when using the assignment (=) and equality (==) operators, along with examples of correct usage. **Programming ...

    基于云计算的中文文本分类方法的研究

    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...

    date-equality

    script src =" date-equality.js " > </ script > 节点/ RequireJS // Typescript/ES6 import * as DE from 'date-equality' ; // ES5 var DE = require ( 'date-equality' ) ; 原料药 sameDate 如果两...

    prolog manual.pdf

    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 ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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....

    python3.6.5参考手册 chm

    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 ...

    HPL: Vol. IV: Functional and Logic Programming Languages

    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 ...

    Practical.Foundations.for.Programming.Languages.2nd

    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....

Global site tag (gtag.js) - Google Analytics