java基础

# Java学习笔记 # 1.从安装到卸载 ## 1.1 安装 1.1.1 jdk下载与安装 甲骨文官方网站:https://www.oracle.com/cn/java/ 清华大学镜像:https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/ 华为云镜像:https://repo.huaweicloud.com/java/jdk/ 1.1.2 环境变量配置 略 1.1.3 IDE的下载使用 免费 Eclipse: https://www.eclipse.org/ 收费 IDEA: https://www.jetbrains.com/idea/ ## 1.2 卸载 略 # 2.语法基础 ## 2.1 标识符 1. 包含:类名、方法名、变量名 2. 标识符命名规则: ```java /* Java标识符(类名、方法名、变量名)命名规则: 1. 所有的标识符都只能以字母(A-Z或a-z),美元符($),下划线(_)开始 A、a、$、_ 2. 首字母之后可以是字母(A-Z或a-z),美元符($),下划线(_),数字(0-9)的任何字符组合 Aa、a$、$1、_A 3. 不能使用关键字作为 关键字详见下图 4. 标识符大小写敏感 aa和Aa不相同 5. 可以使用中文命名但不推荐,也不推荐使用pinyin 你好、nihao */ ``` Java关键字 ![image-20210504231607254](https://cdn.jsdelivr.net/gh/lalkk/imgurl/images/2021image-20210504231607254.png) ## 2.2 数据类型 基本数据类型: ​ 整型类型 [ 字节型byte、短整形short、整形int、长整型long ] ​ 浮点型 [ 单精度float、双精度double ] ​ 字符型 [ char ] ​ 布尔类型 [ boolean ] (包括true和false两个值) ## 2.3 数据类型转换 自动转换:低容量 ——> 高容量,不会损失精度 ```java byte ——> short ——> int ——> long ——> float ——>double ``` 强制转换:低容量 <<< 高容量,会丢失数据精度 语法格式:在待转换的变量名前用括号注明要转换的目标类型 1.double型变量转换成int ```java double a = 16.6; int b = (int)a;// double类型容量大于int类型,需要强制转换 System.out.println(a);//a = 16.6 System.out.println(b);// b = 16 ``` 2.将 int 型变量转换为 short ```java /* 1. 不能对布尔值进行转换 2. 转换可能出现内存溢出、精度丢失等问题 */ int a = 10; short b = (short) a; // int类型容量大于short类型,需要强制转换 System.out.print(b); // b = 10 ``` 将 long 类型变量转换为 double 类型 ```java long a = 10; double b = a; // long类型容量小于double类型,自动转换 System.out.print(b); // b =10.0 ``` ## 2.4 变量 变量的命名规范:可以由字母、数字、下划线、美元符号组成,但不能以数字开头。没有长度限制,但区分大小写,最好简短清楚。一般建议使用驼峰命名法,即第一个单词首字母大写,其后单词首字母大写,如UserName。 语法: 数据类型 变量名 = 值; 注意:数据类型可以 8 中基本数据类型(整型、浮点型、字符型、布尔型)也可以是引用类型(数组、字符串等) ```java // 数据类型 变量名 = 值; //例如 int a = 6; double b = 3.14; ``` 变量分类:局部变量(方法外、类内);类变量(方法外、类内);实例变量(类内,方法外) ```java public class Test { // 类变量(类内,方法外),比实例对象多static,可直接引用 static double flag = 10; // 实例变量(类内,方法外),要new对象才能引用 String name = "Jerry"; public static void main(String[] args) { // 局部变量(方法内) int i = 10; System.out.println(i); // 10 } } ``` ## 2.5 常量 定义:始终不变的量,赋值后无法再更改 创建常量 PI,其值为 3.14 ```java public class Test { // 语法结构:final 数据类型 常量名 = 值 //常量命名规则:全大写字母,下划线(MAX_NUM) static final double PI = 3.14; // static、final都是修饰符无先后顺序 public static void main(String[] args) { System.out.println(PI); // 3.14 } } ``` ## 2.6 输入和输出 Scanner类: 输出:使用`System.out.println()`来向屏幕输出一些内容。 ```java System.out.println("Hello World") //Hello World System.out.println(3 + 2) // 5 ``` java占位符 | %d | 格式化输出整数 | | ---- | -------------------------------- | | %x | 格式化输出十六进制整数 | | %f | 格式化输出浮点数 | | %e | 格式化输出科学计数法表示的浮点数 | | %s | 格式化字符串 | 输入 ```java import java.util.Scanner;//类的包 public class Test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 创建Scanner对象 System.out.print("请输入姓名: "); // 提示 String name = scan.nextLine(); // 读取一行输入并获取字符串 System.out.print("请输入年龄: "); // 打印提示 int age = scan.nextInt(); // 读取一行输入并获取整数 System.out.printf("姓名:"+name+"年龄:"+age); // 格式化输出 } } ``` ## 2.7 运算符 定义:运算符是一种特殊符号,用以表示数据的运算、赋值和比较。 ### 分类: - 算术运算符 - 赋值运算符 - 比较运算符 - 逻辑运算符 - 位运算符(熟悉) - 三元运算符 #### 2.7.1 算术运算符 - +(正)、-(负)、 +、 -、*、/、%(取余)、++(前、后自增)、–(前、后自减)、+(字符串拼接) - 基本使用: ```java /* 除法运算 / */ int x = 16; int y = 5; int result_1 = x / y; System.out.println(result_1); // 3, 两int变量相除结果仍为int,结果只保留整数 double result_2 = x / y; // 3.0,int除int的double类型的结果是带小数点的整数,丢失数据精度 double result_5 = (x * 1.0) / y; //3.2,double除int,结果为double。不损失精度 double result_6 = ((double) x) / y; //3.2 强制转换后,结果为double。不损失精度 /* 取余(模)运算符号 % 运算结果的符号始终和被模数的符号一致。 */ 1. int x = 12; int y = 5; System.out.println(x % y); // 2 2. int x = -12; int y = -5; System.out.println(x % y); // -2 3. int x = -12; int y = 5; System.out.println(x % y); // -2 4. int x = 12; int y = -5; System.out.println(x % y); // 2 /* 自增、自减 ++i 先自增1,再运算 i++ 先运算,再自增1 自增不会改变变量数据类型 常量不能自加(1++) */ int a1 = 10; int b1 = ++a; System.out.println(a1 + " , " + b1); // 11 , 11 int a2 = 10; int b2 = a2++; System.out.println(a2 + " , " + b2); // 11 , 10 int a3 = 10; a3++; // ++a3 System.out.println(a3); // 11 // 注意: short s1 = 10; // s1=s1+1; //编译失败,s1=(short)(s1+1) //正确编译 s1++; // 自增1不会改变变量本身数据类型 // 例 int a4 = 10; int b4 = a4--; System.out.println(b4); // 10 /* 字符串拼接,要至少要保证一边为字符串类型 */ int n = 10; System.out.println("n: " + n); // n:10 ``` #### 2.7.2.赋值运算符 =、+=、-=、*=、/=、%= #### 2.7.3.比较运算符 ==、!=、<、>、<=、>= #### 2.7.4.逻辑运算符 &(与)、&&(短路与)、|(或)、||(短路或)、!(非)、^(异或) 注意:逻辑运算符操作的都是布尔类型的变量 ![image-20210504233141514](https://cdn.jsdelivr.net/gh/lalkk/imgurl/images/2021image-20210504233141514.png) - &、&&:两边均为 true,结果为 true; - |、||:任一边为 true,结果就为 true; - !:true 变 false;false 变 true; - ^:两边相异时结果为 true,反之为 false; ```java public class Test1 { public static void main(String[] args) { int a = 16; double b = 9.5; String str1 = "hello"; String str2 = "world"; System.out.println("a=b:"+(a==b)); System.out.println("a>b:"+(a> b)); System.out.println("a<=b"+(a<=b)); System.out.println("str1=str2:"+(str1==str2)); } } //运行结果 a=b:false a>b:true a<=bfalse str1+str2:false ``` #### 2.7.5.位运算符 <<(左移)、>>(右移)、>>>(无符号右移)、&、|、! 、^ 、~(取反) 注意:位运算符操作的都是整型的数据 ## 2.8 程序流程控制 ### 流程分类: - 顺序结构 - 分支结构(if-else、switch-case) - 循环结构(while、do…while、for) #### 2.8.1 分支结构 ```java /* 第一种: if(条件表达式){ 执行表达式 } */ public class Test { public static void main(String[] args) { // TODO 自动生成的方法存根 if(1 < 2) { System.out.println("true"); } } } /* 第二种:二选一 if(条件表达式){ 执行表达式1 }else{ 执行表达式2 } */ public class Test { public static void main(String[] args) { // TODO 自动生成的方法存根 if(1 < 2) { System.out.println("true"); }else { System.out.println("false"); } } } /* 第三种:多中结果选其一 if(条件表达式){ 执行表达式1 }else if(条件表达式){ 执行表达式2 }else if(条件表达式){ 执行表达式3 } ... else{ 执行表达式n } */ //多重判断 public class Test { public static void main(String[] args) { int age = 21; if(age > 60) { System.out.print("老年"); }else if(age > 40) { System.out.println("中年"); }else if(age > 18) { System.out.println("青年"); }else{ System.out.print("少年"); } } } /* switch-case */ switch-case语法: switch(表达式){ case 常量1: 执行语句1; // break; case 常量2: 执行语句2; // break; ... default: 执行语句n; // break; ``` #### 2.8.2 循环结构 -------for循环------- ```java /* for循环结构: for(循环变量初始化①;循环条件②;循环变量迭代③){ 循环体④ } 执行过程:①->②->③->④->②->③->④->...->② */ //例 public class Test1 { public static void main(String[] args) { // 循环输出10行"hello world" for (int i = 0; i < 10; i++) { System.out.println("hello world"); } } } //循环嵌套 public class Test2 { public static void main(String[] args) { // 输出3行4列的"hello world" for(int i=1; i<=3;i++) //外层循环控制行 { for(int j=1;j<=4;j++) //内层循环控制列 { System.out.print("Hello World"); } System.out.println(); //打印换行在内层循环结束后 } } } //循环嵌套打印九九乘法表 int f = 0; for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) { f = i*j; System.out.print(i+"*"+j+"="+f+"\t"); }System.out.println(); } //for嵌套if判断(计算1~100之间偶数之和) int sum = 0;//初始化 for(int i = 1;i <= 100;i++) { if(i % 2 == 0) //判断i是否为偶数 { sum = sum +i;//累加 } } ``` ------while循环------- ````java /* while循环的结构 ① 初始化条件 while(② 循环条件){ ③ 循环体; ④ 迭代条件; } 执行过程:① - ② - ③ - ④ - ② - ③ - ④ - ... - ② 注意: while循环不能没有迭代条件,缺少可能导致死循环! for与while区别:for循环和while循环的初始化条件部分的作用范围不同。for循环和while循环是可以相互转换。 */ public class Test1 { public static void main(String[] args) { // 遍历100以内所有奇数 int i = 1; while (i <= 100) { if (i % 2 != 0) { System.out.print(i + " "); // 1 3 5 7 9 ...... } i++; } } } ```` ------do-while 循环------ ```java /* do-while循环结构: ① 初始化条件 do{ ③ 循环体; ④ 迭代条件; }while(② 循环条件); 执行过程:① - ③ - ④ - ② - ③ - ④ - ... - ② 注意: do-while循环至少会执行一次循环体! */ public class DoWhileTest { public static void main(String[] args) { // 遍历10以内的偶数 int i = 1; do { if (i % 2 != 0) { System.out.print(i + " "); // 1 3 5 7 9 ...... } i++; } while (num <= 10); } } ``` #### 2.8.3 跳转语句 ```java /* break 强制退出循环 continue 直接跳过循环体内剩余语句,只能应用于for、while、do-while循环语句 */ ``` # 3 .数组字符串 ## 3.1 一维数组 #### 创建数组 •一维数组的声明格式有两种,分别是: 数据类型[] 数组名; 数据类型 数组名[]; ```java int[] scores; //定义存储分数的数组,类型为整型 double height[]; //定义存储身高的数组,类型为浮点型 String[] names; //定义存储姓名的数组,类型为字符串。 /* 初始化方法: 使用关键字new为数组分配存储空间 格式如下: 类型标识符 数组名[]=new 类型标识符[数组长度]; 类型标识符[] 数组名=new 类型标识符[数组长度];*/ int[] scores = new int[5]; String names[] = new String[3]; //创建数组并赋值 int array [ ]={1,2,3,4,5}; String hobbys [ ]={"music", "sports", "game"}; ``` #### 数组的下标 在对数组元素创建后,就可以通过数组的下标来访问数组元素,格式为:数组名[下标] ```java int[] scores = {95,93,80,86,79} System.out.print("scores数组中第1个元素的值:" + scores[0]);//注意:数组的下标从0开始 ``` 循环输出: ```java int[] ns = {1,2,3,34,4,5,} //循环遍历输出 for(int i = 0;i <= ns.length-1;i++) { //因为第一个下边时0 所以数组长度为:数组名.length-1 System.out.println(ns[i]); } ``` 数组冒泡排序: ```java int[] ns = {1,2,3,34,4,5,} //冒泡排序 for(int i = 0;i < ns.length-1;i++) { for(int j = 0;j < ns.length-i-1;j++) { if(ns[j] < ns[j+1]) { int t = ns[j]; ns[j] = ns[j+1]; ns[j+1] = t; } } } System.out.println(Arrays.toString(ns));//通过arrays方法类输出为字符串,用法是arrays.toString(数组名)。 //循环遍历输出 for(int i = 0;i <= ns.length-1;i++) { System.out.println(ns[i]); } ``` ## 3.2 二维数组 二维数组的声明格式 数据类型[][] 数组名; 数据类型 数组名[][]; 数据类型[] 数组名[]; 例如: int[][] array1; double array2[][]; char[] array3[]; 创建: 数组名[行的索引][列的索引] = 值; ## 3.3字符串应用 ```java public class T0530 { public static void main(String[] args) { // 字符串应用 String s1 = "This is a String"; String s2 = new String("This is a String"); System.out.println(s1); System.out.println(s2); //String 类对象的常用方法 //获取字符串的长度 length() String str = "This is a String"; int len = str.length(); System.out.println(len); //16 包括空格 //字符串比较 equals() 和 equalsIgnoreCase() System.out.println("JAVA".equals("java"));//equals区分大小写,结果false System.out.println("JAVA".equalsIgnoreCase("java"));//不区分大小写,结果true //字符串查找 indexOf() String str1 = "This is a String"; String str2 = "h"; int index = str.indexOf(str2);//从str1的开始位置查找str2字符串,查找从0位开始 System.out.println(index);//1 //字符串连接 concat() String str3 = "This is a String"; String str4 = str3.concat("Test"); System.out.println(str4);//This is a StringTest //字符串替换 replace() String str5 = "This is a String"; String str6 = str5.replace("T","t"); System.out.println(str6);//this is a String //字符串截取 substring() String str7 = "This is a String"; String sub1 = str7.substring(3);//s is a String String sub2 = str7.substring(3,9);//s is a System.out.println(sub1);//截取第3个字符后的内容 System.out.println(sub2);//截取3 - 9位之间 //获取指定位置的字符 charAt() String str8 = "This is a String"; char chr = str8.charAt(3); // s System.out.println(chr); //实现字符串的大小写转换 toUpperCase() 和 toLowerCase() String str9 = "This is a String"; String str10 = str9.toUpperCase(); System.out.println(str10);//转换成大写 String str11 = "THIS IS A STRING"; String str12 = str11.toLowerCase(); System.out.println(str12);//全部转换成小写 //字符串转换成字符数组 toCharArray() String str13 = "hello"; char[] arr = str13.toCharArray(); //System.out.println(arr[0]);//h //循环输出 for(int i = 0;i < arr.length;i++){ System.out.print(arr[i]); } } } ``` # 4.面对对象 面向对象的基本概念,包括: - 类 - 实例 - 方法 面向对象的实现方式,包括: - 继承 - 多态 Java语言本身提供的机制,包括: - package - classpath - jar 以及Java标准库提供的核心类,包括: - 字符串 - 包装类型 - JavaBean - 枚举 - 常用工具类