java一些常见类的使用

public class Demo {

    public static void main(String[] args) {

        //java.text包下的DecimalFormat类,日期格式化类SimpleDateFormat
        //#表示这一位存在就显示,不存在就不显示,#后的0表示存在就显示,不存在显示0,.后面表示保留2位小数
        java.text.DecimalFormat dfDecimalFormat=new java.text.DecimalFormat("#0.00");
        System.out.println(dfDecimalFormat.format(12.43453));//12.43
        java.text.DecimalFormat dfDecimalFormat1=new java.text.DecimalFormat("#.00");
        System.out.println(dfDecimalFormat1.format(0.43453));//输出.43,前面0.几的话0会省略掉    
        System.out.println(dfDecimalFormat1.format(4.43453));//输出4.43,所以格式("#0.00")好    

        SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日");
        Date d=new Date();
        System.out.println(sf.format(d));//格式化日期对象2015年01月10日
        sf=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        System.out.println(sf.format(d));//2015年01月10日 04:46:06


        //java.lang包下的:math数学公式类
        System.out.println(Math.PI);//圆周率
        System.out.println(Math.abs(-100));//绝对值
        System.out.println(Math.ceil(4.1232));//向上取整5.0
        System.out.println(Math.ceil(-4.1232));//向上取整-4.0
        System.out.println(Math.floor(4.1232));//向下取整4.0
        System.out.println(Math.floor(-4.1232));//向下取整-5.0
        System.out.println(Math.round(4.432));//四舍五入4
        System.out.println(Math.round(4.654));//5
        System.out.println(Math.sqrt(100));//平方根10.0
        System.out.println(Math.pow(2, 3));//2的三次方8.0
        System.out.println(Math.random());//生成0到1间的随机数


        //java.util包下的Random类,Date类,calendar类
        Random random=new Random();
        System.out.println(random.nextInt());//打印随机整数正负都有,没有确定范围
        System.out.println(random.nextInt(100));//打印随机整数[0到100)

        java.util.Date date=new java.util.Date();//获得当前系统日期对象
        System.out.println(date);//标准时间格式 //Sat Jan 10 13:57:21 CST 2015

        //先通过java.util.Data对象,构造java.sql.Date对象
        java.sql.Date date2=new java.sql.Date(date.getTime());
        System.out.println(date2);  //2015-01-10

        Calendar calendar=Calendar.getInstance();
        //Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象
        System.out.println(calendar);
        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 minute=calendar.get(Calendar.MINUTE);
        int second=calendar.get(Calendar.SECOND);
        System.out.println(year+"-"+month+"-"+day+"   "+hour+":"+minute+":"+second);


        //String类与StringBuffer类
        //String:内容不可变,改变内容就是创建新对象
        //StringBuffer:内容可变,改变内容不改变对象
        String s=new String("I love you");//s是栈内存的引用,右边为堆内存里的值,s指向堆内存的"I love you"
        System.out.println(s);
        System.out.println(s.hashCode());
        s+=" china";
        System.out.println(s);
        System.out.println(s.hashCode());//hashCode不同,相当于创建了新对象

        StringBuffer sb=new StringBuffer("I love you");
        System.out.println(sb);
        System.out.println(sb.hashCode());//508839021
        sb.append(" China");
        System.out.println(sb);//I love you China
        System.out.println(sb.reverse());//字符串的反转anihC uoy evol I
        System.out.println(sb.hashCode());//508839021,hashCode相同,相当于在远对象上追加,没有创建新对象

        // StringBuilder:线程非安全的,StringBuffer:线程安全的
        // StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操作时,
        //实际上是在一个对象上操作的,不像String一样创建一些对象进行操作,所以速度就快了
        //1.如果要操作少量的数据用 = String
        // 2.单线程操作字符串缓冲区 下操作大量数据 = StringBuilder
        //3.多线程操作字符串缓冲区 下操作大量数据 = StringBuffer



}
          //System类
        System.err.println("this is error");//红色输出
        //System.exit(0);//表示正常退出,执行这语句后,后面的语句不会执行了
        //System.getProperties().list(System.out);//打印环境变量
        
        //实现程序运行的计时功能
        long startTime=System.currentTimeMillis();
        for(int i=0;i<100000;i++)
        {
            System.out.println(i);
        }
        long endTime=System.currentTimeMillis();
        System.out.println("程序执行时间为"+(endTime-startTime)+"ms");


        Runtime runtime=Runtime.getRuntime();
        //每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。
        //可以通过 getRuntime() 方法获取当前运行时。 
        try {
            runtime.exec("notepad"); //打开一个记事本操作命令
        } catch (Exception e) {
            e.printStackTrace();
        }
public class 垃圾回收 {

    public static void main(String[] args) {
        person p=new person("jack", 21);//p的引用指向堆内存的jack,21这个对象
        p=null;//将栈内存p的引用置为null,此时堆中的person()对象为垃圾。
        System.gc();//Garbage Collection(简称gc)实行垃圾回收,将堆中的person对象回收
        //执行System.gc()方法时候会自动调用person类的finalize()方法
        /*
         * java垃圾回收是有jvm自动执行的,不是人为操作的,所以当不存在对某对象的任何引用时,
         * 该对象就处于被jvm回收的状态,并不是马上予以销毁。
         */
    }

}
class person
{
    private String name;
    private int age;
    public person(String name,int age)
    {
        this.name=name;
        this.age=age;
    }
    @Override
    protected void finalize() throws Throwable {
        // TODO Auto-generated method stub
        super.finalize();
        System.out.println("person对象销毁了");
    }
    
}

 


 

转载于:https://www.cnblogs.com/linhong/p/4215286.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值