Java基础复习总结笔记(上)

前言

对Java部分基础知识进行复习总结,没事可以看看。

目录

前言

一、基础语法

1.1 Java名词

1.2 Java关键字

1.3 基本数据类型(4类8种)

(1)整数类型

(2)浮点类型

(3)字符型

(4)布尔型

1.4 基本数据类型包装类

(1)对应关系

(2)装箱与拆箱

(3)一些包装类方法

1.5 引用数据类型

(1)String类

(2)接口

(3)数组

二、流程控制

2.1 顺序结构

2.2 选择结构

(1)if语句

(2)switch语句

2.3 循环结构

(1)while 和do while

(2)for和增强for

2.4 break、continue、return

三、面向对象

3.1 构造方法、代码块、抽象类和栈堆内存

(1)声明对象和Lombok使用

(2)代码块

(3)抽象类

(4)栈堆内存

3.2 继承、封装、多态

(1)继承

(2)封装

(3)多态

3.3 内部类

四、异常处理

4.1 异常类关系

 4.2 异常处理

(1)捕获异常

(2)抛出异常

(3)throws和throw

 4.3 自定义异常

五、泛型注解

5.1 泛型

5.2 注解

(1)普通注解

(2)元注解

(3)自定义注解

六、JDBC

总结


Java和Maven安装演示环境为LinuxMint系统,xfce的桌面环境。

Java安装操作如下:

首先去官网(Java 软件 | Oracle 中国)下载JDK,我这边下载的是JDK19,然后进行安装。

然后进行环境变量配置,在目录下编辑.bashrc文件,在文件中添加如下内容:

export JAVA_HOME=/usr/lib/jvm/jdk-19
export PATH=$PATH:$JAVA_HOME/bin

在终端输入source .bashrc使之生效,通过输入java -version验证是否安装成功。

Maven安装操作如下:

首先去官网(Maven)下载,下载完之后解压到安装目录里,这里假定安装目录为/usr/lib/。

然后进行环境变量配置,在目录下编辑.bashrc文件,在文件中添加如下内容:

export MAVEN_HOME=/usr/lib/apache-maven-3.8.6
export PATH=$PATH:$MAVEN_HOME/bin

在终端输入source .bashrc使之生效,通过输入mvn -version验证是否安装成功。

Java和Maven环境变量配置具体如下图所示:

833de0cd0ad941b49fff5bd7a4af7b73.png如果使用VSCODE进行编写代码的话,在VSCODE中安装Java、Maven相应插件,之后配置如下:

  "java.jdt.ls.java.home": "/usr/lib/jvm/jdk-19",
  "maven.terminal.useJavaHome": true,
  "maven.executable.path": "/usr/lib/apache-maven-3.8.6/bin/mvn",
  "maven.settingsFile": "/usr/lib/apache-maven-3.8.6/conf/settings.xml",
  "java.configuration.maven.userSettings": "/usr/lib/apache-maven-3.8.6/conf/settings.xml"

如下图所示:

ab1644cff64149c0b349978d32363df8.png

反编译工具推荐Xjad这个软件。


一、基础语法

1.1 Java名词

JDK:Java开发环境,包括编译(Javac)、解释(Java)、打包(Jar)等工具。

JRE:Java运行环境,由Java的虚拟机和Java的API构成。

JVM:Java虚拟机。

字节码本质是一种标准化可移植的二进制格式。格式为.Class。一个Java程序可由不同的.Class文件构成。

1ad08b054fee4d849ccb0261d0a0a0a4.jpeg

1.2 Java关键字

abstract classextendsimplementsnullstrictfptrue
assert constfalseimportpackagesupertry
booleancontinuefinalinstanceofprivateswitchvoid
break defaultfinallyintprotectedsynchronizedvolatile
bytedofloatinterfacepublicthiswhile
casedoubleforlongreturnthrow
catchelsegotonativeshortthrows
charenumifnewstatictransient

上述关键字中,用于定义数据类型的关键字有:class interface enum byte short int long float double char boolean void; 

用于定义流程控制的关键字有:if else switch case default while do for break continue return;

用于定义访问权限修饰符的关键字有:private protected public;

用于定义类与类之间关系的关键字有:abstract final static  synchronized;

用于定义建立实例及引用实例,判断实例的关键字有:new this super instanceof;

用于处理异常的关键字有:try catch finally throw throws;

用于包的关键字有:package import;

其他修饰符关键字有:native strictfp transient volatitle assert;

goto、const在Java中没有任何意义。

strictfp用来确保浮点数运算的准确性,例如:

public strictfp class Test1 {
    public void sumab(float a,double b){
        double sum = a + b;
        System.out.println(sum);
    }
 
    @Test
    public void test(){
        float a = 0.123f;
        double b = 0.0123d;
        new Test1().sumab(a,b);
    }
 }

运行结果:

0.13530000339746476

Process finished with exit code 0

instanceof用于测试它左边的对象是否是它右边的类的实例,例如:

public class Test1 {
    @Test
    public void test(){
        int[] array = {1,2,3,4,5,6};
        System.out.println(array instanceof int[]);
    }
}

运行结果:

true

Process finished with exit code 0

transient只能用来修饰成员变量,被transient修饰的成员变量不参与序列化。

静态成员变量也不能被序列化,不管有没有transient关键字。

1.3 基本数据类型(4类8种)

(1)整数类型

byte字节数1-128、127
short 字节数2-32768、32767
int 字节数4-214783648、14783647
long字节数8-1L、031L、0X19L、0b11001L

(2)浮点类型

float字节数41.35
double字节数81.35f

(3)字符型

char字节数2'D'、'好'

(4)布尔型

boolean 字节数1true、flase

1.4 基本数据类型包装类

(1)对应关系

byte Byte
booleanBoolean
short Short
charCharacter
intInteger
longLong
floatFloat
doubleDouble

(2)装箱与拆箱

public class Test1 {       
    @Test
    public void test(){
        // 装箱
        Integer a = new Integer(10);
        // 拆箱
        int result = a.intValue();
        System.out.println(result+result);
        // 自动装箱
        Integer b = 20;
        // 自动拆箱
        int result2 = b;
        System.out.println(result2+result2);
    }
}

运行结果:

20
40

Process finished with exit code 0

(3)一些包装类方法

public class Test1 {       
    @Test
    public void test(){
        String a = "123";
		//将字符串转换成整型
		int b = Integer.parseInt(a);
		//将整型变为字符串
		String c = String.valueOf(b);
		System.out.println(a.getClass().getName());
		System.out.println(c.getClass().getName());
    }
}

运行结果:

java.lang.String
java.lang.String

Process finished with exit code 0

包装类对象之间值比较使用equals()方法。

1.5 引用数据类型

(1)String类

查找字符串的方法indexOf()、lastIndexOf(),例如:

public class Test1 {
    @Test
    public void test(){
        String a = "abcde";
        System.out.println(a.indexOf("a"));
        System.out.println(a.lastIndexOf("b"));
    }
}

运行结果:

0
1

Process finished with exit code 0

判定字符串一致的方法为equals()和==:equals()是比较值是否相等,==是否引用同一位置。例如:

public class Test1 {
    @Test
    public void test(){
        String a = "abcdef";
        String b = new String("abcdef");
        System.out.println(a.equals(b));
        System.out.println(a==b);
    }
}

运行结果:

true
false

Process finished with exit code 0

String、StringBuffer、StringBuilder:String是只读字符串,一旦被初始化,就不能改变,每次改变都会生成新的字符串,浪费内存;StringBuffer内容可改变,不生成新对象,节约内存,线程安全;StringBuilder操作效率比StringBuffer高,非线程安全。

public class App 
{
    public static void main( String[] args )
    {
        String a = "abcd";
        System.out.println(a);
        
        StringBuffer b = new StringBuffer("abcd");
        System.out.println(b);

        StringBuilder c = new StringBuilder("abcd");
        System.out.println(c);
    }
}

(2)接口

接口中有常量、抽象方法,也有非抽象方法,接口中定义的量为常量,无构造方法,例如:

public interface TestInterface {
    public static final int num =  1;
    public abstract void aaa();
    public abstract void bbb(int num);
    public default void ccc(){
        System.out.println("ccc");
    }
}

接口必须有子类,子类可同时实现多接口,例如:

interface aaa{
    public abstract void print();
}
interface bbb{
    public abstract void get();
}
class ccc implements aaa,bbb{
    public void print(){
        System.out.println("aaa");
    }
    public void get(){
        System.out.println("bbb");
    }
}

一句话:单继承,多实现

(3)数组

数组初始化,例如:

public class Test1 {
    @Test
    public void test(){
        int[] array1 = {1,2};
        int[] array2 = new int[]{1,2};
        int[] array3 = new int[2];
        array3[1] = 1;
        array3[2] = 2;
        int[][] array4 = {{1,2},{1,2,3},{1,2}};
    }
}

数组遍历方式,例如:

public class Test1 {
    int[] array1 = {1,2};
    int[][] array2 = {{1,2},{1,2,3}};
    @Test
    public void test(){
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
        for (int a:array1) {
            System.out.println(a);
        }
        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.print(array2[i][j]);
            }
            System.out.println();
        }
        for (int[] a:array2) {
            for (int b:a) {
                System.out.print(b);
            }
            System.out.println();
        }
    }
}

冒泡排序法,例如:

public class Test1 {
    int[] array1 = {1,2,1,6,2,3,5,4,3};
    @Test
    public void test(){
        for (int i = 0; i < array1.length; i++) {
            for (int j = i+1; j < array1.length; j++) {
                if (array1[i]<array1[j]){
                    int temp = array1[i];
                    array1[i] = array1[j];
                    array1[j] = temp;
                }
            }
        }
        for (int a:array1) {
            System.out.print(a+" ");
        }
    }
}

Arrays工具类,属java.util包下的类,常用方法例如:

public class Test1 {
    int[] array1 = {1,2,1,6,2,3,5,4,3};
    int[] array2 = {1,2,3,4,5,6};
    @Test
    public void test() {
        Arrays.sort(array1);
        //数组的遍历
        System.out.print(Arrays.toString(array1));
        System.out.println();
        //二分法查找有序数组    
        System.out.println(Arrays.binarySearch(array1,1));
        System.out.println(Arrays.equals(array1,array2));
        //数组复制
        int[] array3 = Arrays.copyOf(array2,3);
        //数组全部填充10
        Arrays.fill(array3,10);
        System.out.print(Arrays.toString(array3));
    }
}

运行结果:

[1, 1, 2, 2, 3, 3, 4, 5, 6]
7
false
[10, 10, 10]

二、流程控制

结构化程序设计,面向过程编程POP

2.1 顺序结构

public class Test1 {
    @Test
    public void test1(){
        System.out.println("语句1");
        System.out.println("语句2");
        System.out.println("语句3");
    }
}

2.2 选择结构

(1)if语句

if...else和位语句,例如:

public class Test1 {
    int a = 1;
    @Test
    public void test1() {
        if(a>0){
            System.out.println("a>0");
        }else if (a<0){
            System.out.println("a<0");
        }else {
            System.out.println("a=0");
        }
    }
    @Test
    public void test2(){
        if(a>0){
            System.out.println("a>0");
        }
        if(a<0){
            System.out.println("a>0");
        }
        if(a==0){
            System.out.println("a=0");
        }
    }
}

(2)switch语句

switch,例如:

public class Test1 {
    int a = 2;
    @Test
    public void test() {
        switch (a){
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            default:
                System.out.println("未知");
                break;
        }
    }
}

运行结果:

2

Process finished with exit code 0

2.3 循环结构

(1)while 和do while

while和do while,例如:

public class Test1 {
    int a = 2;
    @Test
    public void test() {
        while(a<3){
            a+=1;
        }
        System.out.println(a);
        do{
            a+=1;
        }while (a<10);
        System.out.println(a);
    }
}

运行结果:

3
10

Process finished with exit code 0

(2)for和增强for

public class Test1 {
    @Test
    public void test1(){
        int[] array = {1,2,3,4,5,6,7};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
        System.out.println();
        for (int a:array) {
            System.out.print(a);
        }
    }
}

运行结果:

1234567
1234567
Process finished with exit code 0

2.4 break、continue、return

public class TestJava {
    public static void main(String[] args){
        while(true) {
            System.out.println("猜数字,请输入猜测的数字:");
            Scanner scanner = new Scanner(System.in);
            int a = scanner.nextInt();
            switch (a){
                case 10:
                    System.out.println("猜测正确");
                    break;
                case 9:
                    System.out.println("没有猜对哦,猜测结束了");
                    return;
                case 8:
                    System.out.println("猜测错误");
                    break;
                case 7:
                    System.out.println("猜测错误");
                    break;
                case 6:
                    System.out.println("猜测错误");
                    break;
                case 5:
                    System.out.println("猜测错误");
                    break;
                case 4:
                    System.out.println("猜测错误");
                    break;
                case 3:
                    System.out.println("猜测错误");
                    break;
                case 2:
                    System.out.println("猜测错误");
                    break;
                case 1:
                    System.out.println("猜对一半,请继续");
                    continue;
                case 0:
                    System.out.println("猜对一半,请继续");
                    continue;
            }
            break;
        }
        System.out.println("谢谢参与");
    }
}

带标签的break语句使用。

public class Test1 {
    @Test
    public void test1(){
        for (int i = 0; i < 2; i++) {
            System.out.println("最外层循环"+i);
            loop:
            for (int j = 0; j < 2; j++) {
                System.out.println("中间层循环"+j);
                for (int k = 0; k < 2; k++) {
                    System.out.println("最内层循环"+k);
                    break loop;
                }
            }
        }
    }
}

运行结果:

最外层循环0
中间层循环0
最内层循环0
最外层循环1
中间层循环0
最内层循环0

三、面向对象

面向对象编程OOP

3.1 构造方法、代码块、抽象类和栈堆内存

(1)声明对象和Lombok使用

声明对象:类名称  对象名称  =  new  类名称()

new是作为开辟堆内存的唯一方法,实例化对象

类名称()是构造方法

Lombok的导入

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

@Data:相当于写了getter、setter、equals、hasCode、toString等方法

@AllArgsConstructor:相当于写了全参构造函数

@NoArgsConstructor:相当于写了无参构造函数

public interface PhoneInterface {
    public void listen();
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Phone implements PhoneInterface{
    private String brand;
    private int price;

    public void listen(){
        System.out.println(brand+"手机听歌");
    }
}

public class Test1 {
    @Test
    public void test() {
        Phone phone = new Phone("华为",12);
        phone.listen();
    }
}

运行结果:

华为手机听歌

Process finished with exit code 0

(2)代码块

静态代码块执行级别最高。静态代码块优先于构造代码块优先于构造函数。

静态代码块只执行一次,构造代码块、构造函数可执行多次。代码块例如:

public class TestJava {
    static{
        System.out.println("我是静态代码块");
    }
    public TestJava(){
        System.out.println("我是构造方法");
    }
    {
        System.out.println("我是构造代码块");
    }
    public static void main(String[] args){
        for (int i = 1; i < 3; i++) {
            System.out.println("创建第"+i+"个对象");
            new TestJava();
        }
    }
}

运行结果:

我是静态代码块
创建第1个对象
我是构造代码块
我是构造方法
创建第2个对象
我是构造代码块
我是构造方法

Process finished with exit code 0

(3)抽象类

抽象类必须有子类,使用extend继承抽象类,也是单继承,可含普通方法,有构造方法。抽象类例如:

abstract class Phone {
    private double size = 1.5;
    public void openPhone(){
        System.out.println("手机开机");
    }
    public abstract void closePhone();
}

(4)栈堆内存

内存分为三部分:栈、堆、方法区

栈内存:保存堆内存的地址、对象名称。

堆内存:保存真正的数据、对象属性信息、new出来的东西。

一块堆内存可被多个栈内存所指向。

例如如下代码用栈、堆、方法区图示:

public class TestJava {
    int a;
    String b;
    public TestJava(int a,String b){
        this.a = a;
        this.b = b;
    }
    public static void main(String[] args) {
        TestJava testJava = new TestJava(1,"嗯嗯");
    }
}

f3b5162ccc1a43a5bd638096c92c45ba.jpeg

3.2 继承、封装、多态

(1)继承

父类能出现的地方子类一定也要能出现。

public class TestJava {
    public static void main(String[] args) {
        Student student = new Student(50,"化学");
        student.eat();
        student.Study();
    }
}
@AllArgsConstructor
class Person{
    int height;
    void eat(){
        System.out.println("我有"+height+"公斤重,我要吃饭");
    }
}
class Student extends Person{
    String learn;
    public Student(int height,String learn) {
        super(height);
        this.learn = learn;
    }
    void Study(){
        System.out.println("我在学习"+learn);
    }
}

运行结果:

我有50公斤重,我要吃饭
我在学习化学

Process finished with exit code 0

(2)封装

Java访问权限修饰符:public、private、protected、default

public class TestJava {
    public static void main(String[] args) {
        Person person = new Person();
        person.setHeight(10);
        person.speak();
    }
}
@Data
class Person{
    private int height;
    void speak(){
        System.out.println("我的体重是"+height);
    }
}

(3)多态

父子类对象转型:向上转型、向下转型

向上转型:父类 父类对象 = new 子类类型

向下转型:子类 子类对象 = (子类)new 父类对象

public class TestJava {
    public static void main(String[] args) {
        Father father = new Son();
        father.fatherMethod();
        ((Son) father).sonMethod();
        Son son = (Son) new Father();
        son.fatherMethod();
        son.sonMethod();
    }
}
class Father{
    public static void fatherMethod(){
        System.out.println("父类方法");
    }
}
class Son extends Father{
    public static void sonMethod(){
        System.out.println("子类方法");
    }
}

3.3 内部类

标识符class外部类{

   外部类成员

   标识符class内部类{

        内部类成员

  }

}

四、异常处理

异常,计算机无法正常处理的情况

4.1 异常类关系

4a2ea6a5a92842bf9c4df3497f0f5caf.png

 Error为JVM错误,无需自己处理,Exception为程序可处理异常。

 4.2 异常处理

(1)捕获异常

两种方式try...catch和try...catch...finally。

public class Test1 {
    @Test
    public void test() {
        try{
            int a = 1/0;
        }catch (ArithmeticException arithmeticException){
            System.out.println("算术异常");
        }finally {
            int b = 10/2;
            System.out.println(b);
        }
    }
}

运行结果:

算术异常
5

Process finished with exit code 0

(2)抛出异常

使用throws抛出异常。

public class Test1 {
    @Test
    public void test() {
        try{
            throw new ArithmeticException();
        }catch (ArithmeticException arithmeticException){
            System.out.println("算术异常");
        }
    }
}

(3)throws和throw

throws抛出异常,需要调用者解决,throw抛出异常,在内部自己消化。

 4.3 自定义异常

 一是继承Exception,二是编写构造方法,传入异常信息,三是调用时通过throw向外抛出异常。

public class TestJava {
    public static void main(String[] args) {
        try{
            throw new MyException("有异常了");
        }catch (MyException myException){
            System.out.println(myException);
        }
    }
}
class MyException extends Exception{
    public MyException(String e){
        super(e);
    }
}

五、泛型注解

5.1 泛型

泛型提供了在编译阶段约束所能操作的数据类型,避免强制类型转换等出现异常。泛型包括泛型类、泛型方法、泛型接口和泛型通配符,例如:

public class aList<E> {
    List<Object> list = new ArrayList<Object>();
    void add(E e){
        list.add(e);
    }
    void print(){
        for (Object n:list) {
            System.out.println(n);
        }
    }
}

public class TestJava {
    public static void main(String[] args) {
        aList<String> list=new aList<String>();
        list.add("aaa");
        list.print();
        aList<Integer> list1=new aList<Integer>();
        list1.add(12);
        list1.print();
    }
}

一旦程序编译成class文件,class文件中泛型被擦除了没有泛型。

5.2 注解

(1)普通注解

@Override、@Deprecate、@SuppressWarnings

(2)元注解

元注解就是对注解的注解。

@Target、@Retention、@Documented、@Inherited

(3)自定义注解

public class TestJava {
    public static void main(String[] args) {
        try{
            TestJava testJava = new TestJava();
            Method method = testJava.getClass().getMethod("aaa");
            Annotation[] annotations = method.getAnnotations();
            for (Annotation an:annotations) {
                System.out.println(an);
            }
            Annotation an = method.getAnnotation(aaa.class);
            System.out.println(an);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Deprecated
    @aaa(name = "张三")
    public void aaa(){

    }
}
@Retention(RetentionPolicy.RUNTIME)
@interface aaa {
    public String name() default "methodname";
}

六、JDBC

也是一个超简单的JDBC示例,真实的肯定不这么写。

首先导入包依赖。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>

然后JDBC代码示例如下,查询语句。

public class Test1 {
    @Test
    public void Test() throws SQLException {
        Driver driver = new Driver();
        DriverManager.registerDriver(driver);
        String url="jdbc:mysql://XXX.XX.XXX.XXX:3306/demo1?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String user="root";
        String password="a13XXXXXXXXXXXXXXXXXXX";
        Connection connection =DriverManager.getConnection(url, user,password);
        Statement statement = connection.createStatement();
        String sql="select * from dept;";
        ResultSet resultSet =statement.executeQuery(sql);
        while(resultSet.next()){
            int deptno= resultSet.getInt("deptno");
            String dname = resultSet.getString("dname");
            String loc = resultSet.getString("loc");
            System.out.println(deptno+" "+dname+" "+loc);
        }
        statement.close();
        connection.close();
    }
}

总结

以上就是对Java部分非常基础的知识进行复习总结,进阶的复习总结详见Java复习总结笔记(下)。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值