一、基本的算术运算符、+符号做连接符
1. 基本的算术运算符
- 为了掌握基本的算术运算符的使用,我们在IDEA里新建一个package(it.com.operator),然后在这下面新建一个Java class(OperatorDemo1):
package it.com.operator;
public class OperatorDemo1 {
public static void main(String[] args) {
// 目标:掌握基本的算术运算符的使用。
int a = 10;
int b = 2;
System.out.println(a + b); // 加
System.out.println(a - b); // 减
System.out.println(a * b); // 乘
System.out.println(a / b); // 除
System.out.println(a % b); // 取余
System.out.println(5 / 2); // 2,在Java中两个整数相除的结果还是整数
System.out.println(5.0 / 2); // 2.5
int i = 5;
int j = 2;
System.out.println(1.0 * i / j); // 2.5
}
}
- 代码运行结果为:
2. “+”符号可以做连接符的
- “+”符号与字符串运算的时候是用作连接符的,其结果依然是一个字符串。
- 为了掌握使用+符号做连接符的情况,我们继续在OperatorDemo1里写代码:
package it.com.operator;
public class OperatorDemo1 {
public static void main(String[] args) {
// 目标2:掌握使用+符号做连接符的情况
int a2 = 5;
System.out.println("abc" + a2); // "abc5"
System.out.println(a2 + 5); // 10
System.out.println("itcom" + a2 + "a"); // "itcom5a"
System.out.println(a2 + 'a' + "itcom"); // 5 + 97 = 102 ==>102itcom
}
}
- 代码运行结果为:
二、自增自减运算符
1. 自增自减运算符
- 为了掌握自增自减运算符的使用,我们在IDEA里新建一个package(it.com.operator),然后在这下面新建一个Java class(OperatorDemo2):
package it.com.operator;
public class OperatorDemo2 {
public static void main(String[] args) {
// 目标:掌握自增自减运算符的使用。
int a = 10;
a++; // a = a + 1
// ++a; // 11
System.out.println(a); // 11
a--; // a = a - 1
// --a; // 10
System.out.println(a); // 10
// 2++; // 自增自减只能操作变量不能操作字面量的,会报错!
}
}
- 代码运行结果为:
2. 自增自减的使用注意事项
- ++、--如果不是单独使用(如在表达式中、或者同时有其它操作),放在变量前后会存在明显区别
package it.com.operator;
public class OperatorDemo2 {
public static void main(String[] args) {
int i = 10;
int rs = ++i; // 先加后用
System.out.println(rs); // 11
System.out.println(i); // 11
int j = 10;
int rs2 = j++; // 先用后加
System.out.println(rs2); // 10
System.out.println(j); // 11
}
}
- 代码运行结果为:
3. 自增、自减拓展案例
package it.com.operator;
public class OperatorDemo2 {
public static void main(String[] args) {
int m = 5;
int n = 3;
int result = ++m - --m + m-- - ++n + n-- + 3;
// 6 - 5 + 5 - 4 + 4 + 3 = 9
System.out.println(result); // 9
int c = 10;
int d = 5;
int rs3 = c++ + ++c - --d - ++d + 1 + c--;
// 10 + 12 - 4 - 5 + 1 + 12 = 26
System.out.println(rs3); // 26
System.out.println(c); // 11
System.out.println(d); // 5
}
}
三、赋值运算符
1. 基本赋值运算符
- 就是“=”,从右边往左看。
2. 扩展赋值运算符
- 为了掌握扩展赋值运算符的使用,我们在IDEA里新建一个package(it.com.operator),然后在这下面新建一个Java class(OperatorDemo3):
package it.com.operator;
public class OperatorDemo3 {
public static void main(String[] args) {
// 目标:掌握扩展赋值运算符的使用。
// += 累加
// 需求:收红包
double a = 9.5;
double b = 520;
a += b; // a = (a的类型:double)(a + b)
System.out.println(a);
// -= 累减
// 需求:发红包
double i = 600;
double j = 520;
i -= j; // i = (i的类型:double)(i - j)
System.out.println(i);
// *=
int m = 10;
int n = 5;
m *= n; // m = (m的类型:int)(m * n)
System.out.println(m);
// /=
int c = 30;
int d = 3;
c /= d; // c = (c的类型:int)(c * d)
System.out.println(c);
// %=
int e = 7;
int f = 5;
e %= f; // e = (e的类型:int)(e * f)
System.out.println(e);
System.out.println("---------------------------------------");
byte x = 10;
byte y = 30;
// x = x + y; // 编译报错
x += y; //x = (byte)(x+y);
System.out.println(x);
}
}
- 代码运行结果为:
四、关系运算符
1. 关系运算符
- 判断数据是否满足条件,最终会返回一个判断的结果,这个结果是布尔类型的值:true或者false。
注意:在Java中判断是否相等一定是“==”,千万不要把“==”误写成“=”。
- 为了掌握关系运算符的基本使用,我们在IDEA里新建一个package(it.com.operator),然后在这下面新建一个Java class(OperatorDemo4):
package it.com.operator;
public class OperatorDemo4 {
public static void main(String[] args) {
// 目标:掌握关系运算符的基本使用。
int a = 10;
int b = 5;
boolean rs = a > b;
System.out.println(rs); // true
System.out.println(a >= b); // 要么a大于b,或者a等于b
System.out.println(2 >= 2); // true
System.out.println(a < b); // false
System.out.println(a <= b); // false
System.out.println(2 <= 2); // true
System.out.println(a == b); // false
System.out.println(5 == 5); // true
System.out.println(a != b); // true
System.out.println(10 != 10); //false
}
}
五、逻辑运算符
1. 逻辑运算符
- 把多个条件放在一起运算,最终返回布尔类型的值:true、false。
- 为了掌握逻辑运算符的使用,我们在IDEA里新建一个package(it.com.operator),然后在这下面新建一个Java class(OperatorDemo5):
package it.com.operator;
public class OperatorDemo5 {
public static void main(String[] args) {
// 目标:掌握逻辑运算符的使用。
// 需求:要求手机必须满足尺寸大于等于6.95,且内存必须大于等于8.
// 1. & (与运算)前后的条件的结果必须都是true,结果才是true。
double size = 9.8;
int storage = 16;
boolean rs = size >= 6.95 & storage >= 8;
System.out.println(rs); // true
double size1 = 6.8;
int storage1 = 16;
boolean rs1 = size1 >= 6.95 & storage1 >= 8;
System.out.println(rs1); // false
// 需求:要求手机要么满足尺寸大于等于6.95,要么内存必须大于等于8.
// 2. | (或运算)只要多个条件中有一个是true,结果就是true。
boolean rs2 = size1 >= 6.95 | storage1 >= 8;
System.out.println(rs2); // true
// 3. ! (非运算)取反的意思
System.out.println(!true); // false
System.out.println(!false); // true
System.out.println(!(2 > 1)); // false
// 4. ^ (异或运算)前后条件的结果相同时返回false,不同时返回true。
System.out.println(true ^ true); // false
System.out.println(false ^ false); // false
System.out.println(true ^ false); // true
System.out.println(false ^ true); // true
// 5. && (短路与)左边为false,右边不执行。
int i = 10;
int j = 20;
System.out.println(i > 100 && ++j > 99); // false
System.out.println(j); // 20
System.out.println(i > 100 & ++j > 99); // false
System.out.println(j); // 21
// 6. || (短路或)左边是true,右边不执行。
int m = 10;
int n = 30;
System.out.println(m > 3 || ++n > 40); // true
System.out.println(n); // 30
System.out.println(m > 3 | ++n > 40); //true
System.out.println(n); // 31
}
}
六、三元运算符、运算符的优先级
1. 三元运算符介绍
- 格式:
- 执行流程:首先计算关系表达式的值,如果值为true,返回值1,如果为false,返回值2。
- 为了掌握三元运算符的基本使用,我们在IDEA里新建一个package(it.com.operator),然后在这下面新建一个Java class(OperatorDemo6):
package it.com.operator;
public class OperatorDemo6 {
public static void main(String[] args) {
// 目标:掌握三元运算符的基本使用。
double score = 98.5;
String rs = score >= 60 ? "成绩及格" : "成绩不及格";
System.out.println(rs);
// 需求:找出2个整数中的较大值,并输出。
int a = 99;
int b = 167;
System.out.println(a > b ? a : b);
// 需求:找出3个整数中的较大值,并输出。
int i = 10;
int j = 45;
int k = 34;
int temp = i > j ? i : j;
System.out.println(temp > k ? temp : k);
}
}
- 代码运行结果为:
2. 运算符优先级
- 在表达式中,哪个运算符先执行后执行是要看优先级的,例如“*、/”的优先级高于“+、-”。
标签:语法,Java,int,System,运算符,println,true,out From: https://www.cnblogs.com/soulmate-clx/p/17726432.html