Java中常用finalize( )方法,包装类概述,装箱和拆箱,Integer缓存区,StringBuffer和StringBuilder,BigDecimal的使用,Date类,Calendar类,SimpleDateFormat类,System类

finalize( )方法:

 

 

 

包装类概述

 

 基本数据类型所对应的引用数据类型

 

 装箱和拆箱

基本类型转换为引用类型为装箱

引用类型转换为基本类型为拆箱

 Object obj1 = true? new Integer(1) : new Double(2.0);三元运算符是一个整体

复制代码
public class Application {
    public static void main(String[] args) {
        //包装类——String
        Integer i = 100;//自动装箱
        //方法一
        String str1 = i+"";
        //方法二
        String str2 = i.toString();
        //方法三
        String str3 = String.valueOf(i);

        //String——包装类
        String str4 = "12345";
        Integer i2 = Integer.parseInt(str4);
        Integer i3 = new Integer(str4);
    }
}
复制代码

包装类方法:

 

 StringBuffer和StringBuilder

StringBuffer:可变长字符串。JDK1.0提供,运行效率慢,线程安全

StringBuilder:可变长字符串,JDK5.0提供,运行效率快,线程不安全

 

 BigDecimal的使用

Calendar类

 

复制代码
public class Application {
    public static void main(String[] args) {
        //创建calendar
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime().toInstant());
        System.out.println(calendar.getTimeInMillis());
        //获取世界信息
        //获取年
        int year = calendar.get(calendar.YEAR);
        //获取月
        int month = calendar.get(calendar.MONTH);
        //获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        //获取小时
        int hour = calendar.get(calendar.HOUR_OF_DAY);
        //分钟
        int second = calendar.get(calendar.SECOND);
        System.out.println(year+"年"+month+"月"+day+"日"+hour+"时"+second+"分");
        //add方法修改
        calendar.add(Calendar.HOUR,-1);
        System.out.println(calendar.getTime().toLocaleString());
    }
}
复制代码

 SimpleDateFormat类

SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类

 

复制代码
public class Application {
    public static void main(String[] args) throws ParseException {
        //创建SimpleDateFormat对象
        SimpleDateFormat sc = new SimpleDateFormat("yyyy年MM月dd日");
        //创建data
        Date date = new Date();
        //格式化date
        //把日期转换成字符串
        String sting = sc.format(date);
        System.out.println(sting);
        //把字符串转换成日期
        Date date1 = sc.parse("1997年9月20日");
        System.out.println(date1);
    }
}
复制代码

System类

主要用于获取系统的属性数据和其他操作,构造方法私有

复制代码
public class Application {
    public static void main(String[] args) throws ParseException {
        //arraycopy:数组的赋值
        //src:源数据
        //srcPos:从那个位置开始复制
        //dest:目标数组
        //destPos:目标的位置
        //length:复制的长度
        int[] arr={1,2,3,4,5,6,7,8,9,0};
        int[] dest = new int[10];
        System.arraycopy(arr,5,dest,0,5);
        for (int i= 0; i<dest.length; i++){
            //System.out.println(dest[i]);
        }
        long start=System.currentTimeMillis();
        for (int i=-999999; i<999999; i++){
            for (int j=-99999; j<99999; j++){
                int result=i+j;
            }
        }
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }
}
复制代码

 

Math类:

Math类包含用于执行基本数学运算的方法,如初等指数,对数,平方根和三角函数

 

 

 用法

复制代码
public class Application {
    public static void main(String[] args) {
        int abs = Math.abs(9);
        System.out.println(abs);
        double pow = Math.pow(-2,4);
        System.out.println(pow);
        int a=2, b=7;
        System.out.println((int)(a+Math.random()*(b-a+1)));
    }
}
复制代码

 

posted @   贝勒爷ma  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
点击右上角即可分享
微信分享提示