行业知识
【狂神说Java】Java基础详细笔记(全)
2026-12-05 02:54:17
来源:互联网转载
狂神说java
文章目录
- 0x00 java基础
- 0x00 java程序运行机制
- 0x00 hello world
- 0x01 java基础语法
- 0x00 基本数据类型
- 0x01 类型转换
- 0x02 变量作用域
- 0x03 命名规范
- 0x04 运算符
- 0x05 JavaDoc
- 0x02 java流程控制
- 0x00 用户交互Scanner
- 0x01 Switch
- 0x02 For
- 0x03 Break 和 Continue
- 0x03 java方法
- 0x00 方法重载
- 0x01 可变参数
- 0x02 递归
- 0x04 数组
- 0x00 数组基础
- 0x01 二维数组
- 0x02 Arrays类
- 0x03 稀疏矩阵
- 0x05 面向对象
- 0x00 基础
- 0x01 构造函数
- 0x02 封装
- 0x03 继承
- 0x04 重写
- 0x05 多态
- 0x06 instanceof
- 0x07 抽象类和接口
- 0x08 内部类
- 0x06 异常
- 0x00 try-catch
- 0x01 自定义异常
0x00 java基础
- jdk 开发环境
- jre 运行环境
- jvm java虚拟机
0x00 java程序运行机制
0x00 hello world
public class Hello{ public static void main(String[] args){System.out.print("Hello world!");}}cmd下javac hello.java编译生成hello.class文件 字节码文件运行: java hello #若存在包,最要退回去包之外,例如 java com.test.method.hello0x01 java基础语法
0x00 基本数据类型
public class Demo1 { public static void main(String[] args) { // 八大基本数据类型 // 整数 byte num1 = 12; int num2 = 12; short num3 = 122; long num4 = 122L; // long在后面加个 L // 小数:浮点数 float num5 = 30.1F; // float在后面加个 F double num6 = 3.1415; // 字符 char num = 'A'; // 布尔值 boolean flag = true; String str = "string"; System.out.println(str); }}0x01 类型转换
精度从低到高byte, short, char -> int -> long -> float -> double数据从低变高自动转换数据从高变低需要强制转换存在溢出的可能注意 两个数相加,结果类型有 long 为 long有 double 为 double啥都没有就是 int
0x02 变量作用域
public class Demo02 { // 类变量 static static double salary = 2500; // 实例变量,可以不赋值 // 默认是 0 0.0 False null String str; int age; Boolean flag; // 常量 final 修饰符不在意位置 static final double PI = 3.1415; // main方法 public static void main(String[] args) { Demo02 demo02 = new Demo02(); System.out.println(salary); //可直接用 System.out.println(demo02.age); //可直接用 System.out.println(PI); //可直接用 }}0x03 命名规范
- 类名: HelloWorld()
- 常量: MAX_VALUE
- 其他: getName()
0x04 运算符
算数运算符+ - * / % ++ --赋值运算符 = += -= *= /=关系运算符 > = > >>条件运算符 ? :Math类 2^3 = 8 Math.pow(2,3) = 8
0x05 JavaDoc
/*** @author Martin* @version 1.0* @since 1.8 #指明最早需要的jdk版本**/public class Doc { String name; /** * @param name * @return * @throws Exception */ public String getName(String name) throws Exception{ return name; }}在cmd命令下 javadoc -encoding UTF-8 -charset UTF-8 Doc.java 生成javaDoc文档,也可用IDEA生成
0x02 java流程控制
0x00 用户交互Scanner
import java.util.Scanner;public class Demo01 { public static void main(String[] args) { // 两个方法 // next() hasNext() 空格为结束符 // nextLine() hasNextLine() 回车为结束符 // 或者 nextDouble() 等等 // 创建一个扫描器对象,用于接收键盘输入 Scanner scanner = new Scanner(System.in); System.out.println("等待接收:"); // 判断用户有没有输入 if (scanner.hasNext()){ // 使用next接收 String str = scanner.next(); System.out.println("输出的内容为:"+ str); } // 关闭IO流 scanner.close(); }}0x01 Switch
public class Demo02 { public static void main(String[] args) { char c = 'A'; // jdk 7以后,表达式可以是 字符串 switch (c){ case 'A': System.out.println("优秀"); break; //不加的话,就不会退出,出现case穿透,导致下面的全部输出 case 'B': System.out.println("及格"); break; default: System.out.println("未知"); } }}0x02 For
/*1*1=12*1=2 2*2=43*1=3 3*2=6 3*3=94*1=4 4*2=8 4*3=12 4*4=165*1=5 5*2=10 5*3=15 5*4=20 5*5=256*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=367*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=498*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=649*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81*/public class Demo03For { public static void main(String[] args) { // 九九乘法表 for(int i=1;i for(int j=1;j System.out.print(i +"*"+ j +"="+ i*j + "\t"); } System.out.println(""); } // 增强FOR int[] numbers = {1,2,3,4,5}; for(int x:numbers){ System.out.print(x+"\t"); // 1 2 3 4 5 } }} public static void main(String[] args) { int num1 = test(10); double num2 = test(10); } public static int test(int num){ return num; } public static double test(double num){ return num; }} public static void main(String[] args) { Demo02 demo02 = new Demo02(); demo02.test(1,2,3,4,5); }// 本质是数组 public void test(int... i){ System.out.println(i[0]); System.out.println(i[1]); System.out.println(i[2]); System.out.println(i[3]); System.out.println(i[4]); }} public static void main(String[] args) { System.out.println(f(5)); //5! = 120 } public static int f(int num){ if (num==1){ return 1; }else { return num*f(num-1); } }} public static void main(String[] args) { // 1. 声明一个数组 int[] num1; int num2[]; // 2. 创建一个数组 num1 = new int[10]; // 3. 赋值 for (int i = 0; i Default>private - 抛出的异常:范围,可以被缩小,但不能扩大; classNotFoundException -->Exception(大)
public class Test { public static void main(String[] args) { // 左边 new 右边() A a = new A(); B b = new A(); // 父类的引用指向了子类 // 静态方法 与左边有关 A.testStatic(); // A->testStatic() B.testStatic(); // B->testStatic() // 非静态方法 与右边有关 a.test(); // A->test() b.test(); // A->test() 子类重写了父类的方法,所以父类调用子类 }}class A extends B{ // Override 重写,有特定功能的注解 Alt + Insert Override @Override public void test() { System.out.println("A->test()"); } public static void testStatic(){ System.out.println("A->testStatic()"); }}class B{ public void test(){ System.out.println("B->test()"); } public static void testStatic(){ System.out.println("B->testStatic()"); }}0x05 多态
多态的前提条件:
- 1 要有继承关系(实现关系)
- 2 要有方法的重写
- 3 要有父类的引用指向子类对象。
多态指的是同一个方法调用,由于对象不同可能会有不同的行为。
用该父类引用调用子类重写的方法,此时多态就出现了。
package oop.Demo06;// 多态public class Test { public static void main(String[] args) { Student s1 = new Student(); Person s2 = new Student(); // 通过父类 new一个子类 s1.run(); //Student run s2.run(); //Student run s1.say(); //Student say // 高变低 强制转换 ((Student)s2).say(); //Student say }}class Person{ public void run(){ System.out.println("Person run"); }}class Student extends Person{ @Override public void run() { System.out.println("Student run"); } public void say(){ System.out.println("Student say"); }}0x06 instanceof
X instanceof Y X和Y是不是有父子类的关系
0x07 抽象类和接口
- 普通类: 只有具体实现
- 抽象类: 可有规范也可有具体实现
- 接口: 只有规范
public class TestAbstract { public static void main(String[] args) { }}// 抽象类 abstractabstract class Action{ // 约束 有人帮我们实现 // abstract 抽象方法,只有名字没有实现 public abstract void doSomething(); public void run(){ } // 1.抽象类不能被 new 出来,只能子类实现 // 2.可以有普通的非抽象方法}// 子类必须实现 抽象类的所有方法,除非子类也是抽象类class A extends Action{ @Override public void doSomething() { }}接口不能被实例化
// 约束和实现分离public class TestInterface { public static void main(String[] args) { }}// interface 接口// 接口都需要有实现类interface UserService{ // 接口中所有的定义都默认是抽象的,默认有 public abstract void run();}interface TimeService{ void timer();}// 实现类, 可以间接实现伪多继承class UserServiceImpl implements UserService, TimeService{ @Override public void timer() { } @Override public void run() { }}0x08 内部类
package oop.Demo08;public class Test { public static void main(String[] args) { Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.in(); //Inner inner.getId(); //10 }}class Outer{ private int id=10; public void out(){ System.out.println("Outer"); } public class Inner{ public void in(){ System.out.println("Inner"); } // 获得外部类的私有属性 public void getId(){ System.out.println(id); } }}0x06 异常
0x00 try-catch
package exception;public class Demo01 { public static void main(String[] args) { int a = 1; int b = 0; // 假设捕获多个异常,要从小到大 // 快捷键 Ctrl + Alt + T try { if (b==0){ // 主动抛出异常,一般在方法中用 throw new ArithmeticException(); } System.out.println(a/b); }catch (ArithmeticException e){ System.out.println("b不能为0"); }catch (Error e){ System.out.println("error"); }finally { // 一般处理善后的工作,例如关闭IO System.out.println("finally"); }// b不能为0// finally try { test(a,b); } catch (ArithmeticException e) { e.printStackTrace(); } } public static void test(int a,int b) throws ArithmeticException{ if (b==0){ throw new ArithmeticException(); } }}0x01 自定义异常
package exception;// 自定义异常public class Demo02 { public static void main(String[] args) { try { test(11); } catch (MyException e) { System.out.println("MyException->" + e); // MyException->MyException{detail=11} } } public static void test(int a) throws MyException { if (a>10){ throw new MyException(a); } }}class MyException extends Exception{ private int detail; public MyException(int detail) { this.detail = detail; } @Override public String toString() { return "MyException{" + "detail=" + detail + '}'; }}