包装类、String类源码分析、Date时间类


title: 包装类、String类源码分析、Date时间类
date: 2020-08-12 15:03:12
tags: [java基础]

包装类

测试包装类

package cn.yishan.test;

public class TestWrappedClass {
    public static void main(String[] args) {
        //基本数据类型转成包装类对象
        Integer a = new Integer(3);
        Integer b = Integer.valueOf(30);

        //把包装类对象转成基本数据类型
        int c = b.intValue();
        double d = b.doubleValue();

        //把字符串转成包装类对象
        Integer e = new Integer("999");
        Integer f = Integer.parseInt("666666");

        //把包装类对象转成字符串
        String str = f.toString();

        //常见的常量
        System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
    }
}

自动装箱和拆箱(jdk1.5之后)

测试自动装箱、自动拆箱

package cn.yishan.test;

public class TestAutoBox {
    public static void main(String[] args) {
        Integer a = 100; //自动装箱:Integer a = Integer.valueOf(100);
        int b = a ; //自动拆箱:编译器会修改成:int b = a.intValue();

        Integer c = null;
        if(c != null){
            int d = c; //自动拆箱:调用了:c.intValue()
        }
    }
}

缓存问题

测试自动装箱、自动拆箱

package cn.yishan.test;

public class TestAutoBox {
    public static void main(String[] args) {
        Integer a = 100; //自动装箱:Integer a = Integer.valueOf(100);
        int b = a ; //自动拆箱:编译器会修改成:int b = a.intValue();

        Integer c = null;
    //    if(c != null){
    //        int d = c; //自动拆箱:调用了:c.intValue()
    //    }
        //缓存[-128,127]之间的数字。实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组。
        //当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经创建好的对象
        //如果不在这个范围,则创建新的Integer对象。
        Integer in1 = Integer.valueOf(-128);
        Integer in2 = -128;
        System.out.println(in1 == in2 );//true因为123在缓存范围内
        System.out.println(in1.equals(in2));//true
        Integer in3 = 1234;
        Integer in4 = 1234;
        System.out.println(in3 == in4);//false:因为1234不在缓存范围内
        System.out.println(in3.equals(in4));//true
    }
}

String

  • 字符串内容全部存储到value[]数组中,而变量value是final类型的,也就是常量(即只能被赋值一次)。这就是“不可变对象”的典型定义方式
  • private final char value[];

测试String

package cn.yishan.test;

public class TestString {
    public static void main(String[] args) {
        //String str = "123456";
        //String str2 = str.substring(2,5);
        //System.out.println(str);
        //System.out.println(str2);

        //编译器做了优化,直接在编译的时候将字符串进行拼接
        String str1 = "hello" + " java";
        String str2 = "hello java";
        System.out.println(str1 == str2);//true
        String str3 = "hello";
        String str4 = " java";
        //编译的时候不知道变量中存储的是什么,所以没有办法在编译的时候进行优化
        String str5 = str3 + str4;
        System.out.println(str2 == str5);//false
        //做字符串比较的时候,使用equals 不要使用==
        System.out.println(str2.equals(str5));//true
    }
}

StringBuilder、StringBuffer

测试StringBuilder、StringBuffer可变字符序列

package cn.yishan.test;

public class TestStringBuilder {
    public static void main(String[] args) {
        String str;
        //StringBuilder线程不安全,效率高(一般使用它);StringBuffer线程安全,效率低。
        StringBuilder sb = new StringBuilder("abcdefg");
        System.out.println(Integer.toHexString(sb.hashCode()));
        System.out.println(sb);
        sb.setCharAt(2, 'M');
        System.out.println(Integer.toHexString(sb.hashCode()));
        System.out.println(sb);
    }
}

测试StringBuilder、StringBuffer可变字符序列的常用方法

package cn.yishan.test;

public class TestStringBuilder2 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0 ;i<26;i++){
            char temp = (char)('a'+i);
            sb.append(temp);
        }
        System.out.println(sb);
        sb.reverse();//倒序
        System.out.println(sb);
        sb.setCharAt(3,'高');//修改
        System.out.println(sb);
        //链式调用。核心就是:该方法调用了 return this,把自己返回了
        sb.insert(0,'我').insert(6,'爱');//添加
        System.out.println(sb);
        sb.delete(20,23);//删除
        System.out.println(sb);
    }
}

测试可变字符序列和不可变字符序列的陷阱(字符串循环累加一定要使用StringBuilder)

package cn.yishan.test;

public class TestStringBuilder3 {
    public static void main(String[] args) {
        //使用String进行字符串的拼接
        String str8 = " ";
        for (int i = 0; i < 5000;i++ ){
            str8 = str8 + i;//相当于产生了1000个对象,耗时费内存,工作中一定不能出现此种代码
        }
        //使用StringBuilder进行字符串的拼接
        StringBuilder sb1 = new StringBuilder();
        for (int i = 0; i < 5000;i++){
            sb1.append(i);//建议使用此种方式
        }
    }
}

时间处理相关类(date)

package cn.yishan.test;

import java.util.Date;

public class TestDate {
    public static void main(String[] args) {
        Date d = new Date();
        System.out.println(d);
        //获取当前位置的时间(毫秒)
        System.out.println(d.getTime());
        Date d2 = new Date();
        System.out.println(d2.getTime());
        //d是否在d2之后
        System.out.println(d.after(d2));
    }
}

DateFormat类和SimpleDateFormat类

测试时间对象和字符串之间的互相转换

DateFormat抽象类和SimpleDateFormat实现类的使用

package cn.yishan.test;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDateFormat {
    public static void main(String[] args) throws ParseException {

        //把时间对象按照“格式字符串指定的格式”转成相应的字符串
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String str = df.format(new Date(4000000));
        System.out.println(str);
        //把字符串按照“格式字符串指定的格式”转成相应的时间对象
        DateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
        Date date = df2.parse("2020年08月13日 11时42分45秒");
        System.out.println(date);
        //测试其他的格式字符。比如:利用D,获得当前日是本年的第几天
        DateFormat df3 = new SimpleDateFormat("D");
        String str3 = df3.format(new Date());
        System.out.println(str3);
    }
}

Calendar日历类

Calendar类是一个抽象类,提供了关于日期计算的相关功能

测试日期类的使用

package cn.yishan.test;

import javax.xml.crypto.Data;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;



public class TestCalendar {
    public static void main(String[] args) {
        //获得日期的相关元素
        Calendar calendar = new GregorianCalendar(2999,10,9,22,10,50);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONDAY);
        //1-7表示对应的星期几。1是星期一   以此类推
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        //几号
        int day = calendar.get(Calendar.DATE);
        System.out.println(year);
        System.out.println(month);
        //0-11表示对应的月份。0是1一月   以此类推
        System.out.println(calendar);
        System.out.println(weekday);
        System.out.println(day);

        //设置日期的相关元素
        Calendar c2 = new GregorianCalendar();
        c2.set(Calendar.YEAR,8012);
        System.out.println(c2);

        //日期的计算
        Calendar c3 = new GregorianCalendar();
        c3.add(Calendar.DATE,100);
        System.out.println(c3);

        //日期对象和时间对象的转化
        Date d4 = c3.getTime();
        Calendar c4 = new GregorianCalendar();
        c4.setTime(new Date());
        printCalendar(c4);
    }

    public static void printCalendar(Calendar c){
        //打印:1918年2月3日 11:23:34 周三
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONDAY) + 1;
        int date = c.get(Calendar.DAY_OF_MONTH);
        int dayweek = c.get(Calendar.DAY_OF_WEEK) - 1;
        String dayweek2 = dayweek==0?"日":dayweek+"";
        int hour = c.get(Calendar.HOUR);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        System.out.println(year+"年"+month+"月"+date+"日"+hour+"时"+minute+"分"+second+"秒"+" 周"+dayweek2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值