11

Java官方教程(三-2)赋值 运算和一元运算符(2020.12.19)

 3 years ago
source link: https://blog.csdn.net/weixin_42509923/article/details/111400012
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

运算符 operators

赋值,运算和一元运算符 Assignment, Arithmetic, and Unary Operators

简单赋值运算符 The Simple Assignment Operator

你将遇到的最常见的运算符之一是简单的赋值运算符“=”。你已经在Bicycle类中看到了该运算符;它将右边的value赋给左边的操作数(operand)。

 int cadence = 0;
 int speed = 0;
 int gear = 1;

如Creating Objects章节所述,该运算符还可以用于给objects分配对象引用(object references)。

算术运算符 The Arithmetic Operators

Java语言提供了加减乘除的运算符。你在基础数学中已经很好地认识了它们。唯一看起来比较新的符号是“%”,它将一个operand除以另一个,然后返回余数作为结果。

在这里插入图片描述
下面的程序ArithmeticDemo测试了算术运算符:
class ArithmeticDemo {

    public static void main (String[] args) {

        int result = 1 + 2;
        // result is now 3
        System.out.println("1 + 2 = " + result);
        int original_result = result;

        result = result - 1;
        // result is now 2
        System.out.println(original_result + " - 1 = " + result);
        original_result = result;

        result = result * 2;
        // result is now 4
        System.out.println(original_result + " * 2 = " + result);
        original_result = result;

        result = result / 2;
        // result is now 2
        System.out.println(original_result + " / 2 = " + result);
        original_result = result;

        result = result + 8;
        // result is now 10
        System.out.println(original_result + " + 8 = " + result);
        original_result = result;

        result = result % 7;
        // result is now 3
        System.out.println(original_result + " % 7 = " + result);
    }
}

1 + 2 = 3
3 - 1 = 2
2 * 2 = 4
4 / 2 = 2
2 + 8 = 10
10 % 7 = 3

你也可以将算术运算符与简单赋值运算符结合起来去创建复合赋值。例如,x+=1;和x=x+1;都将x的值增加1。

+运算符还可以用于两个字符串(strings)的连接,如下面的ConcatDemo程序所示:

class ConcatDemo {
    public static void main(String[] args){
        String firstString = "This is";
        String secondString = " a concatenated string.";
        String thirdString = firstString+secondString;
        System.out.println(thirdString);
    }
}

最终,变量thirdString包含“This is a concatenated string.”。该字符串将被打印到标准输出中。

一元运算符 The Unary Operators

一元运算符只需要一个operand;它们执行各种操作,例如将值增加/减少1,否定表达式或者将布尔值取反。

在这里插入图片描述

下面的程序UnaryDemo测试一元运算符:

class UnaryDemo {

    public static void main(String[] args) {

        int result = +1;
        // result is now 1
        System.out.println(result);

        result--;
        // result is now 0
        System.out.println(result);

        result++;
        // result is now 1
        System.out.println(result);

        result = -result;
        // result is now -1
        System.out.println(result);

        boolean success = false;
        // false
        System.out.println(success);
        // true
        System.out.println(!success);
    }
}

可以在operand之前或之后运用增加/减少运算符。代码result++和++result都将导致result加1。唯一的区别是前缀版本(++result)的值为增量值,后缀版本(result++)的值为原始值。如果你只是执行简单地加减运算,选择哪个版本都行。但是,如果在较大表达式中使用,两者将产生不同的结果。
下面的程序PrePostDemo演示了前缀/后缀一元增加运算符:

class PrePostDemo {
    public static void main(String[] args){
        int i = 3;
        i++;
        // prints 4
        System.out.println(i);
        ++i;			   
        // prints 5
        System.out.println(i);
        // prints 6
        System.out.println(++i);
        // prints 6
        System.out.println(i++);
        // prints 7
        System.out.println(i);
    }
}

橙子一直都是保持日更,想系统学习Java的小伙伴如果跟着橙子走下来,相信会有不小的收获


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK