运算符知识点和面试题总结
运算符
- Java语言支持如下运算符
- 算术运算符:+,-,*,/,%(取余运算叫模),++,–
- 赋值运算符:=
- 关系运算符:>, <, >=, <=, ==(等于), !=(不等于),instanceof
- 逻辑运算符:&&,||,!(与 或 非)
- 位运算符:&,|,^,>>,<<,>>>(了解)
- 条件运算符?:
- 扩展值运算符:+=,-=,*=,/=
初识二元运算符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package operator;
public class Demo1 {
public static void main(String[] args) {
//二元运算符
//ctrl+D:复制当前行到下一行
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(float)b);
//这里注意需要强转一下。
}
}强转知识的扩展(⭐⭐⭐⭐)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22package operator;
public class Demo2 {
public static void main(String[] args) {
long a = 123123123123123L;
int b= 123;
short c = 10;
byte d = 8;
float e = 4.14f;
System.out.println(a+b+c);//long
System.out.println(b+c+d);//int
System.out.println((c+d));//int
System.out.println((d+e));//float
//总结:
/*
byte、short和char类型在算术运算中会被提升为 int,结果也是 int 类型。
对于其他数据类型,运算结果的类型由操作数中精度最高的类型决定。 //很重要!//
*/
}
}总结:
- byte、short、char和int类型在算术运算中会被提升为 int,结果也是 int 类型。
- 对于其他数据类型,运算结果的类型由操作数中精度最高的类型决定。
重点记忆:
- byte、short和char类型在算术运算中会被提升为 int,结果也是 int 类型。
- 对于其他数据类型,运算结果的类型由操作数中精度最高的类型决定。
这是自己总结的东西,要时常翻一翻。有自己的理解才是王道。
自增、自减(⭐⭐⭐⭐⭐)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package operator;
import sun.nio.cs.ext.MacThai;
public class Demo4 {
public static void main(String[] args) {
//++ -- 自增, 自减 ------一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增。
//a = a+1;
System.out.println(a);//------4
System.out.println(b);//------3
System.out.println("============================");
//a = a+1;
int c = ++a;//执行完这行代码前,先自增,再给b赋值。
System.out.println(a);//------5
System.out.println(c);//------5
//总结:a++就是先赋值后自增
// ++a就是先自增后赋值
System.out.println("========================");
int x = 4;
int y = x--;
System.out.println(x);//------3
System.out.println(y);//------4
System.out.println("========================");
int z = --x;
System.out.println(x);//------2
System.out.println(z);//------2
//总结:我发现如果是先自增/减,输出的两个结果是一样的。
//幂运算 2^3 2*2*2 = 8
很多运算,我们会使用一些工具类来操作。
double pow = Math.pow(3,2);
System.out.println(pow);
这里的题目,有个印象就可以。属于补充知识。
}
}总结:我发现如果是先自增/减,输出的两个结果是一样的。
逻辑运算符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package operator;
//逻辑运算符
public class Demo5 {
public static void main(String[] args) {
//&&与(and) ||或(or) !非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b: "+(a&&b));//逻辑与运算:都真才真。
System.out.println("a || b: "+(a||b));//逻辑或运算:一真为真。
System.out.println("! (a && b): "+!(a&&b));//取反:真假互换。
System.out.println(a&&b);
//这里加号的作用是:拼接作用。Demo7里有重点讲解。
System.out.println("========================================");
//短路计算=====
int c = 5;
boolean d = ((c<4)&&(c++<4));
System.out.println(d);
System.out.println(c);
//解析:因为与运算中都真才真,在这个式子中。(c<4)和(c++<4);
//按正常来说,输出结果应该是false和6。但是因为c<4已经是false。
//所以会出现短路。执行完c<4后,后面就不会执行了。所以结果是false和5。
//程序在基础阶段,没什么天赋不天赋。只有看你努力不努力。
}
}这里的短路计算需要特别注意一下!
位运算符(⭐)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34package operator;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
public class Demo6 {
public static void main(String[] args) {
//位运算符
/*
A = 0011 1100
B = 0000 1101
与A&B = 0000 1100 //都1才1,其余均为0.
或/B = 0011 1101 //有一个1就为1,其余为0.
抑或A^B = 0011 0001 //同为0,异为1.
取反~B = 1111 0010 //反过来。1-0 0-1.
一道面试题(最快计算)
2*8 = 16 2*2*2*2
<< *2
>> /2
原理:
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
}
}+=、-=和一道面试题(⭐⭐⭐)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34package operator;
public class Demo7 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a = a+b
System.out.println(a);
a-=b;//a = a-b
System.out.println(a);
System.out.println("===================");
//一道面试题:
//字符串连接符:+ ==String
//"+"运算符两侧,只要有一方出现了String类型,都会将其他操作数转换为String
//进行拼接(注意不是运算!例:(""+a+b) a+b不是30而是1020)
//如果字符串在前面,就会直接拼接。如果字符串在后边,前面依旧会运算,然后完成拼接。
System.out.println(""+a+b);
System.out.println(a+b+"");
System.out.println(a+b+"英雄联盟");
/*
输出结果为:
1020
30(其实这后边也有东西,因为我只写了"")
30英雄联盟
*/
}
}三元运算符(⭐⭐⭐)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26package operator;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
// x ?y :z
//如何x==true,则结果为y,否则结果为z
int score = 80;
String type = score <60 ?"不及格":"及格";//必须掌握!!
//if
System.out.println(type);
System.out.println("=====================================");
int num = 1;
String Typa = num< 5 ?"恭喜您!您中奖了!":"谢谢惠顾!";
System.out.println(Typa);
System.out.println("=====================================");
int num1 = 6;
String Typb = num1< 5 ?"恭喜您!您中奖了!":"谢谢惠顾!";
System.out.println(Typb);
System.out.println("=====================================");
int num2 = 60;
String typo = num2< 60 ?"不好意思,你挂科了!":"恭喜你,通过成功!";
System.out.println(typo);
}
}运算符的优先级:
运算符 描述 结合性 () 圆括号 从左到右 [] 方括号 从左到右 . 成员访问 从左到右 ++ – 前缀递增/递减 从右到左 ++ – 后缀递增/递减 从左到右 + - 一元正号/负号 从右到左 ! ~ 逻辑非/按位取反 从右到左 (类型) 强制类型转换 从右到左 * / % 乘法、除法、取余 从左到右 + - 加法、减法 从左到右 << >> >>> 左移、右移、无符号右移 从左到右 < <= > >= 关系运算符 从左到右 == != 相等性运算符 从左到右 & 按位与 从左到右 ^ 按位异或 从左到右 | 按位或 从左到右 && 逻辑与 从左到右 || 逻辑或 从左到右 ? : 条件运算符 从右到左 = += -= *= /= 赋值运算符 从右到左 &= ^= |= <<= >>= >>>= 按位与赋值、按位异或赋值、按位或赋值、左移赋值、右移赋值、无符号右移赋值 从右到左 多看几遍!到这里,运算符的知识点就结束了!
- Java语言支持如下运算符
包机制
JavaDoc
- javadoc命令是用来生成自己API文档的
- 参数信息
- @author
- @version
- @since指明需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- @throws 异常抛出情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package com.xiwen.base;
/**
* @author xiwen
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;//属性
/**
*
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{ //方法
return name;
}
//第一种:通过命令行的方法进行生成文档
//第二种:通过CMD命令:-encoding UTF-8 -charset UTF-8!
//基础部分的一切知识,后面几乎每天都会用!
}总结:
- 加在类上面就是类的注释。
- 加在方法上面就是方法的注释。
通过命令行的方法进行生成文档:
- 第一步,点击导航栏ToolS–>Generate JavaDoc.
- 第二步,Generate JavaDoc scope选项勾选File……
- 第三步,Output directory最好选择一个新建的文件夹。
- 第四步,locale:zh_CN Command line arguments:-encoding UTF-8 -charset UTF-8
- 第五步,最后点击ok即可,会自动跳转浏览器查看文档。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Xiwen!