📜  Java运算符

📅  最后修改于: 2020-09-26 15:15:08             🧑  作者: Mango

在本教程中,您将通过示例了解Java中不同类型的运算符 ,它们的语法以及如何使用它们。

运算符是对操作数(变量和值)进行运算的特殊符号(字符)。例如, +是执行加法的运算符 。

在上一教程中,您将了解Java变量。您将学习声明变量并为变量分配值。现在,您将学习使用运算符来操纵变量。


赋值运算符

赋值运算符在Java中用于为变量赋值。例如,

int age;
age = 5;

赋值运算符将其右侧的值分配给其左侧的变量。在这里,使用= 运算符将5分配给变量age

还有其他赋值运算符 。但是,为了简单起见,我们将在本文后面学习其他赋值运算符 。


示例1:赋值运算符

class AssignmentOperator {
    public static void main(String[] args) {
        
        int number1, number2;
        
        // Assigning 5 to number1 
        number1 = 5;
        System.out.println(number1);
                
        // Assigning value of variable number2 to number1
        number2 = number1;
        System.out.println(number2);
    }
}

输出

5
5

算术运算符

算术运算运算符用于执行数学运算,例如加法,减法,乘法等。

Operator Meaning
+ Addition (also used for string concatenation)
Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator

示例2:算术运算符

class ArithmeticOperator {
    public static void main(String[] args) {
        
        double number1 = 12.5, number2 = 3.5, result;
        
        // Using addition operator
        result = number1 + number2;
        System.out.println("number1 + number2 = " + result);
        
        // Using subtraction operator
        result = number1 - number2;
        System.out.println("number1 - number2 = " + result);
        
        // Using multiplication operator
        result = number1 * number2;
        System.out.println("number1 * number2 = " + result);

        // Using division operator
        result = number1 / number2;
        System.out.println("number1 / number2 = " + result);
        
        // Using remainder operator
        result = number1 % number2;
        System.out.println("number1 % number2 = " + result);
    }
}

输出

number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.5714285714285716
number1 % number2 = 2.0

在上面的示例中,所有使用的操作数都是变量。但是,根本没有必要。算术运算运算符使用的操作数也可以是字面量 。例如,

result = number1 + 5.2;
result = 2.3 + 4.5;
number2 = number1 -2.9;

+ 运算符还可用于连接两个或多个字符串。


示例3:添加字符串的算术运算符

class ArithmeticOperator {
    public static void main(String[] args) {
        
        String start, middle, end, result;
        
        start = "Talk is cheap. ";
        middle = "Show me the code. ";
        end = "- Linus Torvalds";
        
        result = start + middle + end;
        System.out.println(result);
    }
}

输出

Talk is cheap. Show me the code. - Linus Torvalds

一元运算符

一元运算运算符仅对一个操作数执行运算。

Operator Meaning
+ Unary plus (not necessary to use since numbers are positive without using it)
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a boolean

示例4:一元运算符

class UnaryOperator {
    public static void main(String[] args) {
        
        double number = 5.2, resultNumber;
        boolean flag = false;
        
        System.out.println("+number = " + +number);
        // number is equal to 5.2 here.
        
        System.out.println("-number = " + -number);
        // number is equal to 5.2 here.
        
        // ++number is equivalent to number = number + 1
        System.out.println("number = " + ++number);
        // number is equal to 6.2 here.

        // -- number is equivalent to number = number - 1
        System.out.println("number = " + --number);
        // number is equal to 5.2 here.

        System.out.println("!flag = " + !flag);
        // flag is still false.
    }
}

输出

+number = 5.2
-number = -5.2
number = 6.2
number = 5.2
!flag = true

增减运算符

您也可以在Java中同时使用++和- 运算符作为前缀和后缀。 ++ 运算符将值增加1,而-- 运算符将值减少1。

int myInt = 5;
++myInt   // myInt becomes 6
myInt++   // myInt becomes 7
--myInt   // myInt becomes 6
myInt--   // myInt becomes 5

到目前为止很简单。但是,在使用递增和递减运算符作为前缀和后缀时,存在一个至关重要的区别。考虑这个例子,

示例5:一元运算符

class UnaryOperator {
    public static void main(String[] args) {
        
        double number = 5.2;

        System.out.println(number++);
        System.out.println(number);

        System.out.println(++number);
        System.out.println(number);
    }
}

输出

5.2
6.2
7.2
7.2

在这里,请注意这行,

System.out.println(number++);

执行此语句时,将首先评估原始值。然后数量增加。这就是获得5.2作为输出的原因。

现在,当线,

System.out.println(number);

将打印增加的值。那是6.2

但是,

System.out.println(++number);

首先将数字增加1 ,然后执行该语句。因此,输出为7.2

类似的是递减的情况下-- 运算符。


平等与关系运算符

等式和关系运算符确定两个操作数之间的关系。它检查操作数是否大于,小于,等于,不等于等。根据关系,它被评估为truefalse

Operator Description Example
== equal to 5 == 3 is evaluated to false
!= not equal to 5 != 3 is evaluated to true
> greater than 5 > 3 is evaluated to true
< less than 5 < 3 is evaluated to false
>= greater than or equal to 5 >= 5 is evaluated to true
<= less than or equal to 5 <= 5 is evaluated to true

在决策和循环中将使用等式和关系运算符 (稍后将进行讨论)。现在,检查这个简单的例子。


示例6:平等和关系运算符

class RelationalOperator {
    public static void main(String[] args) {
        
        int number1 = 5, number2 = 6;

        if (number1 > number2) {
            System.out.println("number1 is greater than number2.");
        }
        else {
            System.out.println("number2 is greater than number1.");
        }
    }
}

输出

number2 is greater than number1.

在这里,我们使用了> 运算符来检查number1是否大于number2

由于number2大于number1 ,因此表达式number1 > number2的计算结果为false

因此,将执行else内部的代码块,并跳过if内部的代码块。

如果您不理解上面的代码,请不要担心。如果…其他文章,您将在Java中详细学习它。

现在,请记住,相等运算符和关系运算符比较两个操作数,并被评估为truefalse


运算符实例

除了关系运算符,也有一种类型的比较运算符 instanceof其中对象进行比较,以指定类型。例如,

示例7:instanceof运算符

这是instanceof 运算符的示例。

class instanceofOperator {
    public static void main(String[] args) {
        
        String test = "asdf";
        boolean result;
        
        result = test instanceof String;
        System.out.println("Is test an object of String? " + result);
    }
}

输出

Is test an object of String? true

在这里,由于变量testString类型的。因此, instanceof 运算符返回true 。要了解更多信息,请访问Java instanceof。


逻辑运算符

逻辑运算符 || (条件OR)和&& (条件AND)对布尔表达式进行运算。这是他们的工作方式。

Operator Description Example
|| conditional-OR: true if either of the boolean expression is true false || true is evaluated to true
&& conditional-AND: true if all boolean expressions are true false && true is evaluated to false

示例8:逻辑运算符

class LogicalOperator {
    public static void main(String[] args) {
        
        int number1 = 1, number2 = 2, number3 = 9;
        boolean result;
        
        // At least one expression needs to be true for the result to be true
        result = (number1 > number2) || (number3 > number1);

        // result will be true because (number3 > number1) is true
        System.out.println(result);
                
        // All expression must be true from result to be true    
        result = (number1 > number2) && (number3 > number1);

        // result will be false because (number1 > number2) is false
        System.out.println(result);
    }
}

输出

true
false

注意 :逻辑运算符用于决策和循环。


三元运算符

条件运算符或三元运算符 ?:if-then-else语句的缩写。条件运算符的语法为:

variable = Expression ? expression1 : expression2

运作方式如下。

  • 如果Expressiontrue ,则将expression1分配给变量
  • 如果Expressionfalse ,则将expression2分配给变量

示例9:三元运算符

class ConditionalOperator {
    public static void main(String[] args) {
        
        int februaryDays = 29;
        String result;
        
         result =  (februaryDays == 28) ? "Not a leap year" : "Leap year";
         System.out.println(result);
    }
}

输出

Leap year

在这里,我们使用三元运算符检查年份是否为if年。要了解更多信息,请访问Java三元运算符。


按位和移位运算符

为了在Java中执行按位和位移位运算符 符 ,请使用这些运算符 。

Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR

这些运算符不常用。要了解更多信息,请访问Java按位和移位运算符。


更多分配运算符

在本文开头,我们仅讨论了一个赋值运算符 = 。除了该运算符,还有很多赋值运算符使我们编写更简洁的代码。

Operator Example Equivalent to
+= x += 5 x = x + 5
-= x -= 5 x = x – 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
<<= x <<= 5 x = x << 5
>>= x >>= 5 x = x >> 5
&= x &= 5 x = x & 5
^= x ^= 5 x = x ^ 5
|= x |= 5 x = x | 5

现在你知道了Java 的运算符 ,它的时间来了解其中当两个运算符共享一个共同的操作数在表达式运算符的计算顺序。要了解这一点,请访问Java Operator Precedence。