Java算术运算有加+,减-,乘*,除/,模%,前置++,后置++,前置--,后置-- 9个。
示例:运算符
packagecom.bwf.ch01.dao;
publicclassDemo1 {
publicstaticvoidmain(String[]args) {
inta= 23;
intb= 8;
intc=a+b;
System.out.println(String.format("%d+%d=%d",a,b,c));
c=a-b;
System.out.println(String.format("%d-%d=%d",a,b,c));
c=a*b;
System.out.println(String.format("%d*%d=%d",a,b,c));
c=a/b;
System.out.println(String.format("%d/%d=%d",a,b,c));
c=a%b;
System.out.println(String.format("%d%%%d=%d",a,b,c));
c=a++;//先读取a的值赋值给c,再给a加1
System.out.println(String.format("c=%d,a=%d",c,a));
a=23;
c= ++a;//先给a加1,再读取a的值赋值给c
System.out.println(String.format("c=%d,a=%d",c,a));
a=23;
c=a--;//先读取a的值赋值给c,再给a减1
System.out.println(String.format("c=%d,a=%d",c,a));
a=23;
c= --a;//先给a减1,再读取a的值赋值给c
System.out.println(String.format("c=%d,a=%d",c,a));
}
}
执行的结果如下图所示: