day02-运算符_算术运算符

This commit is contained in:
2026-07-16 09:47:16 +08:00
parent 8a36d49f8a
commit fa57bc8100
6 changed files with 35 additions and 5 deletions
@@ -0,0 +1,29 @@
package com.inmind.yunsuan02;
/**
算数运算符:+ - * / % ++ --
注意:
1.算数运算时,运算符前后的数据类型如果一致,那么结果也是该类型
2.算数运算时,运算符前后的数据类型如果不一致,那么结果是大范围的数据类型
有特例:byte,short,char,这3个类型在运算时,都会提升为int
*/
public class Demo06 {
public static void main(String[] args) {
System.out.println(1+3);//4
System.out.println(1-3);//-2
System.out.println(1*3);//3
System.out.println(10/3);//3
System.out.println(10%3);//1
System.out.println("--------------");
System.out.println(3124/1000*1000);//3000
System.out.println(3124/1000.0*1000);//3124.0
System.out.println("----------以下是特殊情况------------");
byte b1 = 3;
byte b2 = 5;
int sum = b1+b2;//byteshort,char,这3个类型在运算时,都会提升为int
byte sum1 = (byte)(b1+b2);
System.out.println(sum);
}
}