Documentation

The Java™ Tutorials
Hide TOC
Assignment, Arithmetic, and Unary Operators赋值运算符、算术运算符和一元运算符
Trail: Learning the Java Language
Lesson: Language Basics
Section: Operators

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

The Simple Assignment Operator简单赋值运算符

One of the most common operators that you'll encounter is the simple assignment operator "=".您将遇到的最常见的运算符之一是简单赋值运算符“=”。You saw this operator in the Bicycle class; it assigns the value on its right to the operand on its left:你在自行车课程上见过这个运算符;它将右边的值赋给左边的操作数:

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

This operator can also be used on objects to assign object references, as discussed in Creating Objects.此运算符也可用于对象以指定对象引用,如创建对象中所述。

The Arithmetic Operators算术运算符

The Java programming language provides operators that perform addition, subtraction, multiplication, and division.Java编程语言提供了执行加法、减法、乘法和除法的运算符。There's a good chance you'll recognize them by their counterparts in basic mathematics.你很有可能会被基础数学的对手认出。The only symbol that might look new to you is "%", which divides one operand by another and returns the remainder as its result.唯一对您来说可能是新的符号是“%”,它将一个操作数除以另一个操作数,并返回余数作为其结果。

Operator运算符 Description描述
+Additive operator (also used for String concatenation)加法运算符(也用于字符串连接)
-Subtraction operator减法运算符
*Multiplication operator乘法运算符
/Division operator除法运算符
%Remainder operator余数运算符

The following program, ArithmeticDemo, tests the arithmetic operators.下面的程序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);
    }
}

This program prints the following:此程序打印以下内容:

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

You can also combine the arithmetic operators with the simple assignment operator to create compound assignments.您还可以将算术运算符与简单赋值运算符组合以创建复合赋值For example, x+=1; and x=x+1; both increment the value of x by 1.例如,x+=1x=x+1两者都将x的值增加1。

The + operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo program:+运算符也可用于将两个字符串连接在一起,如以下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);
    }
}

By the end of this program, the variable thirdString contains "This is a concatenated string.", which gets printed to standard output.在本程序结束时,变量thirdString包含“This is a concatenated string.”,它被打印到标准输出。

The Unary Operators一元运算符

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.一元运算符只需要一个操作数;它们执行各种操作,例如将值递增/递减1、对表达式求反或反转布尔值。

Operator运算符 Description描述
+Unary plus operator; indicates positive value (numbers are positive without this, however)一元加运算符;表示正值(但是,如果没有此值,则数字为正值)
-Unary minus operator; negates an expression一元负运算符;否定一个表达式
++Increment operator; increments a value by 1增量运算符;将值增加1
--Decrement operator; decrements a value by 1减量运算符;将值递减1
!Logical complement operator; inverts the value of a boolean逻辑补码运算符;反转布尔值

The following program, UnaryDemo, tests the unary operators:下面的程序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);
    }
}

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand.递增/递减运算符可以应用于操作数之前(前缀)或之后(后缀)。The code result++; and ++result; will both end in result being incremented by one.代码result++;++result;result都将增加1。The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value.唯一的区别是前缀版本(++result)的计算结果为递增值,而后缀版本(result++)的计算结果为原始值。If you are just performing a simple increment/decrement, it doesn't really matter which version you choose.如果您只是执行一个简单的递增/递减操作,那么选择哪个版本并不重要。But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.但是,如果在较大表达式的一部分中使用此运算符,则选择的运算符可能会产生显著的差异。

The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:以下程序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);
    }
}

Previous page: Operators
Next page: Equality, Relational, and Conditional Operators