Java官方教程(三-3)相等 关系和条件运算符(2020.12.20)

前言

本文是橙子出于兴趣爱好对Java官方教程的尝试翻译,几乎每日更新,感兴趣的朋友可以关注一下橙子;翻译过程中尽可能多的对一些关键词保留了英文原文,如果你想看最纯正的英文原版教材却又看不懂,可以试着来看一下橙子的翻译版啊,欢迎大家留言讨论,冲鸭!
更多相关文章点击阅读
Java官方教程目录2020最新版

运算符 operators

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

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

相等和关系运算符用于确定一个operand(操作数)是否大于,小于,等于或不等于另一个operand。这些operators中大大多数你可能很熟悉。切记,在比较两个基本值(primitive values)是否相等时,必须用“==”而不是“=”。

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

下面程序ComparisonDemo演示了比较(comparison)运算符:

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");
    }
}

输出为:

value1 != value2
value1 <  value2
value1 <= value2

条件运算符 The Conditional Operators

&&和||运算符对两个布尔表达式执行逻辑与和逻辑或操作。这些运算符表现出“短路”行为,这意味着仅在需要时才计算第二个操作数。

&& Conditional-AND
|| Conditional-OR

下面的程序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");
    }
}

另一个条件运算符是?:,可以将其视为if-then-else的简写。该运算符也成为三元运算符,因为它使用了三个operators。在下面的示例中,这个运算符应该被读作:“如果someCondition为真”,则将value1的值赋给result,否则将value2的值赋给result。

下面程序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);
    }
}

因为someCondition为真,所以程序将“1”打印到屏幕上。如果想要使代码更易读,使用?:运算符而不是if-then-else语句;例如赋值时使用。

(备注:官方把&&,||和?:统称为条件运算符了,但是很多资料上是把&&,||称为逻辑运算符,单独把?:称为条件运算符。)

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

The instanceof运算符将一个object和指定类型比较。你可以用它来测试一个object是否是一个类的实例,一个接口的子类或者一个接口的实现。

下面程序InstanceofDemo定义了一个父类(Parent),一个简单接口(MyInterface),以及一个继承父类(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 {
    
    }

输出为:

obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true

使用instanceof运算符时,切记,null不是任何事物的实例(not an instance of anything)。

猜你喜欢

转载自blog.csdn.net/weixin_42509923/article/details/111413605