public class helloworld {
public static void main(String[] args) {
System.out.println(7 / 2);
//当整数除以整数的时候,会把结果的小数部分舍弃,只保留整数部分
}
}
上方代码的结果为3
public class helloworld {
public static void main(String[] args) {
System.out.println(7.0 / 2);
//当整数除以整数的时候,会把结果的小数部分舍弃,只保留整数部分
}
}
上方代码的结果为3.5
++: - -:(同++)
自增/自减(前):先运算后取值,++i
自增/自减(后):先取值后运算,i++
例:
public class helloworld {
public static void main(String[] args) {
int i = 0;
int k = i++;
System.out.println(k);
System.out.println(i);
}
}
//此时的运算结果为k=0,i=1
public class helloworld {
public static void main(String[] args) {
int i = 0;
int k = ++i;
System.out.println(k);
System.out.println(i);
}
}
//此时的运算结果为k=1,i=1
public class helloworld {
public static void main(String[] args) {
String str = "h" + "e" + "llo";
System.out.println(str);
}
}
//字符串的加号是字符串的拼接
算术运算符的注意问题
如果对负数取模,可以把模数负号忽略不记,如 5%-2=1。但是被模数是负数则不可忽略,如 -5%2=-1。此外,取模运算的结果不一定总是整数。“+”除字符串相加功能外,还能把非字符串转换成字符串。例如:
public class helloworld {
public static void main(String[] args) {
System.out.println("5+5="+5+5);
}
}
//该代码的结果为5+5=55
赋值运算符
赋值运算符就是给变量提供数值,要求变量的数据类型与所提供的数值的数据类型一致,若不一致,必须转化为与变量的数据类型一致。
赋值运算符分两种:基本赋值运算符和复合赋值运算符。
1)基本赋值运算符
格式:变量名=表达式;
功能:首先计算表达式的值,然后将表达式的值赋值给变量。
例如: i = 75+25; //i的值为100
(2) 复合赋值运算符(扩展)
对于a=a+x形式的赋值语句,可简化:a+=x,将“+=” 运算符称为复合赋值运算符。
public class helloworld {
public static void main(String[] args) {
short s = 2;
s = (short)(s + 3);
//这里必须做强制类型转换,否则会报错
s += 3;
//在使用扩展赋值运算符时,变量在参与运算时会把结果自动强制转换为当前变量的类型
System.out.println(s);
}
}
public class helloworld {
public static void main(String[] args) {
int i = 1;
i *= 0.1;
//等价于下面这种写法0.1转换成整数就相当于是0了
//i = (int) (i * 0.1);
System.out.println(i);
}
}
//运行结果为:i=0
比较运算符
public class helloworld {
public static void main(String[] args) {
System.out.println(4 == 5);
}
}
//运行结果为:false
逻辑运算符
Java的逻辑运算符包括:
(1)!(否定):取反
(2)&&(与):有一个为假,整个结果为假
(3)||(或):有一个为真,整个结果为真
public class test {
public static void main(String[] args) {
System.out.println(4 == 4 & 1 == 1);
}
}
//结果为:true
逻辑运算符用于连接布尔型表达式,在Java中不可以写成3 “|”和“||”的区别同理,||表示:当左边为真,右边不参与运算。
注意: 位运算符是直接对二进制进行运算 无符号右移 >>> :正数的 >>> 与 >> 一致 获取两个数中较大的数 获取三个数中较大的数
单&时,左边无论真假,右边都进行运算;
双&时,如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算。
注意区别以下两个例子:
public class test {
public static void main(String[] args) {
int i = 0;
int k = 1;
//左边false,右边true;结果为false
System.out.println(i != 0 & ++k == 2);
//单&时,左边无论真假,右边都进行运算;
System.out.println(k);
//此时k=2
}
}
public class test {
public static void main(String[] args) {
int i = 0;
int k = 1;
//左边false,右边true;结果为false
System.out.println(i != 0 && ++k == 2);
//双&时,如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算。
System.out.println(k);
//此时k=1
}
}
仔细体会以下四个例子,掌握&、&&、|、||。public class test {
public static void main(String[] args) {
int x = 1;
int y = 1;
if( x++ == 2 & ++y == 2) {
x=7;
}
System.out.println("x="+x+",y="+y);
}
}
//运行结果为x=2,y=2
public class test {
public static void main(String[] args) {
int x = 1;
int y = 1;
if( x++ == 2 && ++y == 2) {
x=7;
}
System.out.println("x="+x+",y="+y);
}
}
//运行结果为x=2,y=1
public class test {
public static void main(String[] args) {
int x = 1;
int y = 1;
if( x++ == 1 | ++y == 1) {
x=7;
}
System.out.println("x="+x+",y="+y);
}
}
//运行结果为x=7,y=2
public class test {
public static void main(String[] args) {
int x = 1;
int y = 1;
if( x++ == 1 || ++y == 1) {
x=7;
}
System.out.println("x="+x+",y="+y);
}
}
//运行结果为x=7,y=1
位运算符
public class test {
public static void main(String[] args) {
System.out.println(1 << 2);
//1*2的2次方,结果为4
System.out.println(3 << 3);
//3*2的3次方,结果为24
}
}
public class test {
public static void main(String[] args) {
System.out.println(8 >> 2);
System.out.println(8 >>> 2);
//以上俩输出均为2
System.out.println(-8 >> 2);
//输出为-2
System.out.println(-8 >>> 2);
//输出为1073741822
}
}
三目运算符
public class test {
public static void main(String[] args) {
int i = 1;
int k = i > 0 ? 1 : 0;
System.out.println(k);
}
}
//输出为1
public class test {
public static void main(String[] args) {
//获取两个数中较大的数
int m = 1;
int n = 2;
System.out.println(m > n ? m : n);
}
}
//输出为:2
public class test {
public static void main(String[] args) {
//获取三个数中较大的数
int m = 1;
int n = 2;
int k = 0;
System.out.println(m > n ? (m > k ? m : k) : (n > k ? n : k));
}
}
//输出为:2
运算符的优先级



