1. Java运算符
1.1 基础数学运算符
数学是整个程序的基础,所以各种常用的计算符号,Java基本都会提供。但是程序可以提供的只是最为基础的数学运算支持,如果需要完成更加复杂的计算,需要自己进行二进制的处理。
在以后写的代码中,别写出以下的代码片段:
代码
public class Test{
public static void main(String args[]){
int x = 10;
int y = 20;
int reasult = x - y + ++x * y-- - --x * y / y * --y ;
}
}
对于程序的计算而言,千万别写的谁也不知道。以后写的代码都写简单代码。所有运算符都有其优先级,没背过,使用括号。
范例:简单的四则运算
代码
public class Test{
public static void main(String args[]){
int x = 10;
int y = 20;
System.out.println(x+y);// 30
System.out.println(x-y);// -10
System.out.println(x*y);// 200
System.out.println(x/y); // 0
}
}
在数学计算里有自增和自减操作:(实在记不住就分开写)
+ 自增云算:
- ++ 变量:先进行自增,后进行运算
- 变量 ++:先进行运算,后进行自增
代码
public class Test{
public static void main(String args[]){
int x = 10;
int y = 20;
int result = ++x *y;
System.out.println("result = " + result);// ++x先执行,值为11,结果为220
System.out.println("x = " + x);// 11
}
}
代码
public class Test{
public static void main(String args[]){
int x = 10;
int y = 20;
int result = x++ *y;
System.out.println("result = " + result);// x++值为10,结果为200
System.out.println("x = " + x);// 11
}
}
- 自减运算:
- – 变量:先进行自减,后进行运算
- 变量 –:先进行运算,后进行自减
代码
public class Test{
public static void main(String args[]){
int x = 10;
int y = 20;
int result = --x *y;
System.out.println("result = " + result);// --x值为9,结果为180
System.out.println("x = " + x);// 9
}
}
代码
public class Test{
public static void main(String args[]){
int x = 10;
int y = 20;
int result = x-- *y;
System.out.println("result = " + result);// x--值为10,结果为200
System.out.println("x = " + x);// 9
}
}
1.2 三目运算符(重点
)
三目是一种赋值运算,他指的是根据条件来进行判断。三目运算的基本使用语法如下:
数据类型 变量 = 布尔表达式 ? 条件满足时复制内容 : 条件不满足时复制内容;
范例:将两个变量中大的变量赋值给新变量
代码
public class Test{
public static void main(String args[]){
int a = 10;
int b = 20;
int max = a > b ? a : b;
System.out.println(max);// 20
}
}
很多时候实在不清楚三目运算,也可以写成 if 判断。
代码
public class Test{
public static void main(String args[]){
int a = 10;
int b = 20;
int result = 0;
if (a > b) {
result = a;
}else {
result = b;
}
System.out.println(result);// 20
}
}
如果不使用三目,整个代码跨越的行数太多。
1.3 关系运算符
若要进行关系运算的操作可以使用:>,< ,>=,<=,!=,==。所有的关系运算符判断完成的数据返回结果都是布尔(boolean)。
范例:
代码
public class Test{
public static void main(String args[]){
int x = 10;
int y = 20;
System.out.println(x > y);// false
System.out.println(x < y);// true
System.out.println(x == y);// false
System.out.println(x == y ? x * 2 : y - 3);// 17
}
}
可以直接将字符与数字进行判断。
代码
public class Test{
public static void main(String args[]){
System.out.println('a' == 97);// true
System.out.println('0' == 0);// fasle
}
}
1.4 逻辑运算符
逻辑运算主要是三类:与、或、非(!)。
范例:观察非操作
代码
public class Test{
public static void main(String args[]){
System.out.println( ! (10 > 20));// true
}
}
在逻辑运算中,最麻烦的就是与和或操作,因为其各有两种写法。
+ 与操作: &、&&
+ 或操作:|、||
1.与操作:在进行多个条件判断的时候,只有所有的条件都满足了才可以进入。
范例:&
代码
public class Test{
public static void main(String args[]){
// 两个条件全部都判断了
if ( 1 > 2 & 10 / 0 == 0) {
System.out.println("条件满足");// 程序出错: ArithmeticException
}
}
}
当使用“&”的时候明确的表示多个条件都需要判断,可是如果前面已经有条件返回了false,即便后面有几十万个true,最终结果也是false,所以理论上后面的判断不应该再执行,此时使用“&&”(短路与)。
范例:&&
代码
public class Test{
public static void main(String args[]){
// 1 > 2 fasle,后面条件不判断,程序未出错。
if ( 1 > 2 && 10 / 0 == 0) {
System.out.println("条件满足");
}
}
}
写代码使使用“&&”。
2.或操作:特点是若干个判断条件只要有一个是true,那么最终结果就是true,无论后面有多少个fasle。
范例:|
代码
public class Test{
public static void main(String args[]){
// 两个条件全部都判断了
if ( 1 < 2 | 10 / 0 == 0) {
System.out.println("条件满足");// 程序出错
}
}
}
范例:||
代码
public class Test{
public static void main(String args[]){
// 1 < 2 true,后面条件不判断,程序未出错。
if ( 1 < 2 || 10 / 0 == 0) {
System.out.println("条件满足!"); // 条件满足!
}
}
}
从此以后所编写的逻辑运算中,都是用&&和||。
1.5 位运算符(了解)
位运算指的是可以按照二进制、八进制、十六进制进行数据处理,基本上考虑最多的还是二进制,数据以为的处理方式实现一些数据的内容变化。
位运算实现的关键在于要进行十进制与二进制的转换,这种转换原则是数据除2取余,直到除到0为止,所有的余数按照倒序排列。
知道了二进制的数据变化之后,就可以利用&(有一个0与的结果就是0)、|(有一个1或的结果就是1)进行位的计算。
范例:位&
代码
public class Test{
public static void main(String args[]){
int numA = 19;// 00000000 00000000 00000000 00010011
int numB = 5; // 00000000 00000000 00000000 00000101
System.out.println(numA & numB); // 1:00000000 00000000 00000000 00000001
}
}
范例:位 |
代码
public class Test{
public static void main(String args[]){
int numA = 19; // 00000000 00000000 00000000 00010011
int numB = 5; // 00000000 00000000 00000000 00000101
System.out.println(numA | numB); // 23:00000000 00000000 00000000 00010111
}
}
位运算最大的有特点是可以进行移位的处理:>>、<<.
范例:>>
代码
public class Test{
public static void main(String args[]){
int numA = 2; // 00000000 00000000 00000000 00000010
System.out.println(numA >> 2); // 0
}
}
范例:<<
代码
public class Test{
public static void main(String args[]){
int numA = 2; // 00000000 00000000 00000000 00000010
System.out.println(numA << 2); // 8:00000000 00000000 00000000 00001000
}
}
面试题:请解释&&、&、|和||的区别?
+ 逻辑运算中:
- &&:表示逻辑与运算,若干个判断条件在前面有条件返回false,那么后面的条件将不再进行判断。
- &:如果用&,则判断全部条件。
- ||:表示逻辑或运算,若干个判断条件在前面有条件返回true,那么后面的条件将不再进行判断。
- |:如果用|,则判断全部条件。
- 位运算中:
- & :位与运算,两个数据位有一个0与的结果就是0,全为1时结果才是1。
- |:位或运算,两个数据位有一个1与的结果就是1,全为0时结果才是0。