JavaSE进阶day04

1-Object类

1.1-介绍

  • 所有的类,都直接或者间接的继承Object类 (祖宗类)

  • Object类的方法是一切子类都可以直接使用的,所以我们要学习Object类的方法。

toString方法

package com.lyl.tostring;
​
public class ToStringDemo {
    /*
            Object类中的tostring方法:
​
            public String tostring()返回该对象的字符串表示
​
            -打印语句,在打印对象名的时候,会自动调用该对的toString方法,
​
           public static String valueOf(Object obj) {
                  return (obj == null) ? "null" : obj.toString();
           }
​
            -0bject类中的tostring方法,默认逻辑
​
            public String tostring(){
                 return getclass().getName()+"@"+Integer.toHexstring(hashcode();
           }
​
注意:打印对象名,如果没有看到地址值,说明这个类绝对重写过toString方法.
     */
    public static void main(String[] args) {
        
        Student student = new Student();
        System.out.println(student);
        System.out.println(student.toString());
​
    }
}
​
class Student{
​
}
​

好处:

equals方法

学生类Student

package com.lyl.domain;
​
public class Student {
    private String name;
    private int age;
​
    //重写equals方法
    @Override
    public boolean equals(Object obj) {
        //判断是否为学生类对象
        if (obj instanceof Student){
            //向下转型
            Student student = (Student) obj;
            //this:stu1
            //obj:stu2
            return this.age  == student.age && this.name.equals(student.name);
        }else {
            return false;
        }
    }
}

测试类Test

package com.lyl.object.equals;
​
import com.lyl.domain.Student;
​
public class EqualsDemo {
    /*
        Object类中的equals方法:
            public boolean equals(Object obj) {
                   return (this == obj);
            }
     */
    public static void main(String[] args) {
        Student stu1 = new Student("张三",23);
        Student stu2 = new Student("张三",23);
        System.out.println(stu1.equals(stu2));
    }
}
输出结果:true

IDEA自动重写equals方法:

 @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {return false;}
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

2-Math类

  • 包含执行基本数字运算的方法

Math类的常用方法

  • 注:不用死记,学会查API即可

3-System类

  • System的功能是静态的,都是直接用类名调用即可

 时间原点:1970年1月1日 00:00:00

void aaraycopy(Object src, int srcPos, Object dest, int destPos, int length)数组拷贝

  • 参数1:数据

  • 参数2:拷贝的起始索引

  • 参数3:数据目的

  • 参数4:拷贝的目标索引

  • 参数5:拷贝的个数

4-BigDecimal类

  • 用于解决小数运算中,出现的不精确问题

学习路径:

  • 1.将需要运算的数据,封装到BigDecimal对象

  • 2.使用BigDecimal对象,完成数据运算,保证精确

BigDecimal的构造方法

1.public BigDecimal(double val):不推荐!

2.public BigDecimal(String val):推荐

package com.lyl.bigdecimal;
​
import java.math.BigDecimal;
​
public class BigdecimalDemo {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal(0.1);
        BigDecimal bd2 = new BigDecimal(0.5);
        System.out.println(bd1.add(bd2));
    }
}

输出结果:0.6000000000000000055511151231257827021181583404541015625

package com.lyl.bigdecimal;
​
import java.math.BigDecimal;
​
public class BigdecimalDemo {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("0.1");
        BigDecimal bd2 = new BigDecimal("0.5");
        System.out.println(bd1.add(bd2));
    }
}

输出结果:

0.6

package com.lyl.bigdecimal;
​
import java.math.BigDecimal;
​
public class BigdecimalDemo {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("0.1");
        BigDecimal bd2 = new BigDecimal("0.5");
        System.out.println(bd1.add(bd2));
        System.out.println(bd1.divide(bd2));
        System.out.println(bd1.multiply(bd2));
        BigDecimal result = bd1.subtract(bd2);
        System.out.println(result);
    }
}

输出结果:

0.6 0.2 0.05 -0.4

  • 注意:BigDecimal方法返回的是一个对象,不能直接进行运算操作,如果想继续进行操作,则要用.doubleValue()方法转换为double类型

divide除法细节:

package com.lyl.bigdecimal;
​
import java.math.BigDecimal;
import java.math.RoundingMode;
​
public class BigdecimalDemo {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("10.0");
        BigDecimal bd2 = new BigDecimal("3.0");
​
        System.out.println(bd1.divide(bd2, 1, RoundingMode.HALF_UP));
        System.out.println(bd1.divide(bd2, 2, RoundingMode.DOWN));
        System.out.println(bd1.divide(bd2, 3, RoundingMode.UP));
    }
}

输出结果:

3.3 3.33 3.334

5-包装类

  • 将基本数据类型,包装成类(变成引用数据类型)

Integer类

1.构造方法

手动的将基本数据类型int,包装为引用数据类型Integer

package com.lyl.IntegerDemo;
​
public class IntegerDemo1 {
​
    public static void main(String[] args) {
        Integer i = Integer.valueOf(10);
        Integer.toBinaryString(10); //2进制
        Integer.toHexString(10); //16进制
        Integer.toOctalString(10); //8进制
    }
}

jdk5之前:

  • 手动装箱:手动的将基本数据类型int,包装为引用数据类型的Integer

     Integer i = Integer.valueOf(10);

    jdk5之后:

  • 自动装箱:基本数据类型,可以直接赋值给引用数据类型的包装类

    Integer i = 110;

jdk5之前:

  • 手动拆箱:手动的调用Integer类中的intValue方法,将其转换为基本数据类型

Integer i = 10;
        int num = i.intValue();
        System.out.println(num + 100);

jdk5之后:

  • 自动拆箱:引用数据类型包装类,可以和基本数据类型直接做运算了

Integer i = 10;
        System.out.println(i + 100);

结论:引用数据类型的包装类,和基本数据类型,可以直接做运算,不需要手动转换

细节:

装箱的范围在-128~127之间,==号比较出来的结果就是true,反之就是false.

如果装箱的数据,不在byte取值范围内,会重新new一个对象再返回

如果在byte的取值范围内,会从底层数组中,取出一个己有的对象进行返回

2.静态方法

 6-Arrays工具类

  • 数组操作工具类,专门用于操作数组元素

 binarySearch:只能操作排好序的数组,而且只能是升序

7-冒泡排序

  • 相邻的两个数进行比较,如果第一个比第二个大,就交换他们两个

package com.lyl.arrays;
​
import java.util.Arrays;
​
public class BubbleSort {
    //冒泡排序
    public static void main(String[] args) {
        int arr[] = {55, 44, 33, 22, 11};
        //外循环控制轮数
        for (int i = 0; i < arr.length; i++) {
            //内循环:
            // -1:为了避免索引越界
            //-i:为了提高效率
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]){
                    int temp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

8-选择排序

  • 从0索引开始,拿着每一个索引上的元素跟后面的元素依次比较

package com.lyl.arrays;

import java.util.Arrays;

public class SelectSort {
    //选择排序
    public static void main(String[] args) {
        int arr[] = {55, 44, 33, 22, 11};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

9-二分查找

package com.lyl.arrays;
​
public class BinarySearch {
    //二分查找
    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55, 66, 77, 88, 99};
        int num = 33;
        int index = myBinarySearch(arr, num);
        System.out.println(index);
    }
​
    private static int myBinarySearch(int[] arr, int num) {
        //1.定义两个变量,记录最大索引和最小索引
        int min = 0;
        int max = arr.length - 1;
        //2.编写折半的条件
        while (min <= max) {
            //3.计算中间值
            int mid = (max + min) / 2;
            //4.比对
            if (num > arr[mid]) {
                min = mid + 1;
            } else if (num < arr[mid]) {
                max = mid - 1;
            } else {
                //5.找到了,返回正确索引
                return mid;
            }
        }
        //6.代码执行到这里,说明没找到
        return -1;
    }
}

10-正则表达式

练习

1,QQ号正则

  • 不能以0开头 5~12位 全部都是数字

String qqRegex = "[1-9]\\d{4,11}";
        System.out.println("5162316".matches(qqRegex));

2,手机号正则

  • 必须是1开头 第二位:3456789 全都是数字,必须是11位

String phoneNumberRegex = "[1][3-9]\\d{9}";
System.out.println("13512341234".matches(phoneNumberRegex));

3,邮箱正则

String emailRegex = "\\w+[@][\\w[^_]]+(\\.+[a-z]{2,3})+";
System.out.println("zhangsand@itcast.qq.com".matches(emailRegex));

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值