Java wrapper classes and other classes

1. List of packaging classes

 
basic data type   Packaging class
byte Byte
short Short
int Integer
boolean Boolean
char Character
long Long
float Float
double Double

 

 1) Among the eight class names, except for Integer and Character, the class names of the other six classes are the same as the basic data types, except that the first letter of the class name is capitalized.

 2) For packaging classes, the uses of these classes mainly include two types:

     a. It exists as a class type corresponding to the basic data type, which is convenient for operations involving objects.

     b. Contains the relevant attributes of each basic data type, such as the maximum value, the minimum value, etc., as well as the related operation methods.

 3) Since the use of the eight wrapper classes is similar, the following takes the most commonly used Integer class as an example to introduce the actual use of the wrapper class.

     1. Implement the conversion between int and Integer classes:

      int n = 10;

      Integer a = new Integer(n); //int to Integer (boxing)

      Integer b = Integer.valueOf(n); //int to Integer (boxing)

 

      int k = a.intValue(); //Integer to int (unboxing)

    2. Common internal methods of Integer

      int n = Integer.parseInt("123"); //The string is converted to int. If the parameter is not a numeric string, an exception NumberFormatException will occur.

      int k = Integer.parseInt("123",16); //The parameter can be two, and the latter represents the base you want to convert.

      

      String s = Integer.toString(123); // Convert the number 123 to the string "123".

      String s1 = Integer.toString(123,16);  //将数字123转成字符串,以16进制的形式。

 

二、日期类型

  Date:java.util包下的类型

  获取当前系统时间:

    1)Date now = new Date();//获取的是日期格式的当前系统时间。

      方法:

        getTime();  //获取毫秒数,long类型

        setTime(long time);  

    2)System.currentTimeMillis();  //获取的是距1970年1月1日时间点的毫秒数

三、日期格式化类型

  SimpleDateFormat:java.text包下的类型

  定义格式:

    1):可以格式化日期,将日期变成字符串

       format(Date date)  //返回的字符串类型

      eg:

       Date date = new Date();

         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");

       String s = simpleDateFormat(date);

    2):可以将字符串转成成日期

      parse(String date)  //返回的Date类型

      Date date = simpleDateFormat.parse("1999-1-1");

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324915043&siteId=291194637