day05 【常用API,引用类型小结】BigInteger类、BigDecimal类、Arrays类、包装类、String类、引用类型使用小结

day05 【常用API,引用类型小结】

反馈和复习
a.一个类,什么时候不能被继承(final),什么时候不能创建对象(抽象类,私有化构造方法),一个变量什么时候只能赋值一次(final)
b.老师少讲点,太多记不住(讲解的速度慢点) 
    
Object类
    toString: 默认返回包名.类名@地址值,重写之后返回对象的内容
    equals: 默认比较两个对象的地址值,重写之后比较对象的内容
Date类:
	public Date(); 当前时间
    public Date(long millis);距离基准时间millis毫秒后的那个时间
DateFormat类:
	public SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	格式化: public String format(Date d);
    解析: public Date parse(String time);  
Calendar类:
	获取:Calendar cc = Calendar.getInstance();
	方法:
		public int get(int field);
		public void add(int field,int value);
    	public void set(int field,int value);
Math类:
	Math的方法都是静态的
    public static double abs(double d);
	public static double ceil(double d);
	public static double floor(double d);
	public static long round(double d);
	public static double pow(double d,double d);
System类:
	System类的方法也是静态,并且不能创建对象(System的构造方法私有化了)
    System.exit(0); 退出JVM
    System.currentTimeMillis(); 获取当前时间的毫秒值
    System.arrayCopy(源数组,开始索引,目标数组,开始索引,复制的元素个数);     
今日内容
BigInteger 大整数计算 【理解】
BigDecimal 高精度计算 【理解】
    
Arrays 工具类(方法都是静态的)    【重点】
包装类(八种基本类型对应的引用类型)  【重点】
    
String类的常用方法(基础班学了一些,但是远远不够) 【重点】   
    
复习(,抽象类,接口,多态)【难点】    

第一章 BigInteger类【理解】

1.1 BigInteger的介绍
用于大整数计算(理论整数的位数是不受限制的)
1.2 BigInteger的构造方法
public BigInteger(String num); //创建一个大整数 
1.3 BigInteger的成员方法
BigInteger不能直接使用+-*/进行计算,而是用通过方法进行计算
    
public BigInteger add(BigInteger value); 求和 
public BigInteger subtract(BigInteger value); 求差
public BigInteger multiply(BigInteger value); 求积
public BigInteger divide(BigInteger value); 求商    
        
public class TestBigInteger {
    public static void main(String[] args) {
        //1.创建一个BigInteger
        BigInteger b1 = new BigInteger("99999999999999999");
        //2.计算
        //求和
        BigInteger add = b1.add(new BigInteger("1111111111111111"));
        System.out.println(add);
        //求差
        BigInteger subtract = b1.subtract(new BigInteger("22222222222222222"));
        System.out.println(subtract);
        //求积
        BigInteger multiply = b1.multiply(new BigInteger("33333333"));
        System.out.println(multiply);
        //求商,如果除不尽,那么不要小数部分
        b1 = new BigInteger("10");
        BigInteger divide = b1.divide(new BigInteger("3"));
        System.out.println(divide);
    }
}    

第二章 BigDecimal类【理解】

2.1 使用基本类型做浮点运算的精度是有问题的
public class TestBigDecimal01 {
    public static void main(String[] args) {
        System.out.println(0.09 + 0.01); // 0.1
        System.out.println(1.0 - 0.32);  // 0.68
        System.out.println(1.015 * 100); // 101.5
        System.out.println(1.301 / 100); // 0.01301
    }
}
发现: 计算机在计算小数时总是可能存在精确的情况
2.2 BigDecimal的介绍
做高精度的浮点数运算
2.3 BigDecimal的构造方法
public BigDecimal(double d);
public BigDecimal(String s); 【推荐】
2.4 BigDecimal的成员方法
BigDecimal不能直接使用+-*/进行计算,而是用通过方法进行计算
    
public BigDecimal add(BigDecimal value) 加法运算
public BigDecimal subtract(BigDecimal value) 减法运算
public BigDecimal multiply(BigDecimal value) 乘法运算
public BigDecimal divide(BigDecimal value) 除法运算(能除尽) 
public BigDecimal divide(BigDecimal value,int 保留位数,RoundingMode.HALP_UP) 除法运算(不能除尽)     
 
public class TestBigDecimal02 {
    public static void main(String[] args) {
        //1.创建一个BigDecimal
        BigDecimal b1 = new BigDecimal("9.99999999999");
        //2.计算
        //加法
        BigDecimal add = b1.add(new BigDecimal("1.11111111111111"));
        System.out.println(add);
        //减法
        BigDecimal subtract = b1.subtract(new BigDecimal("2.222222222222222"));
        System.out.println(subtract);
        //求积
        BigDecimal multiply = b1.multiply(new BigDecimal("3.33333"));
        System.out.println(multiply);
        //求商,注意,如果除不尽,会抛出异常
        b1 = new BigDecimal("1.1");
//        BigDecimal divide = b1.divide(new BigDecimal("0.3"));
//        System.out.println(divide);
        //如果除不尽,我们可以使用divide的一个重载方法
        //BigDecimal divide(BigDecimal divisor, int scale, int roundingMode):
        //                     除数            保留几位小数    取舍模式
        BigDecimal divide = b1.divide(new BigDecimal("0.3"), 5, RoundingMode.HALF_UP);
        System.out.println(divide);
    }
}    

如果是基本类型除法,能否进行指定小数位数的保留,可以的,使用System.out.printf(查看API)

第三章 Arrays类【重点】

3.1 Arrays类的介绍
Arrays是专门操作数组的工具类(方法都是静态的)

3.2 Arrays类的常用方法
public static void sort(int[] a); 对数组中的元素进行从小到大的排序
public static String toString(int[] a); 将一个数组的元素拼成一个大的字符串返回   
    
public class TestArrays {
    public static void main(String[] args) {
        //1.sort 排序方法
        int[] arr = {4,5,3,6,7,8,1,2,9};
        Arrays.sort(arr);
        //2.输出
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        //3.toString 将一个数组的所有元素拼成一个大的字符串返回
        String arrStr = Arrays.toString(arr);
        System.out.println(arrStr);
    }
}

扩展一下:
	sort方法对于数值类型数组排序时,按照数值的从小到大进行排序
    sort方法对于char类型数组排序时,按照字符的码值从小到大进行排序 
    sort方法对于String类型数组排序时,首先比较首字母的码值,
					如果相等再比较次字母的码值,依次类推,按照从小到到进行排序        

第四章 包装类【重点】

4.1 包装类的介绍
包装类就是基本类型对应的引用类型,全称叫基本数据类型的包装类(简称包装类)
    
八大基本类型每种都有其对应的包装类:
	byte		Byte
    short		Short
    "char		Character
    "int		Integer
    long   		Long 
    float		Float
    double		Double
    boolean     Boolean

4.2 Integer包装类介绍
Integer是int基本类型的包装类    

4.3 Integer类的构造方法和静态方法【了解】
构造方法:
	public Integer(int value);
	public Integer(String value);

静态方法:
	public static Integer valueOf(int value);
	public static Integer valueOf(String value);

public class TestInteger {
    public static void main(String[] args) {
        //1.创建Integer对象
        //构造方法
        Integer i1 = new Integer(10);
        System.out.println(i1);

        Integer i2 = new Integer("99");
        System.out.println(i2);
        //静态方法
        Integer i3 = Integer.valueOf(11);
        System.out.println(i3);

        Integer i4 = Integer.valueOf("88");
        System.out.println(i4);
    }
}

4.4 拆箱和装箱
装箱:  把基本类型 --转成--> 对应包装类【重点】
拆箱:	 包装类 --转回--> 对应的基本类型【重点】
    
比如:
	Integer i1 = new Integer(10);  一种装箱操作
    Integer i3 = Integer.valueOf(11); 一种装箱操作
        
    int value = i1.intValue();   一种拆箱操作 

4.4 自动拆箱和装箱【重点】
在JDK1.5,引入了自动拆装箱操作    
Integer i = 10; //底层Integer.valueOf(11)自动帮助我们进行装箱操作
int value = i; //底层 i.intValue()自动帮助我们进行拆箱操作

思考题:以下代码进行几次自动装箱和自动拆箱操作??
    Integer a = 10; // 装1
	a++; // a = a + 1  拆1  装1
	自动装箱几次? 2 
    自动拆箱几次? 1    

4.5 基本类型与字符串之间的转换【面试题】
  • 基本类型 转成 String
    int num = 10;
    a.直接+一个""
      String s = num + "";
    
    b.通过String的静态方法valueOf
      String s = String.valueOf(num);  
    
    
  • String 转成 基本类型
    String num = "100";
    
    第一种方式:
    a.先使用Integer的构造方法
        Integer i = new Integer(num);
    b.接着调用intValue方法拆箱
        int number = i.intValue();
    c.或者不调用intValue自动拆箱即可
        int number = i;   
    
    第二种方式:
    	直接调用包装类的parseXxx(String s)解析字符串的方法
        int number = Integer.parseInt(num); 
    
    
  • 字符串无法解析成基本类型时的异常
    public class TestInteger02 {
        public static void main(String[] args) {
            //1.字符串
            String num = "999";
            //2.使用Integer.parseInt方法解析
            int number = Integer.parseInt(num);
            System.out.println(number);
            //3.如果num中的值无法解析成int,会抛出异常
            num = "999我爱你"; 
            //NumberFormatException 数字格式化异常
            int result = Integer.parseInt(num);
            System.out.println(result);
        }
    }
    
    

第五章 String类常用方法【重点】

String的构造方法:
	直接赋值:
	String s = "java";
	构造方法:
	String s = new String("java");

	char[] chs = {'j','a','v','a'};
	String s = new String(chs);

	byte[] bs = {97,98,99,100};
	String s = new String(bs); // s字符串最终的内容是:"abcd"

//5.1 concat 拼接
	//方法原型:public String concat (String str)
    
//5.2 contains 判断是否包含某个小字符串
	//方法原型:public boolean contains (CharSequence s)  

//5.3 endsWith 是否以xx结尾
	//方法原型:public boolean endsWith(String suffix)

//5.4 startsWith 是否以xx开头
	//方法原型:public boolean startsWith(String prefix)
    
//5.5 indexOf 查找目标字符串在当前字符串中第一次出现的索引
	//方法原型:public int indexOf(String str)
    
//5.6 lastIndexOf 查找目标字符串在当前字符串中最后一次出现的索引
	//方法原型:public int lastIndexOf(String str)
    
//5.7 replace 将当前字符串中的目标字符串,替换为另外一个字符串
	//方法原型:public String replace(CharSequence s1,CharSequence s2)
    
//5.8 substring 截取子串
	//方法原型:public String substring(int beginIndex)
	//方法原型:public String substring(int beginIndex, int endIndex);(含头不含尾)
    
//5.9 toCharArray 将字符串转成char数组
	//方法原型:public char[] toCharArray()
    
//5.10 toLowerCase 转成纯小写
	//方法原型: public String toLowerCase()
    
//5.11 toUpperCase 转成纯大写
	//方法原型:public String toUpperCase()
    
//5.12 trim 取出字符串两端的空格(不去除空间的空格)
	//方法原型:public String trim()
    
//5.13 split 切割字符串,参数称为切割符
	//方法原型:public String[] split(String regex)

public class TestStringDemo {
    public static void main(String[] args) {
        String s = "HelloWorldHelloWorld";
        //1.concat
        String s1 = s.concat("Java");
        System.out.println(s1);
        //2.contains
        boolean b1 = s.contains("oWo");
        System.out.println(b1);
        //3.startsWith endsWith
        boolean b2 = s.startsWith("Hell");
        boolean b3 = s.endsWith("orld");
        System.out.println(b2);
        System.out.println(b3);
        //4.indexOf lastIndexOf
        int index1 = s.indexOf("oWo");
        System.out.println(index1);

        int index2 = s.lastIndexOf("oWo");
        System.out.println(index2);

        //思考:如果没有找到目标字符串,返回什么??
        int index3 = s.indexOf("PHP");
        System.out.println(index3);
        //5.replace
        String s3 = s.replace("World", "Java");
        System.out.println(s3);
        //6.substring
        //"HelloWorldHelloWorld"
        String s4 = s.substring(10); //从10索引截取到最后
        System.out.println(s4);

        String s5 = s.substring(10, 15); //从10索引截取到15之前(不包含15索引)
        System.out.println(s5);

        //7.toCharArray
        char[] chs = s.toCharArray();
        System.out.println(Arrays.toString(chs));

        //8.toLowerCase toUpperCase
        String s6 = s.toLowerCase();
        System.out.println(s6);

        String s7 = s.toUpperCase();
        System.out.println(s7);
        //9.trim
        String ss = "   Hello   World    ";
        System.out.println(ss);

        String s8 = ss.trim();
        System.out.println(s8);

        //10.split 切割
        String sss = "张胜男爱李四爱王五爱赵六爱前妻";
        String[] names = sss.split("爱");
        System.out.println(names[0]);
        System.out.println(names[1]);
        System.out.println(names[2]);
        System.out.println(names[3]);
        System.out.println(names[4]);
    }
}

第六章 引用类型使用小结【难点&&重点】

主要是(普通类,抽象类,接口,以及多态)复习
    
基本类型,可以作为方法的参数和返回
    public void show(int a){
    
	}
	public int method(){
        
    }
普通类,也可以作为方法的参数和返回
    public void show(Dog d){
    
	}
	public Dog method(){
        
    }

抽象类,也可以作为方法的参数和返回
	public void show(Animal a){
    	//Animal a = 任意一个子类对象
	}
	public Animal method(){
        
    }

接口,也可以作为方法的参数和返回
    public void show(Flyable a){
    	//Flyable a = 该接口的某个实现类对象
	}
	public Flyable method(){
        
    }

总结: 
	当基本类型作为方法的参数和返回值时,调用方法和返回数据时,返回该基本类型的值即可
    当引用类型作为方法的参数和返回值时,调用方法和返回数据时,返回该引用类型的对象/子类对象/实现类对象    

6.1 类名作为方法参数和返回值
6.1 普通类作为方法参数和返回值
public class TestPerson {
    public static void main(String[] args) {
        //调用方法
        killPerson(new Person(18,"前妻"));
        //调用方法
        Person pp = birthPerson();
        System.out.println(pp);

    }
    //定义方法,使用普通类Person,作为方法的参数
    public static void killPerson(Person p) {
        System.out.println("给老子去死~~");
        System.out.println(p);
    }

    //定义方法,使用普通类Person,作为方法的返回值
    public static Person birthPerson() {
        //返回一个Person对象
//        Person p = new Person(3,"哪吒");
//        return p;
        return new Person(3,"哪吒");
    }
}
结论: 当方法的参数或者返回值是普通类时,我们要传入或者返回的是该类的对象

6.2 抽象类作为方法参数和返回值
public class TestAnimal {
    public static void main(String[] args) {
        //调用方法
        show(new Dog());
        show(new Cat());
        System.out.println("=======");
        //调用方法
        Animal an = birthAnimal();
        an.eat();
        an.bark();
    }
    //定义一个方法,使用抽象类Animal作为方法的参数
    public static void show(Animal an){
        //Animal an = new Dog();
        //Animal an = new Cat();
        an.eat();
        an.bark();
    }
    //定义一个方法,使用抽象类Animal作为方法的返回值
    public static Animal birthAnimal(){
        //返回Animal的子类对象
        return new Dog();
//        return new Cat();
    }
}
结论:当方法的参数或者返回值是抽象类时,我们要传入或者返回的是该抽象类的子类对象

6.3 接口作为方法参数和返回值
public class TestFlyable {
    public static void main(String[] args) {
        //调用
        showFly(new Bird());
        showFly(new Plane());
        System.out.println("=====");
        //调用
        Flyable ab = getFly();
        ab.fly();
    }
    //定义方法,使用接口Flyable作为方法参数
    public static void showFly(Flyable fa){
        //Flyable fa = new Bird();
        //Flyable fa = new Plane();
        fa.fly();
    }
    //定义方法,使用接口Flyable作为方法返回值
    public static Flyable getFly() {
        //返回Flyable的实现类对象
        return new Bird();
//        return new Plane();
    }
}
结论:当方法的参数或者返回值是接口时,我们要传入或者返回的是该接口的实现类对象 

基本类型可以作为类的成员变量的,其实引用类型也可以作为类的成员变量
public class Student{
    int age;//学生有年龄
    String name;//学生有姓名
    Dog dog;//学生有只狗
    Animal an;//学生有只动物
    Flyable fa;//学生有个会飞的东西
}

6.4 普通类作为成员变量
/**
 * 身份证类
 */
public class IDCard {
    String id; //号码
    String years; //有效年限
    String author;//签发机关
    //...
    public IDCard() {
        
    }
    public IDCard(String id, String years, String author) {
        this.id = id;
        this.years = years;
        this.author = author;
    }
    @Override
    public String toString() {
        return "IDCard{" +
                "id='" + id + '\'' +
                ", years='" + years + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
//人类
public class Person {
    //人拥有一个年龄
    int age;
    //人拥有一张身份证
    IDCard card;
    public Person() {
        
    }

    public Person(int age, IDCard card) {
        this.age = age;
        this.card = card;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", card=" + card +
                '}';
    }
}

/**
 * 测试类
 */
public class TestPerson {
    public static void main(String[] args) {
        //1.创建一张身份证
        IDCard cc = new IDCard("10086","20","中国移动");

        //2.创建一个人
        Person pp = new Person(18,cc);

        //3.打印
        System.out.println(pp);
    }
}
结论: 当普通类作为成员变量,给该成员变量赋值时,赋值普通类的对象

6.5 抽象类作为成员变量
public class Student {
    int age;
    String name;
    //还有一只动物
    Animal an;
    //.....
}

public class TestStudent {
    public static void main(String[] args) {
        //1.创建一个学生
        Student ss = new Student();
        //2.给ss的成员变量赋值
        ss.age = 18;
        ss.name = "王五";
//        ss.an = new Dog();
        ss.an = new Cat();
    }
}
总结: 当抽象类作为成员变量时,给该成员变量赋值,赋该抽象类的子类对象

6.6 接口作为成员变量
public class Student {
    private int age;
    private String name;
    //拥有一个会飞的东西
    private Flyable fa;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Flyable getFa() {
        return fa;
    }

    public void setFa(Flyable fa) {
        this.fa = fa;
    }
}

public class TestStudent {
    public static void main(String[] args) {
        //1.创建一个Student对象
        Student ss = new Student();
        //2.给age赋值
        ss.setAge(10);
        //3.给name赋值
        ss.setName("张三");
        //4.给fa赋值
//        ss.setFa(new Bird());
        ss.setFa(new Plane());
    }
}
结论: 当接口作为成员变量时,给该成员变量赋值,赋该接口的实现类对象

总结
1.BigInteger【理解】
2.BigDecimal 【理解】   
    add subtract multiply divide
3.Arrays工具类【重点】
    public static void sort(int[] arr);默认对数组进行升序排序
    public static String toString(int[] arr);将数组的所有元素拼接成一个大的字符串返回
4.包装类【重点】
    a。八种基本类型对应的引用类型
    b。装箱和拆箱(JDK5之后,自动拆装箱)
    	Integer i = 10;
		int j = i;
5.String 【重点】
    掌握构造方法+常用的13个成员方法(熟练使用)
6.引用类型的使用总结【重点+练习+思考】
    引用类型作为方法的参数和返回值
    引用类型作为成员变量
    
    如果引用类型是普通类,那么需要用到该普通类的对象
    如果引用类型是抽象类,那么需要用到该抽象类的子类对象
    如果引用类型是接口,那么需要用到该接口的实现类对象    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值