2023.04.11-05.12学习笔记
基本语法
- 类名首字母大写,遵循驼峰命名法,如Userlnfo
- 方法名首字母小写,变量首字母小写,后面遵循驼峰命名法
- 源文件名必须和类名相同,后缀是.java,编译后文件后缀是.class
- 主方法入口,所有的java程序由public static void main(String []args)方法开始执行。
Java标识符
- 由26个英文字母大小写、数字(0-9)、符号(_和$)组成
- 标识符不能以数字开头
- 标识符不能是关键字(如class)
JDK JRE JVM
JDK(Java开发工具包)是整个Java的核心,包括了Java运行环境,基本组件(Javac/java/jdb)和Java基础的类库
JRE(java 运行环境)没有任何开发工具,包括JVM虚拟机和Java核心类库
** JVM** (Java虚拟机)因为Java语言使用JVM屏蔽了具体平台相关的信息,所以可以一次编译,多处运行
IDE开发工具——javac编译器——JVM——操作系统
.java .class 汇编指令
只有jvm可以运行代码吗?
不行,运行代码还需要运行时依赖的java类库的支持
JVM加载.class字节码文件的步骤
第一阶段是找到.class文件并把这个文件包含的字节码加载到内存中。 第二阶段又可以分为三个步骤,分别是字节码验证、Class类数据结构分析及相应的内存分配和最后的符号表的链接。 第三阶段是类中静态属性和初始化赋值,以及静态块的执行。
main / 扫描器
import java.until.Scanner
public class Test {
// alt + / 智能提示
public static void main (String[] args){
System.out.println("aaaaa");
//println 输出完内容会加一个换行
System.out.print("bbbbb");
//输入
//创建了一个扫描器
//使用别人写的类型,需要导包
Scanner sc = new Scanner(System.in);
// 开始扫信息
String s = scanner.next();
String s2 = scanner.next();
System.out.println(s);
}
}
import java.until.Scanner
public class Test {
// alt + / 智能提示
public static void main (String[] args){
//创建扫描器
Scanner sc = new Scanner(String.in);
System.out.println("请输入第一个数字“);
//把扫到的第一个数字放入a变量中
int a = sc.nextInt();
System.out.println("请输入第二个数字“);
int b = sc.nextInt();
//打印结果
System.out.println(a+b);
}
}
数据类型
变量
用于在内存中保存数据。可以在同一类型范围内不断变化。
变量类型、变量名、存储值。
先声明,再赋值,再使用
注意变量的作业域
String a = ABC
变量的类型
- 基本数据类型(八种)
** byte**
以8位二进制存储。有正负号。最小值-128,最大值127。
** short**
以16位二进制存储。有正负号。最小值-2^15
最大值2^15 -1。
** int **
以32位二进制存储。有正负号。最小值-2^31,
最大值2^31-1。
** long **
以64位二进制存储。有正负号。最小值-2^63,
最大值2^63-1。
long l1 = 20L
bit 、byte 与字节(Byte)的关系
** float**
以科学计数法方式存储 ,32位
float f1 = 2.2f;
float f1 = 2.2F;
** double**
以科学计数法方式存储 ,64位
== 单精度和双精度的概念与区别==
char
16进制,先编码再存储,可以存储任何字符。
char c1 = 'a';
boolean
只有两个值,ture 和 false
== 默认值是false==
- 引用类型
自动类型转换、强制类型转换
byte b3 = 5;
int i3 = b3 ;
//自动类型转换
int i4 = 5 ;
byte b4 = (byte)i4;//强制
*注意
数据类型在内存中的空间分配
装箱与拆箱
提供一种机制,使得基本数据类型可以与引用类型互相转换
Integer i7 = new Integer(20);
int i8 = 10;
Integer i9 = Integer.valueOf(i8);//使用valueOf包装
//装箱方法名valueOf
double d5 = 3.3;
Double d7 = Double.valueOf(d5);
//拆箱方法名 xxxValue()
int i10 = i9.intValue();//拆箱
double d8 = d7.doubleValue();
//jbk1.5 自动 只发生在javac阶段(编译)
Integer i92 = i8
int i93 = i92
运算符
分类:算术运算符、比较运算符、逻辑运算符、赋值运算符、位运算运算符、三目运算符
算术运算符
package day01;
public class Test01 {
public static void main(String[] args) {
int a = 10;
int b = 2;
System.out.println(a / b );//5
System.out.println(a % b );//0
}
}
a++和++a的区别
逻辑运算符
&&(并且):两边都为真为真
比较运算符
比较完得到boolean类型
短路运算
例: 这里因为短路所以e没有++
赋值运算符
三目运算符
package day01;
public class Test01 {
public static void main(String[] args) {
int f = 10;
int e = 2;
//e>f是比较条件,条件为真。返回:前面的值,反之
int g = e > f ? e : f;
System.out.println(g);
}
}
位运算符
// 0000 0101
// 0000 0011
// 0000 0001
int h = 5;
int i = 3;
System.out.println(5&3);
代码结果是1
运算符的优先级
例题(重点)
package day01;
public class Test02 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a==b);//结果是false
Integer a1 = new Integer(10);
Integer b1 = new Integer(10);
// new 就是开一块堆空间
//都是引用类型,所以==比较的a1与b1中保存的地址是否一样
System.out.println(a1 == b1);//结果是false
//引用类型与基本数据类型运算,引用类型会自动拆箱
System.out.println(a1 == a);//结果是true
// 默认为 -128 到127 byte范围
Integer a2 = 10;
Integer b2 = 10;
Integer.valueOf(10)
//valueOf 包装方法不一样
// a2 与b2地址是一样的
System.out.println(a2 == b2);//结果是true
Integer a3 = 200;//当值不在范围内就另开空间
Integer b3 = 200;
System.out.println(a3 == b3);//结果是false
}
}
程序结构
顺序结构
分支结构
else 、switch
package day01;
public class Test03 {
public static void main(String[] args) {
int i = 10;
if(i > 10) {
System.out.println("i的值 大于10");
}
else {
System.out.println("i的值 不大于10");
}
System.out.println("if 结束了");
System.out.println("main 继续");
//else在不需要时可省略
//{}省略默认{}内包着一条语句
//多条件分支
int w = 3;
if ( w == 1) {
System.out.println("星期一");
} else if ( w == 2) {
System.out.println("星期二");
}else if ( w == 3) {
System.out.println("星期三");
}else if ( w == 4) {
System.out.println("星期四");
}else if ( w == 5) {
System.out.println("星期五");
}else if ( w == 6) {
System.out.println("星期六");
}else if ( w == 7) {
System.out.println("星期天");
}else {
System.out.println("w 值不对");
}
// switch() 中的变量支持的数据类型 break:结束判断
//byte short char int
//上面四个对应的包装类也支持
//jdk1.7时 新增了对String 的支持
//枚举
switch(w) {
case 1:
System.out.println("星期一 ");
break;
case 2:
System.out.println("星期二 ");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四 ");
break;
case 5:
System.out.println("星期五 ");
break;
case 6:
System.out.println("星期六 ");
break;
case 7:
System.out.println("星期日 ");
break;
default:
System.out.println("w 值不对 ");
break;
}
}
}
循环类型
while
package day01;
public class Test04 {
//循环
public static void main(String[] args) {
int i = 1;
// while( i <= 10 ) {
// System.out.println(i++);
// }
while(true) {
System.out.println(i++);
if(i == 11) {
break;//手动结束循环
}
}
// do while
do {
System.out.println(i++);
if(i == 11) {
break;//手动结束循环
}
} while(true);
// do while 至少会执行一次
}
}
for 循环
package day01;
public class Test05 {
public static void main(String[] args) {
//()中有三段内容 用;隔开
// 1. 一般用来初始化一个或者多个值
// 2.条件
// 3.步长(修改影响条件的变化)
for( int i = 1; i < 11; i++) {
System.out.println(i);
}
// 省略第一段
int i = 1; // 变量作用域不同
// for( ; i < 11; i++) { // 注意不要忘记";"
// System.out.println(i);
// }
// System.out.println(i);
//省略
for( ; ; ) {
System.out.println(i);
i++;
if ( i == 11)
break;
}//System.out.println(i);
}
package day01;
public class Test06 {
public static void main(String[] args) {
// for (int i = 1; i < 11; i++) {
// if (i == 5) {
// continue;//当次循环结束,进入第三段
// }
// System.out.println(i);
// }
// 嵌套for循环
for(int i = 0; i < 3;i++) {
for (int j = 0; j < 5; j++) {
System.out.println(j);
}
}
// *****
// *****
// *****
// *****
// *****
for (int i = 0; i < 5;i++) {
for(int j =0 ;j < 5;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
练习题
package day01;
public class Test07 {
public static void main(String[] args) {
for (int i = 0;i < 3; i++) {
// for (int j = 1; j <= 3; j++) {
// for (int a = 1; ;) {
// a = 1 + (j - 1) * 2;
//
//}System.out.print("*");
}System.out.println(" ");
}
}
}
答案
package day01;
public class Test07 {
public static void main(String[] args) {
//等腰三角形
for(int i = 1; i <= 3; i++) {
//输出空格
for(int k =5 ; k>i; k--) {
System.out.print(" ");
}
//打印*号
for(int j =1; j<=2*i-1; j++) {
System.out.print("*");
}
//换行
System.out.println();
}
}
}
package day01;
public class Test07 {
public static void main(String[] args) {
for (int i = 1;i <= 9; i++) {
for (int j = 1; j <= i; j++ ) {
//int c=j * i;
System.out.print(i + "*" + j + "=" + (i*j) + "\t");
}
System.out.print(" " );
System.out.println( );
}
}
}
package day01;
public class Test07 {
public static void main(String[] args) {
double sum = 0;
int day = 0;
while (true) {
day++;
sum += 5;
if(sum >= 56.7) {
break;
}
sum -= 3.5;
}System.out.println(day);
}
}
变量和方法
变量
成员变量、局部变量。
自定义方法
{} 内是方法体。
调用方法
package day01;
public class Teat08 {
public static void test1() {
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
}
public static void main(String[] args) {
test1();//方法调用
System.out.println("test1结束了");
}
}
带参方法
需要提供a
package day01;
public class Teat08 {
public static void test1() {
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
}
public static void add(int a,int b) {
System.out.println(a+b);
}
public static void main(String[] args) {
test1();//方法调用
System.out.println("test1结束了");
add(3, 5);
}
}
形参与实参
返回值
上面的8是 a+b
下面的8是c
** 无返回值方法**
有返回值方法
值传递与引用传递
值传递
package day01;
public class Test09 {
//形参名 无所谓
public static void t1(int b) {
b = 5;
}
public static void main(String[] args) {
int a = 10;
t1(a); //基本数据类型传递是把真实的值传递
System.out.println(a);// 10
}
}
package day01;
public class Test09 {
//形参名 无所谓
public static void t1(int b) {
b = 5;
}
public static void main(String[] args) {
int a = 10;
t1(a); //基本数据类型传递是把真实的值传递
System.out.println(a);// 10
// 造一个学生
Student stu = new Student();
stu.no = "20230001";
stu.name = "张三";
stu.age = 20;
System.out.println(stu.name);
t2(stu); // 引用类型是把地址传到方法里
System.out.println(stu.name);
}
public static void t2(Student a) { //形参名无所谓
a.name = "李四";
}
}
package day01;
public class Test09 {
//形参名 无所谓
public static void t1(int b) {
b = 5;
}
public static void main(String[] args) {
int a = 10;
t1(a); //基本数据类型传递是把真实的值传递
System.out.println(a);// 10
// 造一个学生
Student stu = new Student();
stu.no = "20230001";
stu.name = "张三";
stu.age = 20;
System.out.println(stu.name);
t3(stu); // 引用类型是把地址传到方法里
System.out.println(stu.name);
}
public static void t2(Student a) { //形参名无所谓
a.name = "李四";
}
public static void t3(Student b) {
b = new Student();
b.name = "李四";
}
}
方法重载
重载就是重载的参数列表
重载与重写的区别
方法的重载和重写都是实现多态的方式,区别在于前者实现的是编译时的多态性,而后者实现的是运行时的多态性。重载发生在一个类中,同名的方法如果有不同的参数列表(参数类型不同、参数个数不同或者二者都不同)则视为重载;重写发生在子类与父类之间,重写要求子类被重写方法与父类被重写方法有相同的参数列表,有兼容的返回类型,比父类被重写方法更好访问,不能比父类被重写方法声明更多的异常(里氏代换原则)。重载对返回类型没有特殊的要求,不能根据返回类型进行区分。
原文链接:https://blog.csdn.net/wintershii/article/details/80558739
package day01;
public class Test10 {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public void t1(int a, String b) {
}
public void t1( String d,int c) {
}
public static int add(int c, int d,int e) {
return c + d ;
}
public static void main(String[] args) {
add(3,5);
}
}
数组
是引用类型。存储固定数量的同类型元素的一种数据结构。
package day01;
public class Test11 {
public static void main(String[] args) {
//第一种 声明的同时 赋值
int[] array1 = { 1, 2, 3, 4, 5 };
//
int[] array2 = new int[] { 1, 2, 3, 4, 5 };
//第二种
int[] array4 = new int[8];
String[] array5 = new String[3]; // null null null
// 中括号中的值为下标值:0表示第一个。。。5表示第六个
array4[0] = 1;
test1(array4);
}
public static void test1(int[] array) {
// length属性获取长度
System.out.println(array.length);
}
}
访问数组元素
遍历数组
for
可以知道是第几次循环
for each
排序算法
冒泡排序
int[] array6 = { 3, 20, 6, 2, 18, 44, 68} ;
for (int i = 0; i < array6.length - 1; i++) { //定义冒几轮
// 循环体中是控制一轮的过程
for (int j = 1; j < array6.length - i; j++ ) {
if (array6[j] > array6[j + 1]) {
int temp = array6[j];
array6[j] = array6[j + 1];
array6[j + 1] = temp;
}
}
// 冒完一轮打印一次
System.out.println(Arrays.toString(array6));
}
选择排序
代码
插入排序
数组元素查找
顺序查找
二分查找
前提:顺序排好
可变长参数
public static void main (String[] ards) {
int sum = add();
System.out.println(sum);
}
// 给整数相加
public static int add(int ... a) {
int sum = 0;
for(int temp : a) {
sum += temp;
}
return sum;
}
Arrays工具类
** 数组赋值**
public static void main (String[] ards) {
int sum = add(3);
System.out.println(sum);
int[] arr = {4, 3, 5, 2, 3, 6 };
// Arrays.fill(arr, 0);
Arrays.fill(arr,2,4,0);//2,4是下标值,
System.out.println(Arrays.toString(arr));
}
例题
package day02;
public class Test13 {
public static void main(String[] args) {
System.out.println(t1(8));
}
public static int t1(int a) {
if(a == 1) {
return 1;
}
return a * t1(a - 1);
}
}
多维数组
public static void t2() {
int [] [] arr = new int[3][];
//二维数组里的一维
int[] [] arr2 = { { 1, 2 }, { 3, 4 }, {5, 6, 7} };
}
package day02;
public class Test13 {
public static void main(String[] args) {
System.out.println(t1(8));
t2(); //调用方法
}
public static int t1(int a) {
if(a == 1) {
return 1;
}
return a * t1(a - 1);
}
public static void t2() {
//二维数组里的一维
int[][] arr2 = { { 1, 2 }, { 3, 4 }, {5, 6, 7} };
int [][] arr = new int[3][]; //先创建完再赋值
arr[0] = new int[] { 1, 2};
arr[1] = new int[] { 3, 4 };
arr[2] = new int[] { 5, 6, 7};
System.out.println(arr[0][0]);
}
}
注:
在 int [][] arr = new int[3][]; 中,数组里面是空的,但是数组为引用类型,所以是null。
package day02;
public class Test13 {
public static void main(String[] args) {
System.out.println(t1(8));
t2();
}
public static int t1(int a) {
if(a == 1) {
return 1;
}
return a * t1(a - 1);
}
public static void t2() {
//二维数组里的一维
int[][] arr2 = { { 1, 2 }, { 3, 4 }, {5, 6, 7} };
int [][] arr = new int[3][]; //先创建完再赋值
// arr[0] = new int[] { 1, 2};
// arr[1] = new int[] { 3, 4 };
// arr[2] = new int[] { 5, 6, 7};
System.out.println(arr[0][0]);
}
}
int[] arr3 = new int[5];
System.out.println(arr3[0]);
上面的代码输出为0,因为二维数组中的一维数组是引用类型,但是上面代码只是类型为int的一维数组。
//表示在这个二维数组中,有三个一维数组并且每个一维都是4个长度
// 必须固定二维数组的长度
int [][] arr4 = new int [3][4];
arr4[0][0] = 1;
System.out.println(arr4[0][1]);
** 二维数组遍历**
package day02;
import java.util.Arrays;
public class Test13 {
public static void main(String[] args) {
// System.out.println(t1(8));
t2();
int[] arr2 = { 1, 2, 3};
t3(arr2);
System.out.println(Arrays.toString(arr2));
}
public static int t1(int a) {
if(a == 1) {
return 1;
}
return a * t1(a - 1);
}
public static void t2() {
//二维数组里的一维
// int[][] arr2 = { { 1, 2 }, { 3, 4 }, {5, 6, 7} };
// int [][] arr = new int[3][]; //先创建完再赋值
// arr[0] = new int[] { 1, 2};
// arr[1] = new int[] { 3, 4 };
// arr[2] = new int[] { 5, 6, 7};
// System.out.println(arr[0][0]);
int[] arr3 = new int[5];
System.out.println(arr3[0]);
//表示在这个二维数组中,有三个一维数组并且每个一维都是4个长度
// 必须固定二维数组的长度
int [][] arr4 = new int [3][4];
arr4[0][0] = 1;
System.out.println(arr4[0][0]);
}
public static void t3(int[] arr) {
arr[0] = 4;
}
}
数组是引用类型,所以在传递的时候传递的是地址。
数组例题
第一题
package day02;
import java.util.Arrays;
public class Test14 {
public static void main (String[] args) {
int[] source = {22, 68, 52, 12,8};
t1(source);
System.out.println(Arrays.toString(source));
}
public static void t1(int[] source) {
for (int i = 0; i < source.length / 2 ; i++) {
int t ;
t = source[source.length -1 -i];
source[source.length -1 -i] = source[i];
source[i] = t ;
}
}
}
参考:
public class RunoobTest {
/* 创建方法,第一个与最后一个交互,第二个与倒数第二个交换,以此类推*/
static void reverse(int a[], int n)
{
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
System.out.println("反转后的数组是: \n");
for (k = 0; k < n; k++) {
System.out.println(a[k]);
}
}
public static void main(String[] args)
{
int [] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
}
}
import java.util.*;
public class RunoobTest {
/* 使用 java.util.Arrays.asList(array) 方法*/
static void reverse(Integer a[])
{
Collections.reverse(Arrays.asList(a));
System.out.println(Arrays.asList(a));
}
public static void main(String[] args)
{
Integer [] arr = {10, 20, 30, 40, 50};
reverse(arr);
}
}
第二题
package day02;
public class Test15 {
public static void main(String[] args) {
int[ ] a = {1, 2, 4, 9, 3, 9, 5, 6,7};
t1(a);
//System.out.println();
}
public static void t1(int[] a) {
int j =0;
for(int i = 0;i < a.length; i++) {
if (a[i] == 9) {
j++;
}
}System.out.println(j);
}
}
第五题
package day02;
public class Test16 {
public static void main(String[] args) {
int[] a = {1, 2, 3, 14, 5, 6, 7};
t1(a);
}
public static void t1(int[] a) {
int max=0;
int t =0;
for (int i = 0; i < a.length ; i++) {
if(a[i] > max) {
max = a[i] ;
t = i;
}
}System.out.println(max);
System.out.println(t);
}
}
第六个
package day02;
import java.util.Arrays;
public class Test17 {
public static void main(String[] args) {
int [] a = {1, 2, 3, 4, 5, 6,7};
t1(a);
}
public static void t1(int[] a) {
// int[] j ;
// int[] o ;
int k=0,b=0,jnull=0,onull=0;
for (int i = 0; i < a.length;i++) {
if (a[i] % 2 == 0) {
// j [k] = a[i];
// k++;
jnull++;
} else if (a[i] % 2 != 0){
// o[b] = a[i];
// b++;
onull++;
}
}
// int [] j = new int[];
if (jnull > onull) {
int[] o = new int [(a.length -1) /2 ];
int[] j = new int [a.length - o.length];
for (int i = 0; i < a.length;i++) {
if (a[i] % 2 == 0) {
j[k] = a[i];
k++;
// jnull++;
} else if (a[i] % 2 != 0){
o[b] = a[i];
b++;
// onull++;
}
}
System.out.println(Arrays.toString(j));
System.out.println(Arrays.toString(o));
}
if (jnull == onull) {
int[] j = new int [a.length /2 ];
int[] o = new int [a.length /2];
for (int i = 0; i < a.length;i++) {
if (a[i] % 2 == 0) {
j[k] = a[i];
k++;
// jnull++;
} else if (a[i] % 2 != 0){
o[b] = a[i];
b++;
// onull++;
}
}
System.out.println(Arrays.toString(j));
System.out.println(Arrays.toString(o));
}
else if (jnull < onull) {
int[] j = new int [(a.length -1) /2 ];
int[] o = new int [a.length - j.length];
for (int i = 0; i < a.length;i++) {
if (a[i] % 2 == 0) {
j[k] = a[i];
k++;
// jnull++;
} else if (a[i] % 2 != 0){
o[b] = a[i];
b++;
// onull++;
}
}
System.out.println(Arrays.toString(j));
System.out.println(Arrays.toString(o));
}
}
}