JAVASE10

JAVASE10

常用类:
  1. String
  2. Math
  3. 包装类
  4. 枚举
  5. File
  6. 时间日期API

一、

String
  1. String : String类表示字符串。 Java程序中的所有字符串文字(例如"abc" )都实现为此类的实例。
    学习步骤:
    1.了解类的继承体系,类的作用,类的特点

    ​ 2.构造器

    ​ 3.方法

 public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "";
        //String() 初始化新创建的 String对象,使其表示空字符序列。
        String str1 = new String();

        System.out.println(str == str1); //false
        System.out.println(str.equals(str1)); //false

        //String(byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组构造新的 String 。
       // String(byte[] bytes, String charsetName) 构造一个新的String由指定用指定的字节的数组解码charset 。
        String s = "中国";
        byte[] arr = s.getBytes("GBK");
        System.out.println(arr.length);
        System.out.println(Arrays.toString(arr));

        //String str2 = new String(arr);
        String str2 = new String(arr,"gbk");
        System.out.println(str2);

        //String(byte[] bytes, int offset, int length)
        //String(byte[] bytes, int offset, int length, String charsetName)
        String str3 = new String(arr,2,2,"gbk");
        System.out.println(str3);

        //String(char[] value) 分配新的 String ,使其表示当前包含在字符数组参数中的字符序列。
        //String(char[] value, int offset, int count)
        char[] arr2 = {'a','b','c','d','e'};
        //String str4 = new String(arr2);
        String str4 = new String(arr2,1,3);
        System.out.println(str4);

        //String(String original)
        String str5 = new String("abc");  //创建 了2个 对象  一个new在堆中   一个"abc"在字符串常量池中
        System.out.println(str5);
    }
字符串String 类中常用方法:

注意:
一般结束索引位置的内容不包含

public static void main(String[] args) {
        String str = "wodejiejiezhenbang";
        String str1 = "Wodejiejiezhenbang";

        //char charAt(int index) 返回指定索引处的 char值。
        System.out.println("charAt:"+ str.charAt(5));

        //int codePointAt(int index) 返回指定索引处的字符(Unicode代码点)。
        //int codePointBefore(int index) 返回指定索引之前的字符(Unicode代码点)。

        //
        System.out.println(str.indexOf("a"));
        System.out.println("codePointAt:"+str.codePointAt(15));
        System.out.println("codePointAt:"+str.codePointBefore(15));

        //int compareTo(String anotherString) 按字典顺序比较两个字符串。
        System.out.println("compareTo:"+str.compareTo(str1));

        //int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略大小写差异。
        System.out.println("compareToIgnoreCase:"+str.compareToIgnoreCase(str1));

        //String concat(String str) 将指定的字符串连接到此字符串的末尾。
        System.out.println("concat:"+str.concat(str1));
        System.out.println(str);

        //boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时,才返回true。
        System.out.println("contains:"+str.contains("jiejie"));

        //boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。
        //boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头。
        System.out.println("startsWith:"+str.startsWith("wo"));
        System.out.println("endsWith:"+str.endsWith("wo"));

        //boolean equals(Object anObject) 将此字符串与指定的对象进行比较。
        //boolean equalsIgnoreCase(String anotherString) 将此 String与另一个 String比较,忽略了大小写。

        //byte[] getBytes()
        //byte[] getBytes(String charsetName) 使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。

        //void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将此字符串中的字符复制到目标字符数组中。
        char[] arr = new char[10];
        str.getChars(10,18,arr,1);
        System.out.println(Arrays.toString(arr));

        //int indexOf(String str) 返回指定子字符串第一次出现的字符串中的索引。
        //int indexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引。
        System.out.println("indexOf:"+str.indexOf("e"));
        System.out.println("indexOf:"+str.indexOf("e",4));
        System.out.println("indexOf:"+str.lastIndexOf("e"));

        //boolean isBlank() 如果字符串为空或仅包含 white space代码点,则返回 true ,否则 false 。
        //boolean isEmpty() 返回 true ,当且仅当, length()是 0 。
        System.out.println(" ".isEmpty());
        System.out.println(" ".isBlank());

        //int length() 返回此字符串的长度。
        System.out.println("length:"+str.length());

        //String repeat(int count) 返回一个字符串,其值为此字符串的串联重复 count次。
        System.out.println("repeat:"+str.repeat(3));

        //String replace(char oldChar, char newChar) 返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。
        //String replace(CharSequence target, CharSequence replacement)
        System.out.println("replace:"+str.replace('e','E'));
        System.out.println(str);

        //String[] split(String regex) 将此字符串拆分为给定 regular expression的匹配 项 。
        System.out.println(Arrays.toString(str.split("e")));

        //String trim()  去除前后空格  去除半角空格
        System.out.println(" hahaha ".trim());
        System.out.println(" hahaha ".trim());
        System.out.println(" hahaha ".strip());
        System.out.println(" hahaha ".strip());

        //11新增String类中的方法
        //String strip() 返回一个字符串,其值为此字符串,并删除了所有前导和尾随 white space 。
        //String stripLeading() 返回一个字符串,其值为此字符串,并删除了所有前导 white space 。
        //String stripTrailing() 返回一个字符串,其值为此字符串,并删除所有尾随 white space 。

        //String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
        //String substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。g
        System.out.println("substring:"+str.substring(2));
        System.out.println("substring:"+str.substring(2,5));

        //char[] toCharArray() 将此字符串转换为新的字符数组。
        System.out.println(Arrays.toString(str.toCharArray()));

        //String toLowerCase()  转小写
        //String toUpperCase()  转大写
        System.out.println(str.toUpperCase());

        //static String valueOf(boolean b)  系列静态方法,参数内容转为字符串
        boolean flag  = false;
        System.out.println(String.valueOf(flag));
    }
区别:
  1. String : 不可变的字符序列
  2. StringBuilder 可变的字符序列
    类提供与StringBuffer兼容的API,但不保证同步->线程不安全。效率相对较高
  3. StringBuffer 可变的字符序列
    同步的->线程安全->效率相对较低

执行效率:
StringBuilder>StringBuffer>String

public static void main(String[] args) {
        String s = "";
        //StringBuilder() 构造一个字符串构建器,其中不包含任何字符,初始容量为16个字符。
        StringBuilder sb = new StringBuilder();
        System.out.println(sb);
        System.out.println(sb.length());  //字符中真实字符的个数
        System.out.println(sb.capacity());  //缓冲区容量

        System.out.println(s.equals(sb));

        //StringBuilder(int capacity) 构造一个字符串构建器,其中没有字符,并且具有 capacity参数指定的初始容量。
        StringBuilder sb2 = new StringBuilder(10);
        System.out.println(sb2.length());
        System.out.println(sb2.capacity());

        //StringBuilder(CharSequence seq) 构造一个字符串构建器,其中包含与指定的 CharSequence相同的字符。
        //初始容量参数字符串长度+16
        StringBuilder sb3 = new StringBuilder("abc");
        System.out.println(sb3);
        System.out.println(sb3.length());  //3
        System.out.println(sb3.capacity()); //19

        //String 与 StringBuilder ,StringBuffer 相互转换
        //通过构造器
        String str2 = new String(sb3);
    }
StringBuilder 与 StringBuffer常用方法:
 public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();  //初始容量
        //StringBuilder append(boolean b) 将 boolean参数的字符串表示形式追加到序列中。
        sb.append("abc");
        sb.append(false);
        sb.append(123456);
        sb.append("中国");
        System.out.println(sb);
        System.out.println(sb.length());
        System.out.println(sb.capacity());

        sb.append(123);  //每次扩容: 原容量的2倍+2
        System.out.println(sb);
        System.out.println(sb.length());
        System.out.println(sb.capacity());


        //StringBuilder delete(int start, int end) 删除此序列的子字符串中的字符。  结束索引不包含
        sb.delete(3,8);
        System.out.println(sb);

        //StringBuilder deleteCharAt(int index) 按此顺序删除指定位置的 char 。
        sb.deleteCharAt(2);
        System.out.println(sb);

        //StringBuilder insert(int offset, Object obj) 将 Object参数的字符串表示形式插入此字符序列中。
        sb.insert(5,"哈哈");
        System.out.println(sb);

        //StringBuilder reverse() 导致此字符序列被序列的反向替换。
        sb.reverse();
        System.out.println(sb);
        System.out.println(sb.toString());

        String s = "haha"+"hehe";  //几个字符串对象
        //一个 编译的时候,常量会直接运算   String s = "hahahehe";

        String a = "哈哈";
        String b = "呵呵";
        String c = a+b;  //3个   "哈哈" "呵呵" "哈哈呵呵"
    }

二、

Math 数学相关类
 public static void main(String[] args) {
        //static double abs(double a) 返回 double值的绝对值。

        //static double ceil(double a) 返回大于或等于参数且等于数学整数的最小值(最接近负无穷大) double 。
        System.out.println(Math.ceil(-2));
        System.out.println(Math.ceil(-2.1));

        //static double floor(double a) 返回小于或等于参数且等于数学整数的最大值(最接近正无穷大) double 。
        System.out.println(Math.floor(2));
        System.out.println(Math.floor(2.9));

        //static long max(long a, long b) 返回两个 long值中较大的 long 。
        //static double min(double a, double b) 返回两个 double值中较小的 double 。
        System.out.println(Math.max(1.0,2.0));

        //static long round(double a) 返回与参数最接近的 long ,并将关系四舍五入为正无穷大。  小数点后一位
        System.out.println(Math.round(1.5));
        System.out.println(Math.round(1.45));

        //static double sqrt(double a) 返回 double值的正确舍入正平方根。
        System.out.println(Math.sqrt(9));
    }

三、

基本数据类型的包装类

​ 有了基本数据类型为什么还有包装类型?
​ 1.比如集合只能存储引用数据类型的数据,可以通过包装类型表示
​ 2.包装类提供了很多方法,可以灵活才做数据
​ 3.表示数据默认值的问题|对接数据库中的某些字段值,null与0的概念不同

基本包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
这里我们需要了解一个知识点

自动拆装箱:
自动装箱 : 基本->引用
自动拆箱 : 引用->基本

 public static void main(String[] args) {
        int i= 1;
        Integer in1 = i;  //Integer.valueOf(i);
        System.out.println(i);
        System.out.println(in1);

        int i2 = in1;  // in1.intValue()

        //test(1.0,2.0); //自动装箱

        Integer in = new Integer("123");
        System.out.println(in.intValue());

        Integer in2 = Integer.valueOf("321");
        System.out.println(in2);
    }

    static void test(Double d1,Double d2){
        System.out.println(d1+d2);  //自动拆箱
    }
我们深入了解这个的区别
 public static void main(String[] args) {
        int i1 = 127;
        int i2 = 127;

        Integer i3 = 127;
        Integer i4 = 127;

        Integer i5 = new Integer(127);
        Integer i6 = new Integer(127);

        //自动装箱: Integer.valueOf(128)
        Integer i7 = 128;
        Integer i8 = 128;



        System.out.println(i1 == i2); //true
        System.out.println(i3 == i4); //true
        System.out.println(i1 == i3); //true
        System.out.println(i5 == i6); //false
        System.out.println(i1 == i6); //true
        System.out.println(i3 == i6); //false
        System.out.println(i7 == i8); //false

        //Double.valueOf()
        Double d1 = 1.0;
        Double d2 = 1.0;
        System.out.println(d1 == d2);  //false

    }

总结:
1.Integer(是否new)与int比较,只要数据值相等,就相等,因为会先做自动拆箱,然后再比较
2.两个int基本数据类型只要值相等,肯定相等,因为值比较数据值
3.两个new比较,无论值是否相等都不相等,new在堆中就开辟空间,地址不同
4.如果一个Integer与一个new Integer比较,就算值相等也不相等,new就是新的地址
5.如果两个Integer类型比较,只要再-128,127返回之间就相等 ,因为返回都是缓冲区对象,如果超过范围,返回new Integer(数据),所以不相等

四、

这里我们插入关于Arrays数组的工具类(提供了一些操作数组的静态方法)

public class ArraysDemo01 {
    public static void main(String[] args) {
        //static int compare(int[] a, int[] b)
        int[] arr = {1,2,3,4,7,6,5};
        int[] arr3 = {1,2,3,4,7,6,5};
        int[] arr2 = {1,2,333,4,5,666};
        System.out.println(Arrays.compare(arr,arr2));
        System.out.println(Arrays.compare(arr,arr3));

        String[] s1 ={"ab"};
        String[] s2 ={"AB"};
        System.out.println(Arrays.compare(s1,s2));
        System.out.println(Arrays.compare(s2,s1));

        //static int[] copyOf(int[] original, int newLength)  截取  拷贝  填充,默认从索引为0的位置开始
        System.out.println(Arrays.toString(Arrays.copyOf(arr,4)));
        System.out.println(Arrays.toString(Arrays.copyOf(arr,14)));
        System.out.println(Arrays.toString(Arrays.copyOf(arr,7)));

        //static int[] copyOfRange(int[] original, int from, int to) 将指定数组的指定范围复制到新数组中。  结束索引不包含
        System.out.println(Arrays.toString(Arrays.copyOfRange(arr,4,6)));

        //static boolean equals(boolean[] a, boolean[] a2)
        //static boolean deepEquals(Object[] a1, Object[] a2) 深层比较是否相等,针对于多维数组
        System.out.println(Arrays.equals(arr,arr3));
        System.out.println(Arrays.equals(arr,arr3));

        Student[][] stus1 = {
                {
                    new Student("zhangsan",18),
                    new Student("lisi",19),
                },
                {
                        new Student("wangwu",17)
                }
        };
        Student[][] stus2 = {
                {
                        new Student("zhangsan",18),
                        new Student("lisi",19),
                },
                {
                        new Student("wangwu",17)
                }
        };
        //比较内层的内一个小数组的地址是否相等
        System.out.println(Arrays.equals(stus1,stus2));
        //比较二位数组中每一个具体的学生对象数据是否相等,如果Student中重写equals方法比较内容,否则比较每一个Student对象的地址
        System.out.println(Arrays.deepEquals(stus1,stus2));

        //toString
        System.out.println(Arrays.toString(stus1));
        //以字符串的形式打印多维数组中数据deepToString
        System.out.println(Arrays.deepToString(stus1));

        //fill 填充
        Arrays.fill(arr3,100);
        System.out.println(Arrays.toString(arr3));
        //指定区间使用定值填充,结束索引不包含
        Arrays.fill(arr3,1,3,1000);
        System.out.println(Arrays.toString(arr3));

        //static int mismatch(boolean[] a, boolean[] b)  查找两个数组中第一个不相等的数据的索引
        System.out.println(Arrays.mismatch(arr,arr2));

        //static void sort(int[] a)  默认升序排序
       /* Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));*/

        //static void sort(int[] a, int fromIndex, int toIndex) 按升序对数组的指定范围进行排序。
        Arrays.sort(arr,4,7);
        System.out.println(Arrays.toString(arr));

        //static int binarySearch(int[] a, int key) 使用二进制搜索算法在指定的int数组中搜索指定的值。   要求前提升序排序
        System.out.println(Arrays.binarySearch(arr,10));  //-8 -插入点-1
    }
}

class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @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);
    }

}

五、

枚举 enum:

​ 表示一种事物的所有可能|一种类型的所有情况

注意:
  1. 枚举类型不能自定义实例化,实例由程序员或枚举类型自己提供,因为枚举类的构造器默认私有化

  2. 枚举类型通过枚举字段提供当前类型的实例

  3. 一般通过枚举类型表示某种标识,而不会定义太多的功能和属性

  4. 通过enum关键字,定义的枚举类,都默认继承自java.lang.Enum

public class EnumDemo01 {
    public static void main(String[] args) {
        WeekDay w =WeekDay.TUES;
        //w.setName("星期一");
        System.out.println(w.getName());
        System.out.println(w);

        //switch  1.5支持枚举类型数据的额判定
        switch (w){
            case MON:
                System.out.println("周一");
                break;
            case SUN:
                System.out.println("周天");
                break;
        }

        System.out.println(w.name());  //实例名称
        System.out.println(w.ordinal());  //当前实例的索引 从0开始
        //获取 当前枚举类型的所有实例
        WeekDay[] days  = w.values();
        System.out.println(Arrays.toString(days));
    }
}

//枚举类型WeekDay : 用来表示一周中的每一个一天,WeekDay的实现,代表周一到周天,对象由枚举类型自己控制
enum  WeekDay{
    //字段1,字段2,....;
    MON(),TUES("星期二"),SUN;  //匹配空构造

    private String name;

    private WeekDay(){}

    private WeekDay(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "WeekDay{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Week{
    public static final Week MON = new Week();
    public static final Week TUES = new Week();
    public static final Week SUN = new Week();

    private Week(){}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值