运算符:是一种特殊符号,用以表示数据的运算、赋值和比较。
表达式:使用运算符将运算数据连接起来的符合Java语法规则的式子。
一、运算表达式
1.算术运算符
+ - * / 整除求整商 % 求余数 ++ -- 运算
/*
* Copyright (c) 2020, 2023, 3044483124@qq.com All rights reserved.
*
*/
package itcast;
/**
* <p>Project: JavaStudy - Opt1
* <p>Powered by jiangbo On 2023-01-31
* 14:45:09
* <p>Created by IntelliJ IDEA
*
* @author jiangbo [3044483124@qq.com]
* @version 1.0
* @since 8/17
*/
public class Opt1 {
public static void main(String[] args) {
int a = 10;
int b = 2;
System.out.printf("%d + %d = %d%n", a, b, a + b);
System.out.printf("%d - %d = %d%n", a, b, a - b);
System.out.printf("%d × %d = %d%n", a, b, a * b);
//求整商
System.out.printf("%d ÷ %d = %d%n", a, b, a / b);
//求余数
System.out.printf("%d %% %d = %d%n", a, b, a % b);
int c = 5;
//变量自身值加1
++c;
System.out.println(c);
//d = 6,c = 7
int d = c++;
//int d = ++c;
System.out.println(c);
System.out.println(d);
d--;
System.out.println(d);
System.out.println(--d);
}
}
2.赋值运算符
= += -= *= /= %= (java 程序中 = 赋值 == 判断是不是同一个对象)
/*
* Copyright (c) 2020, 2023, 3044483124@qq.com All rights reserved.
*
*/
package itcast;
/**
* <p>Project: JavaStudy - Opt2
* <p>Powered by jiangbo On 2023-01-31
* 15:39:44
* <p>Created by IntelliJ IDEA
*
* @author jiangbo [3044483124@qq.com]
* @version 1.0
* @since 8/17
*/
public class Opt2 {
public static void main(String[] args) {
int a = 10, b = 3;
int c = 2;
c += a * b;
System.out.println(c);
c = c + a * b;
System.out.println(c);
//50
System.out.println(c-=12);
System.out.println(c*=2);