Java安全-代码审计

前言

java学完忘完了,从头捡起来…顺便记录一下

仅记录自己学习中觉得值得记录的地方,并不完全,也不适合所有人

环境

java:jdk17lts

编辑器:idea

视频教程:https://www.bilibili.com/video/BV1Cv411372m/?p=19&spm_id_from=pageDriver&vd_source=6bf1c94d1bbfd3bb26bf7332b2f748c5

基础语法

变量

image-20230903172231110

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.chenci.hello;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("中国");
System.out.println("中国"+"hello world"); //字符串
int a = 123;
System.out.println(a);
double b = 123.123;
System.out.println(b);
double c = a+b;
System.out.println(c); //变量赋值,相加
}
}

image-20230903174056917

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
int a = 1;
System.out.println(a); //变量需先声明再使用


{
int b = 1;
System.out.println(b); //变量的有效范围是当前{}
}


int c;
System.out.println(c); //变量使用需要有值

}
}

关键字

image-20230903174655609

标识符

image-20230903174635294

八/十六进制

image-20230903182021886

基本数据类型

image-20230903182555257

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
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
byte a = 127;
//byte a = 128; // 越界

short b = 32712;
//short b = 32799; //越界

int c = 1; //默认整数

long lg = 11111L; //整数默认int类型,需要长整型需要加L或者l

float f = 2222F; //整数默认int类型,需要长整型需要加F或者f

double d = 1.2;

char ch = 'a'; //只能有一个字符

// 引用数据类型,字符串类型
String ch2 = "aaa"; //多个字符


}
}

自动类型转换

image-20230903191121994

1
2
3
4
5
6
7
8
9
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
byte a = 10;
int b = a;
System.out.println(b);
}
}

表达式自动类型转换

image-20230904160017501

1
2
3
4
5
6
7
8
9
10
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
byte a = 10;
int b = 20;
long c = 30;
long rs = a+b+c; //表达式最终类型由最高类型决定
}
}

强制类型转换

image-20230904160738532

1
2
3
4
5
6
7
8
9
10
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
int a = 20;
byte b = (byte) a; //快捷键 alt+回车
System.out.println(a);
System.out.println(b); //20
}
}

image-20230904162955230

算术运算符

image-20230904182911448

1
2
3
4
5
6
7
8
9
10
11
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
int a = 20;
int b = 30;
System.out.println(b/a); //1,整数相除取整
System.out.println(1.0*b/a); //1.5,取最高类型
System.out.println("中"+"国"); //中国,字符拼接
}
}

自增自减运算符

image-20230904185352706

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
int a = 20;
int res1 = a++; //先复制再加
System.out.println(res1);

int c = 20;
int res2 = ++c; //先加在赋值
System.out.println(res2);
}
}

赋值运算符

image-20230904191332796

1
2
3
4
5
6
7
8
9
10
11
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
int a = 20;
double b = 1.1;
a+=b;
System.out.println(a); //21
}

}
1
2
3
4
5
6
7
8
9
10
11
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
double a = 20;
double b = 1.1;
a = a+b;
System.out.println(a); //21.1
}

}

关系运算符

image-20230912143816865

逻辑运算符

1
2
3
4
5
6
7
8
9
10
11
package com.chenci.variable;

public class Demo2 {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println(a>100 && ++b>99);//左边为false右边不执行
System.out.println(b); //2
}

}

image-20230912150728044

三元运算符

1
2
3
4
5
public static void main(String[] args) {
double score = 99.5;
String res = score >= 60 ?"及格":"不及格";
System.out.println(res); //及格
}

运算符优先级

image-20230912162356326

键盘输入

1
2
3
4
5
6
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个整数:");
int age = sc.nextInt();
System.out.println(age);
}

分支结构

if结构

1
2
3
4
5
6
7
8
9
10
11
12
public class IfDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入体温:");
int t = sc.nextInt();
if (t > 37.8){
System.out.println("体温异常:");
}else{
System.out.println("正常");
}
}
}

else if结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class IfDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入分数:");
int score = sc.nextInt();
if (score >= 0 && score <= 60) {
System.out.println("D");
} else if (score > 60 && score <= 80) {
System.out.println("C");
} else if (score > 80 && score <= 90) {
System.out.println("B");
} else if (score > 90 && score <= 100) {
System.out.println("A");
} else {
System.out.println("输入有误");
}
}
}

switch

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
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入日期:");
String week = sc.next();

switch (week){
case "周一":
System.out.println("今天周一");
break;
case "周二":
System.out.println("今天周二");
break;
case "周三":
System.out.println("今天周三");
break;
case "周四":
System.out.println("今天周四");
break;
case "周五":
System.out.println("今天周五");
break;
default:
System.out.println("输入错误");
}
}

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入日期:");
String week = sc.next();
switch (week) {
case "周一" -> System.out.println("今天周一");
case "周二" -> System.out.println("今天周二");
case "周三" -> System.out.println("今天周三");
case "周四" -> System.out.println("今天周四");
case "周五" -> System.out.println("今天周五");
default -> System.out.println("输入错误");
}
}

image-20230912195005416

for循环

1
2
3
4
5
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println("hello world");
}
}

while循环

1
2
3
4
5
6
7
public static void main(String[] args) {
int i = 1;
while (i < 10) {
i+=1;
System.out.println("hello world");
}
}

do while循环

1
2
3
4
5
6
7
public static void main(String[] args) {
int i = 0;
do {
System.out.println("hello world");
i++;
}while (i<3); //先执行,后判断
}

image-20230912201218226

image-20230912201740892

跳转关键字

image-20230912202433204

随机数

1
2
3
4
5
6
7
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 10; i++) {
int date = r.nextInt(10);
System.out.println(date);
}
}

猜数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
while (true) {
int date = r.nextInt(1,11);
System.out.println("猜一个数字0-10:");
int input = sc.nextInt();
if (input == date) {
System.out.println("猜对了");
break;
}else {
System.out.println("猜错了,数字是:"+date);
}
}
}

数组

静态数组定义和访问

1
2
3
4
5
6
7
8
public static void main(String[] args) {
int[] age = {1, 2, 2, 4, 5, 6};

for (int i = 0; i < age.length; i++) {
System.out.println(age[i]);
}

}

简写

1
2
3
4
5
6
public static void main(String[] args) {
int[] age = {1, 2, 2, 4, 5, 6};
for (int j : age) {
System.out.println(j);
}
}

image-20230913175646143

动态数组

1
2
3
4
5
6
public static void main(String[] args) {
int[] age =new int [3];
System.out.println(age[1]); //0
age[0] = 2;
System.out.println(age[0]); //2
}

image-20230913180826506

数组最大值

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) {
//定义数组
int[] faceScores = {15,2000,10000,20000,9500,-5};
//定义一个变量用于记录最终最大值
int max = faceScores[0];
//从数组第二个开始遍历
for (int i = 1; i < faceScores.length; i++) {
if (faceScores[i]>max){
max = faceScores[i];
}
}
System.out.println(max);
}

数组反转

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) {
//1. 定义一个数组
int[] arr = {1, 2, 3, 4, 5};
//2. 定义个循环,设计两个变量,一个在前一个在后
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] =temp;
}
for (int a = 0; a < arr.length; a++) {
System.out.print(arr[a]);
}
}

数组随机

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
public class ArrayRand {
public static void main(String[] args) {
// 定义动态数组
int[] codes = new int[5];
// 输入工号
Scanner sc = new Scanner(System.in);
// 将输入的工号加入数组
for (int i = 0; i < codes.length ; i++) {
System.out.println("输入工号:");
codes[i] = sc.nextInt();
}
// 生成随机数
Random ran = new Random();
for (int j = 0; j < codes.length; j++) {
int index = ran.nextInt(codes.length);
int temp = codes[index];
codes[index] = codes[j];
codes[j] = temp;
}

for (int i = 0; i < codes.length; i++) {
System.out.println("随机排序:" + codes[i]);
}
}
}

方法

自定义方法

image-20230918165038495

1
2
3
4
5
6
7
8
9
10
11
public class Demo1 {
public static void main(String[] args) {
int res = sum(10,20);
System.out.println(res);
}

// 自定义方法
public static int sum(int a, int b) {
return a + b;
}
}

image-20230919150151252

求和自定义方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Demo2 {
public static void main(String[] args) {
int res = sum(5);
System.out.println(res);
}

public static int sum(int n ){
int sum = 0;
for (int i = 0; i <= n; i++) {
sum+=i;
}
return sum;
}
}

参数传递机制

基本类型参数传递

1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) {
int a = 10;
change(a);
System.out.println(a); //10
}
public static void change(int a){
System.out.println(a); //10
a = 20;
System.out.println(a); //20
}

image-20230919152529940

引用类型参数传递

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Demo3 {
public static void main(String[] args) {
int[] arrs = {10,20,30};
change(arrs);
System.out.println("main:" + arrs[1]); //222
}

public static void change(int[] arrs){
System.out.println("方法内1:"+arrs[1]); //20
arrs[1] = 222;
System.out.println("方法内2:"+arrs[1]); //222
}
}

判断两个数组是否相等

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
public class Demo4 {
public static void main(String[] args) {
int[] arr1 = {};
int[] arr2 = {};
boolean res = equals(arr1,arr2);
System.out.println(res);
}

public static boolean equals(int[] arr1 ,int[] arr2){
//1.判断两个数组是否为空
if (arr1 == null && arr2==null){
return true;
}
//2.其中一个是null
if (arr1 == null || arr2==null){
return false;
}
//3.判断长度是否一样
if (arr1.length != arr2.length){
return false;
}
//4.判断每个元素是否相等
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]){
return false;
}
}
return true; //相等
}
}

方法重载

image-20230919164015800

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Demo5 {
public static void main(String[] args) {
int a = 10;
test1(a); //10
}
public static void test1(int a){
System.out.println(a);
}

public static void test1(int a,int b ){
System.out.println(a);
System.out.println(20);
}
}

案列

买飞机票

image-20230925161026312

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
public class fly {
public static void main(String[] args) {
double res = calculate(1000,8,"经济舱");
System.out.println(res);
}

public static double calculate(double price, int month, String type) {
// 判断当前月份是淡季还是旺季
if (month <= 10 && month >= 5) {
// 旺季
// 判断仓位类型
switch (type) {
case "头等舱":
price *= 0.9;
break;
case "经济舱":
price *= 0.85;
break;

}
}
else {
switch (type) {
case "头等舱":
price *= 0.7;
break;
case "经济舱":
price *= 0.65;
break;
}
}
return price;
}
}

验证码

image-20230926194609057

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
public class code {
public static void main(String[] args) {
System.out.println(createCode(4));
}

public static String createCode(int n) {
Random r = new Random();
String code = "";
// 循环决定验证码个数
for (int i = 1; i <= n; i++) {
int type = r.nextInt(3);
// 匹配验证码类型,0:数字,1:小写字母,2:大写字母
switch (type) {
case 0:
code += r.nextInt(10);
break;
case 1:
code += (char) r.nextInt(97, 123);
break;
case 2:
code += (char) r.nextInt(65, 91);
}

}
return code;
}
}

打分

image-20230926194756546

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
public class dafen {
public static void main(String[] args) {
System.out.println(getAverageScore(5));
}

public static double getAverageScore(int n) {
// 定义一个数组用来接受分数,长度为n
int[] scores = new int[n];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; i++) {
System.out.println("输入第" + (i + 1) + "个评委的打分:");
int score = sc.nextInt();
scores[i] = score;
}

int sum = 0;//总分
int max = scores[0];//最大
int min = scores[0];//最小

// 循环判断
for (int i = 0; i < scores.length; i++) {
int score = scores[i];
sum += score;

if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
}

return 1.0 * (sum - min - max) / (n - 2);
}
}

copy数组

image-20230926214113251

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class copyArray {
public static void main(String[] args) {
int[] arr = {1,2,3};
int[] res = copyArray(arr);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}

public static int[] copyArray(int[] arr){
//1.创建一个长度一样的数据
int[] arr2 = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
}
}

抢红包

image-20230926214257331

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
public class redbao {
public static void main(String[] args) {
int[] a = {1,2,3,4,5};
start(a);
}
public static void start(int[] moneys){
// 定义输入和随机变量
Scanner sc = new Scanner(System.in);
Random r = new Random();

for (int i = 0; i < 5; i++) {
System.out.println("请您输入任意内容进行抽奖");
sc.next();//输入

//随机抽取一个
while (true){
int index =r.nextInt(moneys.length);
int money = moneys[index];
//判断是否为0
if (money!=0){
System.out.println("恭喜您抽中红包:"+money);
moneys[index] = 0;
break;
}
}
}
System.out.println("活动结束");

}
}

素数

image-20231009204700428

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class sushu {
public static void main(String[] args) {
int res = getShu();
System.out.println(res);
}

public static int getShu() {
int sum = 0;
for (int i = 101; i < 200; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
sum +=1;
System.out.println(i);
}
}
return sum;
}
}

99乘法表

1
2
3
4
5
6
7
8
public static void main(String[] args) {
for (int i = 1; i < 9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j + "x" + i + "=" + (j*i )+ "\t");
}
System.out.println();
}
}

面向对象

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Student {
String name;
double chinese;
double math;

public void printTotalScore() {
System.out.println(name + "总成绩是" + (chinese + math));
}

public void printAverageScore(){
System.out.println(name + "平均成绩是" + (chinese + math)/2 );
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class test1 {
public static void main(String[] args) {
// 1.创建对象
Student s1 = new Student();
s1.name = "小明";
s1.math = 100;
s1.chinese = 80;
s1.printAverageScore();
s1.printTotalScore();

}


}

image-20231011112926803

this关键字

image-20231011140730262

1
2
3
4
5
public class Student {
public void printThis(){
System.out.println(this);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1);
s1.printThis();

System.out.println("-------------------");


Student s2 = new Student();
System.out.println(s2);
s2.printThis();
}

}

image-20231011141056688

this访问当前对象的成员变量

1
2
3
Student s3 = new Student();
s3.score = 300;
s3.printPass(200);
1
2
3
4
5
6
7
public void printPass(double score){
if (this.score > score){
System.out.println("恭喜考入哈佛");
}else{
System.out.println("没有考入");
}
}

构造器

image-20231011154336504

1.构造器要和类名相同

2.构造器无返回值

1
2
3
4
5
6
7
8
9
10
11
12
public class test1 {
public static void main(String[] args) {
Student s1 = new Student();

System.out.println("---------------");

Student s2 = new Student("xx");
}



}
1
2
3
4
5
6
7
8
9
public class Student {
public Student(){
System.out.println("无参数构造器被触发");
}

public Student(String name){
System.out.println("有参数构造器被触发");
}
}

image-20231011152336571

使用实例

image-20231011153924489

封装

私有成员变量

image-20231011161223899

image-20231011161426038

实体类

image-20231011163523258

image-20231011164959308

案例

image-20231015160607614

image-20231015165051239

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
package com.chenci.demo;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
//1.设计一个电影类
//2.设计一个电影操作类
//3.准备电影数据
Movie[] movies = new Movie[4];
movies[0] = new Movie(1,"水门桥",38.9,9.1,"徐克","吴京","12万人想看");
movies[1] = new Movie(2,"月球陨落",37.2,9.8,"汤香兰","李白","11.2万人想看");
movies[2] = new Movie(3,"出拳吧",38.5,9.4,"罗兰","小黑","15.4万人想看");
movies[3] = new Movie(4,"aaa",38.5,9.6,"bb","小白","16.4万人想看");
//4.创建电影操作类的对象,接受电影数据,带入业务处理
MovieOperator operator = new MovieOperator(movies);
// operator.printAllMovie();
// operator.searchMovieById(2);

//5.菜单选项
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("===电影信息系统===");
System.out.println("1.查询全部电影信息");
System.out.println("2.根据id查询电影全部信息");
System.out.println("输入操作编号:");
int command = sc.nextInt();
switch (command) {
case 1 -> operator.printAllMovie();
case 2 -> {
System.out.println("输入id:");
int id = sc.nextInt();
operator.searchMovieById(id);
}
default -> System.out.println("输入操作编号有误,请重新输入");
}
}

}
}
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
package com.chenci.demo;

public class MovieOperator {
private Movie[] movies;

public MovieOperator(Movie[] movies) {
this.movies = movies;
}

//1.展示全部电影信息
public void printAllMovie() {
System.out.println("---------系统全部电影信息如下---------");
for (int i = 0; i < movies.length; i++) {
Movie m = movies[i];
System.out.println("编号:" + m.getId());
System.out.println("名称:" + m.getName());
System.out.println("价格:" + m.getPrice());
System.out.println("--------------------------------");
}
}


//2.通过编号查询详细信息
public void searchMovieById(int id) {
for (int i = 0; i < movies.length; i++) {
Movie m = movies[i];
if (m.getId() == id) {
System.out.println("该电影详情如下");
System.out.println("编号:" + m.getId());
System.out.println("名称:" + m.getName());
System.out.println("价格:" + m.getPrice());
System.out.println("得分:" + m.getScore());
System.out.println("导演:" + m.getDirector());
System.out.println("演员:" + m.getActor());
System.out.println("其他信息:" + m.getInfo());
return;// 结束
}
}
System.out.println("没有该电影信息");
}
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.chenci.demo;

public class Movie {
private int id;
private String name;
private double Price;
private double score;
private String director;
private String actor;
private String info;

public Movie() {
}

public void setPrice(double price) {
Price = price;
}

public Movie(int id, String name,double price, double score, String director, String actor, String info) {
this.id = id;
this.name = name;
this.score = score;
this.director = director;
this.actor = actor;
this.info = info;
Price = price;
}
public double getPrice() {
return Price;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getScore() {
return score;
}

public void setScore(double score) {
this.score = score;
}

public String getDirector() {
return director;
}

public void setDirector(String director) {
this.director = director;
}

public String getActor() {
return actor;
}

public void setActor(String actor) {
this.actor = actor;
}

public String getInfo() {
return info;
}

public void setInfo(String info) {
this.info = info;
}
}

成员变量和局部变量的区别

image-20231015172411400

常用api

image-20231017143130565