Documentation

The Java™ Tutorials
Hide TOC
Equality, Relational, and Conditional Operators相等运算符、关系运算符和条件运算符
Trail: Learning the Java Language
Lesson: Language Basics
Section: Operators

Equality, Relational, and Conditional Operators相等运算符、关系运算符和条件运算符

The Equality and Relational Operators相等与关系运算符

The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand.相等运算符和关系运算符确定一个操作数是大于、小于、等于还是不等于另一个操作数。The majority of these operators will probably look familiar to you as well.这些操作员中的大多数可能对您来说也很熟悉。Keep in mind that you must use "==", not "=", when testing if two primitive values are equal.请记住,在测试两个基本值是否相等时,必须使用“==”,而不是“=”。

==      equal to
!=      not equal to
> greater than
>=      greater than or equal to
<       less than
<=      less than or equal to

The following program, ComparisonDemo, tests the comparison operators:以下程序ComparisonDemo测试比较运算符:

class ComparisonDemo {

    public static void main(String[] args){
        int value1 = 1;
        int value2 = 2;
        if(value1 == value2)
            System.out.println("value1 == value2");
        if(value1 != value2)
            System.out.println("value1 != value2");
        if(value1 > value2)
            System.out.println("value1 > value2");
        if(value1 < value2)
            System.out.println("value1 < value2");
        if(value1 <= value2)
            System.out.println("value1 <= value2");
    }
}

Output:输出:

value1 != value2
value1 <  value2
value1 <= value2

The Conditional Operators条件运算符

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions.&&||运算符对两个布尔表达式执行“条件与”和“条件或”运算。These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.这些运算符表现出“短路”行为,这意味着只有在需要时才计算第二个操作数。

&& Conditional-AND
|| Conditional-OR

The following program, ConditionalDemo1, tests these operators:以下程序ConditionalDemo1测试这些运算符:

class ConditionalDemo1 {

    public static void main(String[] args){
        int value1 = 1;
        int value2 = 2;
        if((value1 == 1) && (value2 == 2))
            System.out.println("value1 is 1 AND value2 is 2");
        if((value1 == 1) || (value2 == 1))
            System.out.println("value1 is 1 OR value2 is 1");
    }
}

Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson).另一个条件运算符是?:,可以将其视为if-then-else语句的简写(在本课程的控制流语句部分中讨论)。This operator is also known as the ternary operator because it uses three operands.此运算符也称为三值运算符,因为它使用三个操作数。In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result.在以下示例中,此运算符应理解为:“如果someConditiontrue,则将value1的值指定给resultOtherwise, assign the value of value2 to result."否则,将value2的值指定给result。”

The following program, ConditionalDemo2, tests the ?: operator:以下程序ConditionalDemo2测试?:运算符:

class ConditionalDemo2 {

    public static void main(String[] args){
        int value1 = 1;
        int value2 = 2;
        int result;
        boolean someCondition = true;
        result = someCondition ? value1 : value2;

        System.out.println(result);
    }
}

Because someCondition is true, this program prints "1" to the screen.由于someConditiontrue,此程序将在屏幕上打印“1”。Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).如果使代码更具可读性,请使用?:运算符而不是if-then-else语句;例如,当表达式紧凑且没有副作用(例如赋值)时。

The Type Comparison Operator instanceof类型比较运算符instanceof

The instanceof operator compares an object to a specified type.instanceof运算符将对象与指定类型进行比较。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.下面的程序InstanceofDemo定义了一个父类(名为Parent)、一个简单接口(名为MyInterface)和一个从父类继承并实现该接口的子类(名为Child)。

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.使用instanceof运算符时,请记住null不是任何对象的实例。


Previous page: Assignment, Arithmetic, and Unary Operators
Next page: Bitwise and Bit Shift Operators