java09

java 09

java 09包装类字符串相关类时间处理相关类Date类DateFomat和SimpleDateFormatCalendar其他常用类Math类和Random类File类递归打印目录树枚举enum自定义包装类

包装类

  • 基本数据类型的包装类

  • 自动拆箱和自动装箱

  • 包装类的缓存问题

//测试包装类的使用
public class TestInteger {
    public static void main(String[] args) {
        Integer j = Integer.valueOf(10);
        int a = j.intValue();//把包装类对象转成基本数据类型
        double b = j.doubleValue();
        Integer m = Integer.valueOf("456");//把字符串转成数字
        System.out.println(Integer.MAX_VALUE);
​
        //测试自动装箱和拆箱
        Integer x = 100;//编译器:Integer x = Integer.valueOf(100);
        int y = x;//编译器:int y = x.valueOf();
​
//        Integer z = null;
//        int z2 = z;//空指针异常
​
        //测试缓存问题
        //自动装箱时,【-128,127】之间的数有缓存
        Integer x1 = 100;
        Integer x2 = 100;
        Integer x3 = 1000;
        Integer x4 = 1000;
        System.out.println(x1==x2);//true
        System.out.println(x3==x4);//false
        System.out.println(x1.equals(x2));//true
        System.out.println(x3.equals(x4));//true
    }
}

字符串相关类

  • String类

String类代表不可变字符序列

  • StringBuilder类和StringBuffer类

StringBuilder(线程不安全,效率高)类和StringBuffer(线程安全,效率低)类代表可变字符序列

最好使用StringBuilder

  • 不可变字符序列使用陷阱

//测试字符串相关类
public class TestString {
    public static void main(String[] args) {
        String str = "abc";
        //StringBuilder
        StringBuilder sb = new StringBuilder("abc");
        for (int i = 0; i < 7; i++) {
            sb.append((char) ('a'+i));//追加单个字符
        }
        System.out.println(sb.toString());//转换成String输出
        sb.append(",I can sing my abc");
        System.out.println(sb.toString());
        //StringBuffer
        StringBuffer sb1 = new StringBuffer("xyzw");
        sb1.insert(0,"爱").insert(0,"我");//插入字符串
        System.out.println(sb1);
        sb1.delete(0,2);//删除字符串
        System.out.println(sb1);
        sb1.deleteCharAt(0).deleteCharAt(0);
        System.out.println(sb1);
        System.out.println(sb1.charAt(0));//获取某个字符
        System.out.println(sb1.reverse());//字符串逆序
    }
}
​
​
//效率测试
public class TestString02 {
    public static void main(String[] args) {
        //使用String类进行字符串的拼接
        String str = "";
        long num1 = Runtime.getRuntime().freeMemory();//测试系统剩余内存空间
        long time1 = System.currentTimeMillis();//获取系统的当前时间
        for (int i = 0; i < 5000; i++) {
            str = str + i;
        }
        long num2 = Runtime.getRuntime().freeMemory();//测试系统剩余内存空间
        long time2 = System.currentTimeMillis();//获取系统的当前时间
        System.out.println("String占用内存"+(num1-num2));
        System.out.println("String占用时间"+(time2-time1));
​
        //使用StringBuilder进行字符串的拼接
        StringBuilder sb = new StringBuilder("");
        long num3 = Runtime.getRuntime().freeMemory();//测试系统剩余内存空间
        long time3 = System.currentTimeMillis();//获取系统的当前时间
        for (int i = 0; i < 5000; i++) {
            sb.append(i);
        }
        long num4 = Runtime.getRuntime().freeMemory();//测试系统剩余内存空间
        long time4 = System.currentTimeMillis();//获取系统的当前时间
        System.out.println("StringBuilder占用内存"+(num3-num4));
        System.out.println("StringBuilder占用时间"+(time4-time3));
    }
}

时间处理相关类

Date类

  • java.util.Date

  • Date类,它的对象表示一个特定的瞬间,精确到毫秒

import java.util.Date;
​
//测试时间类
public class TestDate {
    public static void main(String[] args) {
        long nowNum = System.currentTimeMillis();
        System.out.println(nowNum);
        Date date1 = new Date();
        System.out.println(date1);
        System.out.println(date1.getTime());
        Date date2 = new Date(-21L*365*24*3600*1000);
        System.out.println(date2);
        System.out.println(date2.equals(date1));
        System.out.println(date2.before(date1));
        System.out.println(date2.after(date1));
    }
}

DateFomat和SimpleDateFormat

  • DataFormat类是abstract类

  • DateFormat类的子类是SimpleDateFormat类

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
​
//测试DateFormat
public class TestDateFormat {
    public static void main(String[] args) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//年月日时分秒
        String str = "2020-1-1 10:10:22";
        Date yuandan = format.parse(str);
        System.out.println(yuandan.getTime());
        System.out.println(yuandan);
        DateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
        Date date2 = new Date(222222555233L);
        String date2Str = format1.format(date2);
        System.out.println(date2Str);
​
        //小妙招
        Date now = new Date();
        DateFormat f1 = new SimpleDateFormat("今年的第D天,第W周");
        String str3 = f1.format(now);
        System.out.println(str3);
​
​
    }
}

Calendar

  • 日历类:抽象类,提供关于日期计算的功能

  • 月份用0-11表示,星期日是0表示

其他常用类

Math类和Random类

File类

  • 用来表示文件和目录,位于java.io.File类

import java.io.File;
import java.io.IOException;
​
public class TestFile {
    public static void main(String[] args) throws IOException {
        System.out.println(System.getProperty("user.dir"));
        File f = new File("a.txt");//相对路径,默认放到user.dir
        f.createNewFile();//创建文件
        File f2 = new File("d:/b.txt");//绝对路径
        f2.createNewFile();//创建文件
    }
}

递归打印目录树

import java.io.File;
​
//打印目录树,结合递归
public class PrintFileTree {
    public static void main(String[] args) {
        File f = new File(System.getProperty("user.dir"));
        printFile(f,0);
    }
    static void printFile(File file,int level){
        for (int i = 0; i < level; i++) {
            System.out.print("-");
        }
        //输出文件名
        System.out.println(file.getName());
        if (file.isDirectory()){
            File[] files = file.listFiles();//列出他的所有子文件,子目录
            for (File temp :
                    files) {
                printFile(temp, level + 1);
            }
        }
    }
}

枚举enum

  • 一个一个列举出来

  • 枚举本质上还是类,而每个被枚举的成员实质就是一个枚举类型的实例,它们默认都是public static final修饰

  • 若要定义一组常量,可以使用枚举类型

  • 尽量不要使用枚举的高级特性,事实上,高级特性都可以用普通类完成

import java.util.Random;
​
//测试枚举
public class TestEnum {
    public static void main(String[] args) {
        //System.out.println(season.chun);
        //System.out.println(Season.SPRING);
        for (Season s:Season.values()){
            System.out.println(s);
        }
        int a = new Random().nextInt(4);//生成0,1,2,3随机数
        switch (Season.values()[a]){
            case AUTUMN:
                System.out.println("qiu");
                break;
            case SPRING:
                System.out.println("chun");
                break;
            case SUMMER:
                System.out.println("xia");
                break;
            case WINTER:
                System.out.println("dong");
                break;
        }
    }
}
enum Season{
    SPRING,SUMMER,AUTUMN,WINTER
}
//class season{
//    public static final int chun = 0;
//    public static final int xia = 0;
//    public static final int qiu = 0;
//    public static final int dong = 0;
//}

自定义包装类

import java.util.Arrays;
​
//自定义一个包装类,仅限于练习
public class MyInteger {
    private final int value;
​
    private static MyInteger[] cache;//缓存-128--127之间的数字
    private  static final int LOW = -128;
    private  static final int HIGH = 127;
    static {
        cache = new MyInteger[HIGH-LOW+1];
        for(int i = LOW;i<=HIGH;i++){
            cache[i-LOW]= new MyInteger(i);
        }
        System.out.println(Arrays.toString(cache));
    }
    public MyInteger(int value){
        this.value = value;
    }
    public String toString(){
        return value+"";
    }
    public static MyInteger ValueOf(int value){
        if (value>=LOW&&value<=HIGH){
            return cache[value-LOW];
        }
        return new MyInteger(value);
    }
    public int intValue(){
        return value;
    }
​
    public static void main(String[] args) {
        MyInteger a = new MyInteger(10);
        MyInteger b = MyInteger.ValueOf(100);
        MyInteger b2 = MyInteger.ValueOf(100);
        MyInteger b3 = MyInteger.ValueOf(1000);
        MyInteger b4 = MyInteger.ValueOf(1000);
​
        System.out.println(b==b2);//true
        System.out.println(b3==b4);//false
        int c = b.intValue();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值