Java-08-常用类

Java常用类

1 Object类

是所有数据类型的超级父类

Object类常用方法

  • toString()
/**
 * @Author Void
 * @Description //TODO 测试 toString()
 * @Date 19:13 2021/3/10
 * Object 是 所有类的父类
 **/
public class TestObject {

    public static void main(String[] args) {
        /*
        *toString方法的默认实现:return this.getClass().getName() + "@" + Integer.toHexString(hashCode());
        *作用:通过调用这个方法将一个java对象转换成字符串表示形式
        *建议所有子类都重写此方法
        *输出引用的时候,都会默认调用其toString方法
        * */

        Object o = new Object();
        System.out.println(o.toString());//java.lang.Object@1b6d3586
    }
}
  • equals()

public class TestEquals {
    public static void main(String[] args) {
        /*
        * Object类的equals方法
        * 默认实现:return (this == obj); 这里默认使用了"=="
        * 作用:通过equals判断两个对象是否相等
        * 判断两个基本数据类型是否相等,"==" 够了
        * 但判断两个对象相等 "==" 是不行的,他判断的是对象的内存地址如:0x1234 0x4566 无比较意义
        * 当要判断对象的内容是否相等时,需要重写equals方法(具体值的判断)
        * */
        int a = 100;
        int c = 100;
        System.out.println(a==c);//true

        Object o = new Object();
        Object b = new Object();
        System.out.println(o==b);//false
    }
}

2.String类

字符串的不可变性

字符串被创建后,其内容将被Jvm存入常量池,其中的的对象内容是不能更改的,

/* 
hello 存在固定位置
引用 通过指针指向这里
所以s1和s2指向的是同一个地址
*/
String s1 = "hello";
String s2 = "hello";
s1 = "world";//这里并没有将hello修改为world,而是在常量池中创建了新的"world",让s1指向这里

在String前提下,不要过于频繁的使用字符串拼接

因为这样会在常量池大量创建字符串对象,推荐使用StringBuffer 或者 StringBuilder

常用方法

  • char charAt(int index);获取 index 位置的字符
  • boolean contains(CharSequence s);判断字符串中是否包含某个字符串
  • boolean startsWith(String s);判断是否是以某个字符串开始
  • boolean endsWith(String endStr);判断是否是以某个字符串结尾
  • boolean equalsIgnoreCase(String anotherString);忽略大小写比较两个字符串是否相等
  • byte[] getBytes();转换成 byte 数组
  • int indexOf(String str);取得指定字符在字符串的位置
  • int length();获取字符串的长度
  • String replaceAll(String s1,String s2);替换字符串中的内容
  • void toUpperCase();转换为大写
  • void toLowerCase();转换为小写
  • String trim();去除首尾空格
  • String valueOf(Object obj);将其他类型转换为字符串类型

3.StringBuffer 和 StringBuilder

StringBuffer 是一个字符串缓冲区,如果需要频繁的对字符串进行拼接时,建议使用 StringBuffer。

StringBuffer 的底层是 char 数组,如果没有明确设定,则系统会默认创建一个长度为 16 的 char 类型数组,在使用时如果数组容量不够了,则会通过数组的拷贝对数组进行扩容,所以在使用 StringBuffer 时最好预测并手动初始化长度,这样能够减少数组的拷贝,从而提高效率。
//无参构造方法
StringBuffer s1 = new StringBuffer();
//构造方法2:手动指定长度
StringBuffer s2 = new StringBuffer(100);
//构造方法3 直接传入字符串
StringBuffer s3 = new StringBuffer("hello world");

常用方法

  • StringBuffer append(String s) 将指定的字符串追加到此字符序列。
  • StringBuffer reverse() 将此字符序列用其反转形式取代。
  • delete(int start, int end) 移除此start-end 子字符串中的字符。
  • insert(int offset, int i) 将 int 参数的字符串表示形式插入此序列中。

二者区别

StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。

4.包装类

Java里面有8个基本数据类型,每个数据类型都有相应的类,这些类叫做包装类。

作用:有了包装类,可以更方便的对数据进行操作了,例如基本数字类型和字符串之间的转换。

基本类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

用法

package com.commonClass.packagingClass;

public class TestPackaging {
    public static void main(String[] args) {
        //输出int 类型能表示的最大值
        System.out.println(Integer.MAX_VALUE);//2147483647
        //利用Integer的构造方法赋值
        Integer i1 = new Integer(100);
        System.out.println("i1的值: "+i1);//100

        Integer i2 = new Integer("100");//这里的字符串只能为数字
        System.out.println("i2的值: "+i2);//100

        int i3 = i2.intValue();//Integer转int类型
        System.out.println("i3的值: "+i3);//100

        Integer i4 = Integer.valueOf(8);//int转Integer
        System.out.println("i4的值: "+i4);//8

        int i5 = Integer.parseInt("1");//利用Integer将数字字符串转int
        System.out.println("i5的值: "+i5);

        //十进制转二进制
        String s1 = Integer.toBinaryString(10);
        System.out.println("s1的值: "+s1);

        //十进制转8进制
        String s2 = Integer.toOctalString(20);
        System.out.println("s2的值: "+s2);

        //十进制转16
        String s3 = Integer.toHexString(10);
        System.out.println("s3的值: "+s3);

    }
}
/*
2147483647
i1的值: 100
i2的值: 100
i3的值: 100
i4的值: 8
i5的值: 1
s1的值: 1010
s2的值: 24
s3的值: a
*/
  • int->Integer:Integer i1 = Integer.valueOf(10);
  • Integer->int:int i2 = i1.intValue();
  • String–>Integer:Integer i3 = Integer.valueOf("10");
  • Integer–>String:String s1 = i3.toString();
  • String–>int:int i4 = Integer.parseInt("123");
  • int–>String:String s2 = 10 + "";

自动装箱和自动拆箱

//自动装箱:将基本类型无缝转换为包装类
Integer a = 10;
//自动拆箱:把包装类无缝转换为基本类型
Integer b = 1;
int c = b;

Tips

判断两个Integer类型是否相等时,要使用equals方法,不能使用"=="。
Integer num1 =new Integer(100);
Integer num2 =new Integer(100);
System.out.println(num1==num2);//false 内存地址
System.out.println(num1.equals(num2));//true
/*
这是因为Integer重写了equals方法(变成了值判读)
public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
*/

5.Date类型

在开发过程中,不难想象,时间的使用是频繁且广泛的,所以如何获取时间是必须的

获取时间戳

System.currentTimeMillis()会返回long类型值,这段代码会获取从 1970年1月1日 00时00分00秒 000毫秒 到当前的毫秒数

拼接字符串的时间比较案例

package com.commonClass.DateClass;

public class TestDate {

    public static void main(String[] args) {
        String s = "";
        StringBuffer sbf = new StringBuffer();
        //获取拼接前的毫秒数,也就是当前时间
        long start = System.currentTimeMillis();

        for (int i =0;i<=1000;i++){
           s += i;
//            sbf.append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        /*
        * s------5
        * sbf----1
        * */

    }String
}

拼接不要用String

日期的标准格式写法

import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
    public static void main(String[] args) {
        //输出当前日期
        Date date1 = new Date();// java.util.Date;
        System.out.println(date1);//Wed Mar 10 22:44:27 CST 2021

        Date date2 = new Date(0L);//参数填写Lang类型,0L
        System.out.println(date2);//Thu Jan 01 08:00:00 CST 1970

        // SimpleDateFormat关键字可以让日期打印出想要的格式
        //.format() 将某日期按格式格式化
        //y:年 M:月 d:日  h:时 m:分 s:秒 S:毫秒
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");
        String today = sdf.format(date1);
        System.out.println(today);
    }
}

String类型转Date类型

注意格式的一致
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDate {

    public static void main(String[] args) {
        String birthday = "2000年01月01日 00:00:00";
        //创建相应格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        //将字符转Date类
        Date date = new Date();
        try {
            date = sdf.parse(birthday);//通过格式解析 字符 提取年月日
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date);
    }

Calendar类----另一种方法

操作日历方面提供了一些方法
import java.util.Calendar;


public class TestDate {

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();//获取今天的日期对象
        System.out.println(calendar.get(Calendar.YEAR));//
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//周日为第一天  4:周三
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//
    }
}
Calendar.YEAR年份
Calendar.MONTH月份
Calendar.DATE日期
Calendar.DAY_OF_MONTH一个月的第几天
Calendar.HOUR12小时制的小时
Calendar.HOUR_OF_DAY24小时制的小时
Calendar.MINUTE分钟
Calendar.SECOND
Calendar.DAY_OF_WEEK星期(几-1)

6.Math类

封装了基本数学运算的方法,比如开方,平方等

使用


public class TestMath {
    public static void main(String[] args) {
        //圆周率
        System.out.println(Math.PI);//3.141592653589793

        //绝对值
        System.out.println(Math.abs(-4));//4

        //ceil天花板,向上取整 double
        System.out.println(Math.ceil(3.14));//4.0

        //floor 地板,向下取整 double
        System.out.println(Math.floor(3.14));//3.0

        //两数取大者
        System.out.println(Math.max(10, 100));//100

        //平方 2^4
        System.out.println(Math.pow(2, 4));//16.0
        //开方
        System.out.println(Math.sqrt(100));//10.0

        //随机小数 [0.0,1.0)
        System.out.println(Math.random());
        //指定区间? 比如[0,100) 且为整数 +floor
        System.out.println(Math.floor(Math.random() * 100));

        //四舍五入
        System.out.println(Math.round(3.4));//3
        System.out.println(Math.round(3.6));//4
    }
}

7.Random类

生成随机数不仅在Math类中有,还有一个类专门用于生成随机数–Random类

Random类位于java.util包下

用法

import java.util.Random;

public class TestRandom {
    public static void main(String[] args) {
        //实例化
        Random random = new Random();

        //通过nextInt 获取1-1000的随机整数
        int i = random.nextInt(1001);
        System.out.println(i);
    }
}

8.枚举(enum)

枚举类在生活中挺常见,比如月份只有1-12,季节有春,夏,秋,冬,将这些固定值放在一个集合中,使得取值范围不会超过这些值
public class TestEnum {
    //使用枚举存放四季
    public enum Season{
        Spring,Summer,Autumn,Winter
    }
    public static void main(String[] args) {
        //调用枚举
        System.out.println(Season.Summer);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值