常用类:包装类、String类和日期类

一、包装类

概念

---基本数据类型不是对象,所以就有基本数据类型对应的类,称为包装类
基本数据类型包装类
byteByte
booleanBoolean
shortShort
charCharacter
intInteger
longLong
floatFloat
doubleDouble
重点:包装类可以实现字符串和基本数据类型之间的转换
		int i = Integer.parseInt("123");

自动装箱和拆箱

---JDK1.5以后引入自动装箱和拆箱
	1.自动装箱:valueOf()
	例如:Integer i = Integer.valueOf(666);
	可以写成: Integer i = 666;
	2.自动拆箱:xxxValue()
	例如:int j = i.intValue();
	可以写成: int j = i;
注:1.Integer类提供了范围为-128~127的静态数组缓存cache,数据在范围之外的都使用new Integer创建新对象
	2.对象比较用equals方法
	3.作为成员变量int为0,Integer等为null 

二、字符串类

引入

---1.String、StringBuffer、StringBuilder是三个字符串相关类。
---2.String不可变字符序列,StringBuffer、StringBuilder可变可变字符序列
---3.StringBuffer线程安全,速度慢;StringBuilder线程不安全,速度快

方法

//String类方法太多,详细的可以去API中查看
public class TestString {
    public static void main(String[] args) {
        //1.创建String对象
        String str = "abCsdfhfgDef"; 
        //2方法
        System.out.println(str.length());//长度
        System.out.println(str.isEmpty());//判断是否为空
        System.out.println(str.startsWith("a"));//判断以*开头
        System.out.println(str.endsWith("f"));//判断以*结尾
        System.out.println(str.toUpperCase());//转大写
        System.out.println(str.toLowerCase());//转小写
        System.out.println(str.charAt(3));//取索引为3的字符
        System.out.println(str.substring(5,7)); //截取[5,7)
        System.out.println(str.substring(7));//截取7以后的字符
        System.out.println(str.indexOf("C"));//从前往后的索引
        System.out.println(str.lastIndexOf("e"));//从后往前的索引
        System.out.println(str.trim());//去除前面和后面的空格
    }
}

String类底层简述

---1.String类底层是final修饰的不可变数组:
		private final char value[];
---2.JDK9以后char数组变为byte数组,节省空间

StringBuilder类方法

public class TestStringBuilder {
    public static void main(String[] args) {
        StringBuffer builder = new StringBuffer("123");
        builder.append("321");//在末尾追加子串
        builder.insert(3,"fgsd");//索引为3处插入
        builder.setCharAt(3,'g');//修改对应位置的字符
        builder.replace(3,6,"asd");//替换对应位置的字符
        builder.deleteCharAt(3);//删除指定位置的字符
        builder.reverse();//倒叙
    }
}

StringBuildr类底层简述

---1.String类底层是可变数组:char[] value;
	默认长度capacity长度为16
	length()方法返回字符数组元素的真实个数
	capacity()方法返回的是底层数组长度
	扩容机制:默认是原来的长度2倍+2
---2.JDK9以后char数组变为byte数组,节省空间

三、日期类

Date类部分方法

public class TestDate {
    public static void main(String[] args) {        
        Date now = new Date();//获取当前的时间
        System.out.println(now.toString());  //输出当前的时间
        System.out.println(now.toLocaleString());//输出当前的时间
        System.out.println(now.getYear()); //年 
        System.out.println(now.getMonth());//月
        System.out.println(now.getDate());//日
        System.out.println(now.getHours());//时
        System.out.println(now.getMinutes());//分
        System.out.println(now.getSeconds());//秒
    }
}

DateFormat类方法

public class TestDateFormat {
    public static void main(String[] args) {
         Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str2 = dateFormat.format(date);
        System.out.println(str2);
    }
}

Calendar类

public class TestCalendar {
    public static void main(String[] args) {
        //创建Calendar对象
        Calendar cal = new GregorianCalendar();
        //输出日期中的各个部分
        System.out.println(cal.get(Calendar.YEAR));
        System.out.println(cal.get(Calendar.MONTH));
        System.out.println(cal.get(Calendar.DATE));
        System.out.println(cal.get(Calendar.HOUR));
        System.out.println(cal.get(Calendar.MINUTE));
        System.out.println(cal.get(Calendar.SECOND));
        System.out.println(cal.get(Calendar.MONTH));
        System.out.println(cal.get(Calendar.DAY_OF_WEEK));
        //输出Calendar对象
        System.out.println(cal);
        Date date = new Date();
        //Date ---->Calendar
        cal.setTime(date);
        //Calendar---->Date
        Date now2 = cal.getTime();
    }
}

JDK8的新的日期类

public class TestNewDate {
    public static void main(String[] args) {
        //Instant类时间
        Instant instant = Instant.now();
        //LocalDate获得日期
        LocalDate localDate = LocalDate.now();
		//LocalTime时刻
		LocalTime localTime = LocalTime.now();
		 //LocalDateTime具体时间
        LocalDateTime localDateTime = LocalDateTime.now();
        //DateTimeFormatter
        DateTimeFormatter dftr = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
    }
}

Math类

public class TestMath {
    public static void main(String[] args) {
        System.out.println(Math.PI);//圆周率PI
        System.out.println(Math.round(3.14));//大约,取整四舍五入
        System.out.println(Math.ceil(3.14));//向上取整4
        System.out.println(Math.floor(3.14));//向下取整3
        System.out.println(Math.sqrt(64));//开平方
        System.out.println(Math.pow(2,5));//幂
        System.out.println(Math.abs(-5));//绝对值
        System.out.println(Math.max(30,40));//最大值
        System.out.println(Math.min(30,40));//最小值
        System.out.println(Math.random());//随机[0.0,1.0)
    }
}

Random类

public class TestRandom {
    public static void main(String[] args) {
        Random rand = new Random();
        System.out.println(rand.nextInt());//int范围的随机数
        System.out.println(rand.nextInt(10)); //[0,10)的数
    }
}

枚举enum

public enum Gender {,}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值