全部源码:https://github.com/name365/JavaSE-30Day
第2章Java基本语法(上)_变量与运算符
2.4 运算符
运算符是一种特殊的符号,用以表示数据的运算、赋值和比较等。
算术运算符赋值运算符比较运算符(关系运算符)逻辑运算符位运算符三元运算符
算术运算符
class Day3Test{
public static void main(String
[] args
) {
int num1
= 12;
int num2
= 5;
int resule1
= num1
/ num2
;
System
.out
.println(resule1
);
int result2
= num1
/ num2
* num2
;
System
.out
.println(result2
);
double result3
= num1
/ num2
;
System
.out
.println(result3
);
double result4
= num1
/ num2
+ 0.0;
double result5
= num1
/ (num2
+ 0.0);
double result6
= (double)num1
/ num2
;
double result7
= (double)(num1
/ num2
);
System
.out
.println(result5
);
System
.out
.println(result6
);
int m1
= 12;
int n1
= 5;
System
.out
.println("m1 % n1 = " + m1
% n1
);
int m2
= -12;
int n2
= 5;
System
.out
.println("m2 % n2 = " + m2
% n2
);
int m3
= 12;
int n3
= -5;
System
.out
.println("m3 % n3 = " + m3
% n3
);
int m4
= -12;
int n4
= -5;
System
.out
.println("m4 % n4 = " + m4
% n4
);
int a1
= 10;
int b1
= ++a1
;
System
.out
.println("a1 = " + a1
+ ",b1 = " + b1
);
int a2
= 10;
int b2
= a2
++;
System
.out
.println("a2 = " + a2
+ ",b2 = " + b2
);
int a3
= 10;
a3
++;
int b3
= a3
;
short s1
= 10;
s1
++;
System
.out
.println(s1
);
byte bb1
= 127;
bb1
++;
System
.out
.println("bb1 = " + bb1
);
int a4
= 10;
int b4
= a4
--;
System
.out
.println("a4 = " + a4
+ ",b4 = " + b4
);
}
}
算术运算符的注意问题
如果对负数取模,可以把模数负号忽略不记,如:5%-2=1。但被模数是负数则不可忽略。此外,取模运算的结果不一定总是整数。对于除号"/",它的整数除和小数除是有区别的:整数之间做除法时,只保留整数部分而舍弃小数部分。例如:intx=3510;x=x/1000*1000; x的结果是?"+"除字符串相加功能外,还能把非字符串转换成字符串.例如:System.out.println(“5+5=”+5+5); //打印结果是?5+5=55 ?
class AriExer{
public static void main(String
[] args
){
int num
= 187;
System
.out
.println("百位数:" + num
/100);
System
.out
.println("十位数:" + num
%100/10);
System
.out
.println("个位数:" + num
%10);
}
}
赋值运算符
符号:=
当"="两侧数据类型不一致时,可以使用自动类型转换或使用强制类型转换原则进行处理。支持连续赋值。 扩展赋值运算符:+=, -=, *=, /=, %=
class SetValueTest{
public static void main(String
[] args
) {
int i1
= 10;
int j1
= 10;
int i2
,j2
;
i2
= j2
= 10;
int i3
= 10,j3
= 20;
int num1
= 10;
num1
+= 2;
System
.out
.println(num1
);
int num2
= 12;
num2
%= 5;
System
.out
.println(num2
);
short s1
= 10;
s1
+= 2;
System
.out
.println(s1
);
}
}
class MkFan{
public static void main(String
[] args
) {
int i
= 1;
i
*= 0.1;
System
.out
.println(i
);
i
++;
System
.out
.println(i
);
int m
= 2;
int n1
= 3;
n1
*= m
++;
System
.out
.println("m=" + m
);
System
.out
.println("n1=" + n1
);
int n
= 10;
n
+= (n
++) + (++n
);
System
.out
.println(n
);
}
}
比较运算符
比较运算符的结果都是boolean型,也就是要么是true,要么是false。比较运算符"==“不能误写成”=" 。
class CompareTest{
public static void main(String
[] args
){
int i
= 10;
int j
= 20;
System
.out
.println(i
==j
);
System
.out
.println(i
= j
);
boolean b1
= true;
boolean b2
= false;
System
.out
.println(b2
== b1
);
System
.out
.println(b2
= b1
);
}
}
逻辑运算符
&—逻辑与| —逻辑或!—逻辑非&& —短路与|| —短路或^ —逻辑异或
逻辑运算符用于连接布尔型表达式,在Java中不可以写成3
练习
class LogicTest{
public static void main(String
[] args
){
boolean b1
= false;
int num1
= 10;
if(b1
& (num1
++ > 0)){
System
.out
.println("我现在在南京");
}else{
System
.out
.println("我现在在北京");
}
System
.out
.println("num1 = " + num1
);
boolean b2
= false;
int num2
= 10;
if(b2
&& (num2
++ > 0)){
System
.out
.println("我现在在南京");
}else{
System
.out
.println("我现在在北京");
}
System
.out
.println("num2 = " + num2
);
boolean b3
= true;
int num3
= 10;
if(b3
| (num3
++ > 0)){
System
.out
.println("我现在在南京");
}else{
System
.out
.println("我现在在北京");
}
System
.out
.println("num3 = " + num3
);
boolean b4
= true;
int num4
= 10;
if(b4
|| (num4
++ > 0)){
System
.out
.println("我现在在南京");
}else{
System
.out
.println("我现在在北京");
}
System
.out
.println("num4 = " + num4
);
}
}
class LogoinTest{
public static void main(String
[] args
){
boolean x
= true;
boolean y
= false;
short z
= 42;
if((z
++==42)&&(y
=true))z
++;
if((x
=false)||(++z
==45)) z
++;
System
.out
.println("z=" + z
);
}
}
位运算符
位运算是直接对整数的二进制进行的运算
*注意:无<<<
class BitTest{
public static void main(String
[] args
){
int i
= 21;
System
.out
.println("i << 2 :" + (i
<< 2));
System
.out
.println("i << 3 :" + (i
<< 3));
System
.out
.println("i << 20 :" + (i
<< 20));
System
.out
.println("i << 27 :" + (i
<< 27));
int m
= 12;
int n
= 5;
System
.out
.println("m & n :" + (m
& n
));
System
.out
.println("m & n :" + (m
| n
));
System
.out
.println("m & n :" + (m
^ n
));
int num1
= 10;
int num2
= 20;
num1
= num1
^ num2
;
num2
= num1
^ num2
;
num1
= num1
^ num2
;
System
.out
.println("num1 = " + num1
+ ",num2 = " + num2
);
}
}
class BitTest{
public static void main(String
[] args
){
int num1
= 10;
int num2
= 20;
num1
= num1
^ num2
;
num2
= num1
^ num2
;
num1
= num1
^ num2
;
System
.out
.println("num1 = " + num1
+ ",num2 = " + num2
);
}
}
三元运算符
class SanTest{
public static void main(String
[] args
) {
int m
= 12;
int n
= 5;
int max
= (m
> n
)? m
: n
;
System
.out
.println(max
);
double num
= (m
> n
) ? 2 : 1.0;
String str
= (m
> n
) ? "m大" : ((m
== n
)? "m和n相等" : "n大");
System
.out
.println(str
);
int n1
= 12;
int n2
= 30;
int n3
= -43;
int max1
= (n1
> n2
) ? n1
: n2
;
int max2
= (max1
> n3
) ? max1
: n3
;
System
.out
.println("三个数中的最大值是:" + max2
);
int max3
= (((n1
> n2
)? n1
: n2
) > n3
) ?((n1
> n2
) ? n1
: n2
) : n3
;
System
.out
.println("三个数中的最大值是:" + max3
);
if(m
> n
){
System
.out
.println(m
);
}else{
System
.out
.println(n
);
}
}
}
运算符的优先级
运算符有不同的优先级,所谓优先级就是表达式运算中的运算顺序。如右表,上一行运算符总优先于下一行。只有单目运算符、三元运算符、赋值运算符是从右向左运算的。
2.5 程序流程控制
流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑模块。其流程控制方式采用结构化程序设计中规定的三种基本流程结构,即:
顺序结构分支结构循环结构 顺序结构
程序从上到下逐行地执行,中间没有任何判断和跳转。 分支结构
根据条件,选择性地执行某段代码。有if…else和switch-case两种分支语句。 循环结构
根据循环条件,重复性的执行某段代码。有while、do…while、for三种循环语句。注:JDK1.5提供了foreach循环,方便的遍历集合、数组元素。
2.5.1 流程控制:顺序结构
Java中定义成员变量时采用合法的前向引用。如:
整个Java全栈系列都是笔者自己敲的笔记。写作不易,如果可以,点个赞呗!✌
关注公众号 ,更多笔记,等你来拿,谢谢
全栈小刘
认证博客专家
开源框架设计原理
JVM底层原理
JUC
我从不给自己退路