前言
java学完忘完了,从头捡起来…顺便记录一下
仅记录自己学习中觉得值得记录的地方,并不完全,也不适合所有人
环境
java:jdk17lts
编辑器:idea
视频教程:https://www.bilibili.com/video/BV1Cv411372m/?p=19&spm_id_from=pageDriver&vd_source=6bf1c94d1bbfd3bb26bf7332b2f748c5
基础语法
变量

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); } }
|

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);
} }
|
关键字

标识符

八/十六进制

基本数据类型

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;
short b = 32712;
int c = 1;
long lg = 11111L;
float f = 2222F;
double d = 1.2;
char ch = 'a'; String ch2 = "aaa";
} }
|
自动类型转换

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); } }
|
表达式自动类型转换

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; } }
|
强制类型转换

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; System.out.println(a); System.out.println(b); } }
|

算术运算符

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); System.out.println(1.0*b/a); System.out.println("中"+"国"); } }
|
自增自减运算符

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); } }
|
赋值运算符

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); }
}
|
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); }
}
|
关系运算符

逻辑运算符

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); System.out.println(b); }
}
|

三元运算符
1 2 3 4 5
| public static void main(String[] args) { double score = 99.5; String res = score >= 60 ?"及格":"不及格"; System.out.println(res); }
|
运算符优先级

键盘输入
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("输入错误"); } }
|

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); }
|


跳转关键字

随机数
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); } }
|

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

数组最大值
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) { int[] arr = {1, 2, 3, 4, 5}; 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]); } } }
|
方法
自定义方法

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; } }
|

求和自定义方法
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); } public static void change(int a){ System.out.println(a); a = 20; System.out.println(a); }
|

引用类型参数传递
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]); }
public static void change(int[] arrs){ System.out.println("方法内1:"+arrs[1]); arrs[1] = 222; System.out.println("方法内2:"+arrs[1]); } }
|

判断两个数组是否相等
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){ if (arr1 == null && arr2==null){ return true; } if (arr1 == null || arr2==null){ return false; } if (arr1.length != arr2.length){ return false; } for (int i = 0; i < arr1.length; i++) { if (arr1[i] != arr2[i]){ return false; } } return true; } }
|
方法重载

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); } 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); } }
|