一、Object


1、作用

万类之祖


2、常用api

        equals():比较对象是否相等,底层this==obj
        hashcode():返回对象的哈希值,底层public native int hashCode(),即hash(内存地址)
        toString():打印对象详情,底层getClass().getName() + "@" +         Integer.toHexString(hashCode())


3、hashcode和equals的关系

        equals相等则hashcode一定相等
        hashcode相等则equals不一定相等

        案例:
             public boolean equals(Object o) {
                if (this == o) return true;
                //比类型
                if (o == null || getClass() != o.getClass()) return false;

                Student2 student2 = (Student2) o;
                //比属性
                if (age != student2.age) return false;
                return name.equals(student2.name);
            }

            public int hashCode() {
                //根据属性生成
                int result = age;
                result = 31 * result + name.hashCode();
                return result;
            }


4、==和equals

                              ==               equals
        基本类型        值
        引用类型        内存地址          内容


二、包装类

1、基本类型对应的包装类

        byte                  Byte
        short                 Short
        int                   Integer
        long                  Long
        float                 Float
        double                Double
        char                  Character
        boolean               Boolean


2、Integer类

        构造方法:
            Integer(int value)
            Integer(String s) throws NumberFormatException
        方法:
            byteValue():转byte
            shortValue():转short
            intValue():转int
            longValue():转long
            floatValue():转float
            doubleValue():转double
            toString():转String
            equals():比较是否相等
            compareTo:比较是否相等
        静态方法:
            valueOf():把String转Integer
            parseInt():把String转int


3、自动拆箱和装箱

        是什么?
            自动装箱(int->Integer)和自动拆箱(Integer->int)是java语言的两个特性(即底层自动运行无需人工处理)
        底层原理:
            自动装箱:valueOf(int i)
            自动拆箱:intValue()
        自动装箱(包装类型)的使用场景:
            ①适合的场景
                public int method(int a){ //问题:此处int有默认值0
                }
            ②不适合的场景
                Integer sum = 0;
                for(int i=0; i<5000; i++){
                    sum = sum +i; //问题:此处会创建5000个Integer对象
                }


三、字符串类

1、String类
1.1、特点

            String是一个final类,不能被继承
            用final修饰的char[] value,不能被修改
            char[] value存储在方法区的常量池中(jvm优化实现),只有一份且被对象共享

1.2、构造方法

            String(String original)
            String(char value[])
            "":字面量

            字面量和构造方法方式的区别?
                字面量方式:
                    String s1 = "javaEE";
                    String s2 = "javaEE";
                    【栈】           【堆】            【常量池】
                     s1:地址A——>     String对象
                                       value:地址B——>  char[](存javaEE)
                     s2:地址A——>
                构造方法方式:
                     String s1 = new String("javaEE");
                     String s2 = new String("javaEE");
                     【栈】           【堆】            【常量池】
                      s1:地址A——>     String对象
                                        value:地址B——>  char[](存javaEE)
                      s2:地址C——>     新String对象
                                        value:地址B——>


 1.3、方法

            char charAt(int index):返回指定索引处的字符
            boolean equals(Object anObject):比较是否相等
            int indexOf(String str):返回指定字符的索引位置
            int length():字符串的长度
            String replaceAll(String regex, String replacement):替换
            char[] toCharArray():转char数组
            String trim():去掉空格
            String substring(int beginIndex):截取字符串
            String substring(int beginIndex, int endIndex):截取字符串,从beginIndex开始截,截endIndex个
            String[] split(String regex):分割字符串
            String toLowerCase():转小写
            String toUpperCase():转大写


2、StringBuffer类

2.1、是什么?

可变的String,线程安全


2.2、构造方法

 StringBuffer():初始长度是16
 StringBuffer(int capacity):初始长度是capacity
 StringBuffer(String str):初始长度是str.length()+16


2.3、方法

   append():追加字符串
   reverse():字符串反转
   toString():转String


3、StringBuilder类

3.1、String什么

            线程不安全的StringBuffer

 3.2、应用场景


                            应用场景
            String          少量不变
            StringBuffer    线程安全
            StringBuilder   线程不安全


四、日期类

1、System

          currentTimeMillis():返回1970年1月1日到现在的毫秒数(时间戳)

2、Date

        是什么?
            日期类(当作日期数据类型)
        构造方法:
            public Date():用当前时间创建date对象


3、Calendar

        是什么?
            日历类
        构造方法:
            该类是一个抽象类,但是可通过getInstance()返回该类的实例
        方法:
            public int get(int field):根据field返回日期的年/月/日/时/分/秒
            public abstract void add(int field, int amount):根据field穿越
            public final void set(int year,int month,int date):设置年月日


4、SimpleDateFormat

        是什么?
            日期格式化类
        构造方法:
            public SimpleDateFormat():使用默认的格式格式化日期
            public SimpleDateFormat(String pattern):根据指定格式格式化日期
        方法:
            public final String format(Date date):格式化日期
            public Date parse(String source):String转日期


五、Arrays类

      是什么?
        操作数组的工具类
      方法:
        toString(int[] a):打印数组
        sort(int[] a):排序
        copyOf(dataType[] srcArray,int length):拷贝

六、Random类

    是什么?
        用于生成随机数
    构造方法:
        public Random();
        public Random(long seed):种子值(seed)作为算法的起源数字,决定后续随机数序列的生成
    方法:
        nextInt(int bound):生成int类型随机数,注意最小是0,最大是bound-1
        nextLong():生成long类型随机数
        nextFloat():生成float类型随机数
        nextDouble():生成dobule类型随机数
        nextBoolean():生成boolean类型随机数

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐