Java 基本语法---Java运算符

Posted ryanjie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 基本语法---Java运算符相关的知识,希望对你有一定的参考价值。

Java 基本语法---Java运算符


0. 概述

Java中的运算符主要分为以下几种:

  • 算术运算符
  • 赋值运算符
  • 关系运算符
  • 逻辑运算符
  • 条件运算符
  • 位运算符
  • 其他运算符

1. 算术运算符

操作符 描述
+ 加法,加号两侧的两个数相加
- 减法,减号左侧的数减去右侧的数
* 乘法,乘号两侧的两个数相乘
/ 除法,除号左侧的数除以右侧的数
% 取模。左侧的数除以右侧的数的余数
++ 自增,操作数的值增1
-- 自减,操作数的值减1
public class ArithmeticTest {
    public static void main(String[] args) {
        double number1 = 12.390;
        double number2 = 74.340;
        int number3 = 4;

        System.out.println(number2 + " + " + number1 + " = " + (number2 + number1));
        System.out.println(number2 + " - " + number1 + " = " + (number2 - number1));
        System.out.println(number1 + " * " + number3 + " = " + (number1 * number3));
        System.out.println(number2 + " / " + number1 + " = " + (number2 / number1));
        System.out.println(number1 + " % " + number3 + " = " + (float)(number1 % number3));

        int number4 = 5;
        int number5 = 5;
        int number = number4++;
        System.out.println("number4++ = " + number + ", number4 = " + number4);

        number = ++number5;
        System.out.println("++number5 = " + number + ", number5 = " + number5);

        System.out.println("(number++) = " + (number++));;
        System.out.println("(number--) = " + (number--));
    }
}

Output:
    74.34 + 12.39 = 86.73
    74.34 - 12.39 = 61.95
    12.39 * 4 = 49.56
    74.34 / 12.39 = 6.0
    12.39 % 4 = 0.39
    number4++ = 5, number4 = 6
    ++number5 = 6, number5 = 6
    (number++) = 6
    (number--) = 7

2. 赋值运算符

操作符 描述 举例
= 简单的赋值运算符,将右侧操作数的值赋给左侧的操作数 C = A+B 将A+B的值赋值给C
+= 加和赋值操作符,它把左操作数和右操作数相加赋值给左操作数 C + = A等价于C = C + A
-= 减和赋值操作符,它把左操作数和右操作数相减赋值给左操作数 C - = A等价于C = C - A
*= 乘和赋值操作符,它把左操作数和右操作数相乘赋值给左操作数 C * = A等价于C = C * A
/= 除和赋值操作符,它把左操作数和右操作数相除赋值给左操作数 C / = A等价于C = C / A
(%)= 取模和赋值操作符,它把左操作数和右操作数取模后赋值给左操作数 C%= A等价于C = C%A
<<= 左移位赋值运算符 C << = 2等价于C = C << 2
>>= 右移位赋值运算符 C >> = 2等价于C = C >> 2
&= 按位与赋值运算符 C&= 2等价于C = C&2
^= 按位异或赋值操作符 C ^ = 2等价于C = C ^ 2
|= 按位或赋值操作符 C | = 2等价于C = C | 2
public class AssignmentTest {
    public static void main(String[] args) {
        int a = 100;
        int b = 200;
        int c = 4;

        System.out.println("a (a += b) = " + (a += b));
        System.out.println("a (a -= 100) = " + (a -= 100));
        System.out.println("a (a *= c) = " + (a *= c));
        System.out.println("a (a /= 100) = " + (a /= 100));
        System.out.println("a (a %= 100) = " + (a %= 100));
        System.out.println("c ( c<<2 ) = " + ( c << 2 ));
        System.out.println("c ( c>>2 ) = " + ( c >> 2 ));
        
    }
}

Output:
    a (a += b) = 300
    a (a -= 100) = 200
    a (a *= c) = 800
    a (a /= 100) = 8
    a (a %= 100) = 8
    c ( c<<2 ) = 16
    c ( c>>2 ) = 1

3.关系运算符

运算符 描述
== 检查如果两个操作数的值是否相等,如果相等则条件为真。
!= 检查如果两个操作数的值是否相等,如果值不相等则条件为真。
> 检查左操作数的值是否大于右操作数的值,如果是那么条件为真。
< 检查左操作数的值是否小于右操作数的值,如果是那么条件为真。
>= 检查左操作数的值是否大于或等于右操作数的值,如果是那么条件为真。
<= 检查左操作数的值是否小于或等于右操作数的值,如果是那么条件为真。
public class RelationalTest {
    public static void main(String[] args) {
        int a = 100;
        int b = 88;

        System.out.println("(a == b) = " + (a == b ));
        System.out.println("(a != b) = " + (a != b ));
        System.out.println();
        System.out.println("(a <= b) = " + (a <= b ));
        System.out.println("(a >= b) = " + (a >= b ));
        System.out.println();
        System.out.println("(a > b) = " + (a > b ));
        System.out.println("(a < b) = " + (a < b ));
    }
}

Output:
    (a == b) = false
    (a != b) = true

    (a <= b) = false
    (a >= b) = true

    (a > b) = true
    (a < b) = false

4. 逻辑运算符

操作符 描述
&& 称为逻辑与运算符。当且仅当两个操作数都为真,条件才为真。
|| 称为逻辑或操作符。如果任何两个操作数任何一个为真,条件为真。
称为逻辑非运算符。用来反转操作数的逻辑状态。如果条件为true,则逻辑非运算符将得到false。
public class LogicalTest {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;

        System.out.println("(a && b) = " + (a && b));
        System.out.println("(a || b) = " + (a || b));
        System.out.println("!(a && b) = " + !(a && b));

        //短路逻辑运算符&&:先判断前一个条件,true则执行第二个判断操作,为false则不在执行
        int c = 100;
        System.out.println((c > 100) && (c++ < 150));
        System.out.println("c = "  + c);
        System.out.println();
        System.out.println((c++ < 150) && (c > 100));
        System.out.println("c = "  + c);
    }
}

Output:
    (a && b) = false
    (a || b) = true
    !(a && b) = true
    false
    c = 100

    true
    c = 101

5. 条件运算符

运算符有3个操作数,需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。

eg: a = ( b == 1) ? 20 : 30 ;

这一条语句:先判断 b是否为1,为 true 则将 a设置为 20,为 false 则将 b 设置为 30;

public class ConditionalTest {
    public static void main(String[] args) {
        int a;
        int b = 100;

        a = (b == 100) ? 66 : 88;
        System.out.println("a =" + a);
        System.out.println();
        b = (a == 66) ? 888 : 88;
        System.out.println("b =" + b);
    }
}

Output:
a =66

b =888

6.案例

判断输入的年份是否为闰年;

LeapYear : 能被400整除的年份和能被4整除但是不能被100整除的年份

public class LeapYearDemo {
    //LeapYear:能被400整除的年份和能被4整除但是不能被100整除的年份
    public static void main(String[] args) {
        System.out.println("请输入要判断的年份: ");
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();

        if ((year % 400 == 0) || (year % 4 == 0 && year %100 != 0 ))
        {
            System.out.println(year + "是闰年!");
        }
        else
        {
            System.out.println(year + "不是闰年!");
        }
    }
}


Output:
    请输入要判断的年份: 
    1997
    1997不是闰年!
    
    请输入要判断的年份: 
    2020
    2020是闰年!

以上是关于Java 基本语法---Java运算符的主要内容,如果未能解决你的问题,请参考以下文章

Java开发技术栈:Java基础语法流程控制语句运算符

Java 基本语法---Java运算符

Java基本语法--运算符

java学习——第二章《java语法》

尚硅谷Java基础笔记——2. Java基本语法(上)·变量和运算符

Java-Day03,基本语法