java005学习记录

包装类

把基本数据类型封装成类的形式,基本数据类型不能付 null 值

每个对象都有一个内存地址,null 就表示内存地址不存在,null 是用来表示对象的,非对象的数据类型无法使用 null 来表示

基本数据类型不是对象,不能用 null 来表示

变量:1、基本数据类型 2、引用类型(对象)

基本数据类型不能用 null 表示会产生哪些问题?

数据都是存在数据库中的,如果该数据存在,查出数据之后使用变量来接收数据

select user_name from person where person_id = 91;
String name = "张寻婷";

select house_no from person where person_id = 91;
int no = 808;

当数据库中查询不到对应的数据时,直接返回 null,如果用基本数据类型来接收则程序报错

如何解决这个问题?

使用包装类来解决

基本数据类型对应的 Java 类,通过 Java 类来创建对象,通过包装类将基本数据类型转换成对象,则解决了不能用 null 接收的问题

基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
byte b = 1;
Byte byt = new Byte(b);
short s = 2;
Short shor = new Short(s);
int i = 3;
Integer integer = new Integer(i);
long l = 4;
Long lon = new Long(l);
float f = 5.5f;
Float flo = new Float(f);
double d = 6.6;
Double dou = new Double(d);
char cha = 'J';
Character charac = new Character(cha);
boolean bo = true;
Boolean bool = new Boolean(bo);

接口

提高代码的灵活性和复用性,接口也是多态一种具体表现形式,由抽象类衍生出来的

多态

继承 -》抽象类 -》接口

接口极度抽象的抽象类

抽象类中可以包含非抽象方法

一个抽象类中可以同时存在抽象方法和非抽象方法,极度抽象就是该方法中全部都是抽象方法,没有非抽象方法

抽象类不能直接 new,通过非抽象子类来创建

接口也不能直接 new,通过实现类来创建

接口和实现类的关系就是父子类的关系

public interface MyInterface {
    public void add(Integer num1,Integer num2);
    public void sub(Integer num1,Integer num2);
    public void mul(Integer num1,Integer num2);
    public void div(Integer num1,Integer num2);
    public void add1(Integer num1,Integer num2);
    public void sub2(Integer num1,Integer num2);
    public void mul2(Integer num1,Integer num2);
    public void div2(Integer num1,Integer num2);
}
public abstract class MyImplament implements MyInterface {

}

面向接口编程是一种常用的编程方式,可以有效地提高代码复用性,增强程序的扩展性和维护性

某工厂生产产品A,通过设备A来完成生产,用程序模拟该过程

package test;

public interface Equipment {
    public void work();
}
package test;

public class Factory {

    private Equipment equipment;

    public Equipment getEquipment() {
        return equipment;
    }

    public void setEquipment(Equipment equipment) {
        this.equipment = equipment;
    }

    public void work(){
        System.out.println("开始生产...");
        this.equipment.work();
    }
}
package test;

public class EquipmentA implements Equipment {
    public void work(){
        System.out.println("设备A运行,生产产品A");
    }
}
package test;

public class Test {
    public static void main(String[] args) {
        Equipment equipment = new EquipmentC();
        Factory factory = new Factory();
        factory.setEquipment(equipment);
        factory.work();
    }
}

异常

Java 中错误处理机制

1、编译时错误,语法错误,不需要考虑,代码会报错

2、运行时错误,语法正确,逻辑错误,正常通过编译,一运行就报错

Java 中会有很多运行时错误,JDK 总结出了一套类,叫做异常类

异常是一组类,JDK 提供专门用来描述各种各样错误的类,创建对应的异常对象来表示对应的错误

常用的异常类

  • ArithmeticException:表示数学异常
int num = 10/0;
  • ClassNotFoundException:表示类未定义异常
Class clazz = Class.forName("Account");
System.out.println(clazz);
  • IllegalArgumentException:表示参数格式错误异常
package test;

public class Account {
    public void test(Integer num){

    }
}
Class clazz = Class.forName("test.Account");
Account instance = (Account) clazz.newInstance();
Method test = clazz.getMethod("test", Integer.class);
test.invoke(instance, new String("abc"));
  • ArrayIndexOutOfBoundsException:表示数组下标越界异常
int[] array = {1,2,3};
System.out.println(array[3]);
  • NullPointerException:表示空指针异常
Integer num = null;
System.out.println(num.equals(1));
  • NoSuchMethodException:表示方法未定义异常
Class clazz = Class.forName("test.Account");
System.out.println(clazz.getMethod("test2", null));
  • NumberFormatException:表示数据类型转换异常
public class Account {
    public void test(String num){
        Integer integer = Integer.parseInt(num);
        System.out.println(integer);
    }
}
Class clazz = Class.forName("test.Account");
Method test = clazz.getMethod("test", String.class);
Account instance = (Account) clazz.newInstance();
test.invoke(instance, "abc");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值