• java中必须知道的常用类


    1、Java的包装类

      基本数据类型我们都很熟悉,例如:int、float、double、boolean、char等,基本数据类型不具备对象的特征,不能调用方法,一般能实现的功能比较简单,为了让基本数据类型也具备对象的特性,Java为每个数据类型都提供了一个包装类,这样我们就可以像操作对象一样,操作这些基本数据类型了

      常见的包装类和基本类型对应关系如下:

      包装类主要提供了两类方法:

      1、进行多个类型之间的转换

      2、将字符串和本类型及包装类相互转换

      比如下面代码:

     int i = 2;
     Integer m = new Integer(5);
     Integer n = new Integer("8");

      第一行是用基本类型定义了整型变量i,第二行是利用int包装类定义了int对象m,虽然赋值为整型的5,但是现在会将基本类型转换为包装类中的5

      第三行初始化值为字符串类型的"8",但是会被包装类转换成整数类型,这就是包装类的基本使用方法

      每个包装类都可以和其他类进行转换,比如Integer包装类,转换的对应关系如下:

      

    根据转换的对应关系,可以写一个简单的实例如下:

    public class HelloWorld {
        public static void main(String[] args) {
    
             int score1 = 86;
    
             // 创建Integer包装类对象,表示变量score1的值
             Integer score2=new Integer(score1);
    
             // 将Integer包装类转换为double类型
             double score3=score2.doubleValue();
    
             // 将Integer包装类转换为float类型
             float score4=score2.floatValue();
    
             // 将Integer包装类转换为int类型
             int score5 =score2.intValue();
    
             System.out.println("Integer包装类:" + score2);
             System.out.println("double类型:" + score3);
             System.out.println("float类型:" + score4);
             System.out.println("int类型:" + score5);
         }
     }

    通过这个简单的包装类转换程序,可以看出包装类转换为各种基本数据类型也是很方便的

    那么基本类型怎么转换为包装类的对象呢?其实前面三行代码已经体现了,只不过不太完全,基本类型转换为包装类可以理解为一个装箱的过程,装箱有两种方式:手动装箱和自动装箱,那么同理包装类转换为基本类型叫做拆箱,同样分为:手动拆箱和自动拆箱,上面的例子就是一个手动拆箱的例子,

    那么其中自动装箱和自动拆箱,都是编译器根据数据类型自动完成了转换,简单的代码如下:

    public class HelloWorld {
         public static void main(String[] args) {
             double a = 91.5;
    
              // double类型手动装箱
             Double b = new Double(a);
    
             // double类型自动装箱
             Double c = a;
    
             System.out.println("装箱后的结果为:" + b + "和" + c);
    
             Double d = new Double(87.0);
    
             // Double包装类对象手动拆箱
             double e = d.doubleValue();
    
             //Double包装类对象自动拆箱
             double f = d;
    
              System.out.println("拆箱后的结果为:" + e + "和" + f);
         }
     }

    例子很简单,对每一个数据类型分别完成了自动和手动两个操作,所以b和c的值是一样的,e和f的值是一样的

    除了基本数据类型的包装转换之外,基本类型和字符串之间也可以进行转换

    比如一个基本类型:int a = 30;有3中方式转换为字符串对象:

      1、使用包装类的toString方法,String str1 = Integer.toString(a);

      2、使用String类的valueOf方法,String str2 = String.valueOf(a);

      3、用一个空字符串加上基本类型,系统会将基本类型转换为字符串类型,String str3 = a + "";

    反过来,定义:String str = "18";将一个字符串类型转换为基本类型有以下两种方法:

      1、调用包装类的parseXxx静态方法,int b = Integer.parseInt(str);

      2、调用包装类的valueOf方法,完成自动拆箱,int c = Integer.valueOf(str);

    其他类型转换也是一样,替换里面的类型即可,下面是一个简单的例子:

    public class HelloWorld {
         public static void main(String[] args) {
    
             double m = 78.5;
             //将基本类型转换为字符串
             String str1 = Double.toString(m);
    
             System.out.println("m 转换为String型后与整数20的求和结果为: "+(str1+20));
    
             String str = "180.20";
             // 将字符串转换为基本类型
             Double a = Double.valueOf(str);
    
             System.out.println("str 转换为double型后与整数20的求和结果为: "+(a+20));
         }
     }

    这个例子实现了两次转换,所以str1+20应该输出:78.520,a+20应该输出:200.20,前者结果是字符串类型,后者结果是基本的浮点数类型

      2、Java日期时间处理

      程序开发中,时间处理必不可少,这些内容相对简单一些,会使用一些固定的类即可

      首先,我们可以使用java.util包中的Date类来获取时间,方法如下:

     Date d = new Date();
     System.out.println(d);

    这样是最简单的方法了,但是输出格式可能不太适合我们:Mon Sep 21 21:46:13 CST 2015

    所以我们需要进行简单的修饰,那么这时需要java.text包中的SimpleDateFormat类中的format方法对文本进行格式化,代码如下:

     Date d = new Date();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     String today = sdf.format(d);
     System.out.println(today);

    这样就输出了我们常用的时间:2015-09-21 21:51:39,另外格式我们也可以按照我们的需要定义

    反过来呢,我们还可以把文本变成默认的时间格式,这用到了SimpleDateFormat类中的parse方法,但是这个方法可能会出现异常,所以需要java.text中的异常类ParseException类进行异常处理,看一个小实例:

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class HelloWorld {
    
         public static void main(String[] args) throws ParseException {
    
             // 使用format()方法将日期转换为指定格式的文本
             SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
             SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
             SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
             // 创建Date对象,表示当前时间
             Date now = new Date();
    
             // 调用format()方法,将日期转换为字符串并输出
             System.out.println(sdf1.format(now));
             System.out.println(sdf2.format(now));
             System.out.println(sdf3.format(now));
    
             // 使用parse()方法将文本转换为日期
             String d = "2015-9-21 21:56:36";
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
              // 调用parse()方法,将字符串转换为日期
             Date date = sdf.parse(d);
    
             System.out.println(date);
         }
     }

    例子很简单,实现了时间格式化,文本到日期的转换

    另外在java.util包中还有一个时间处理的Calendar类,因为Date类中的本身的设计方法,遭到了众多批评,所以推荐使用Calendar类来处理时间,Calendar类是一个抽象类,可以通过调用getInstance静态方法获得Calendar的一个对象,此对象默认由当前时间进行初始化,直接来一个简单的实例:

    package com.imooc.collection;
    
    import java.util.Calendar;
    
    public class DateAction {
    
        public static void main(String[] args) {
            Calendar c = Calendar.getInstance();    //实例化Calendar对象
            int year = c.get(Calendar.YEAR);    //通过get方法获得当前年
            int month = c.get(Calendar.MONTH) + 1;    //月份0-11,需要加1
            int day = c.get(Calendar.DAY_OF_MONTH);    //日期
            int hour = c.get(Calendar.HOUR_OF_DAY);    //获取小时
            int minute = c.get(Calendar.MINUTE);    //获取分钟
            int second = c.get(Calendar.SECOND);    //获取秒数
            System.out.println("当前时间:" + year + "年" + month + "月" + day + "日" + "   " + hour +
                    ":" + minute + ":" + second);
        }
    
    }

    这样就打印出结果了:当前时间:2015年9月21日   22:12:13

    Calendar还提供了getTime方法,用来获取Date对象,实现Calendar和Date对象的互转,还可以用getTimeInMillis获得1970年01月01日0时0分0秒,到当前时间的毫秒数,大约是Unix时间戳的1000倍

    简单代码如下:

     Date date = c.getTime();    //对象转换
     Long time = c.getTimeInMillis();    //获取至现在的毫秒数
     System.out.println("当前时间:" + date);
     System.out.println("当前毫秒数:" + time);

    这样就实现了转换

      3、Java数学运算Math类

      Math类也是Java中做运算常用的类,在java.lang包中,这个系统会自动导入,这里简单的记录一下,常用的方法:

      

      直接看一个简单的实例:

    public class MathAction {
    
        public static void main(String[] args) {
            double a = 12.86;
            int b = (int) a;    //强制类型转换
            System.out.println("强制类型转换:" + b);
            long c = Math.round(a);    //四舍五入
            System.out.println("四舍五入:" + c);
            double d = Math.floor(a);    //返回小于参数a的最大整数
            System.out.println("小于a的最大整数:" + d);
            double e = Math.ceil(a);    //返回大于参数a的最小整数
            System.out.println("大于a的最小整数:" + e);
            double x = Math.random();    //产生[0,1)区间内的随机浮点数
            System.out.println("默认随机数:" + x);
            int y = (int) (Math.random()*99);    //产生[0,99)区间内的随机整数
            System.out.println("0-99之间的随机整数(不包括99):" + y);
    
        }
    
    }

    很容易理解的例子,效果如下:

     以上就是Java开发中经常用到的类和方法,以上主要就是包装类、时间处理、数学计算这些

    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    用户、角色和权限关系
    IDEA debug启动的时候需要等半个小时甚至更长时间
    快速启动服务
    leetcode — rotate-image
    leetcode — permutations-ii
    leetcode — jump-game-ii
    leetcode — jump-game
    leetcode — trapping-rain-water
    leetcode — first-missing-positive
    leetcode — combination-sum-ii
  • 原文地址:https://www.cnblogs.com/jijm123/p/15662397.html
Copyright © 2020-2023  润新知