Java foundation Day 4 - Common API

Common API

Overview of basic types of packaging

The encapsulation basic data types can be defined as objects in an object advantage of the method of operating function data more

commonly used one operation: means for converting data types between the base and the string

Basic data types Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Overview and use of Integer class

Integer: a target value of the original package type int

Method name Explanation
public Integer (int value) Creating an Integer object (obsolete) according to an int value
public Integer (String s) Creating an Integer object (obsolete) based on the value of String
public static Integer valueOf(int i) Int Returns Integer instance representing the specified value
public static Integer valueOf(String s) Returns an Integer object save the specified value String

int and String interconversion

The most common type of packaging operation are substantially: for conversion between the base and the string types

1.int converted into String

public static String valueOf (I int) : int Returns a string representation of the argument. This method is a method String class

2.String converted to an int

public static int the parseInt (String S) : parse a string int. This method is a method in the class Integer

    /*
    int和String的相互转换
    */
    public class IntegerDemo {
        public static void main(String[] args) {
            //int---String
            int number = 100;
            //方式1
            String s1 = "" + number;
            System.out.println(s1);
            //方式2
            String s2 = String.valueOf(number);
            System.out.println(s2);
            System.out.println("========");

            //String---int
            String 2 = "100";
            //方式1
            //String---Integer---int
            Integer i =Integer.valueOf(s);
            int x = i.intValue();
            System.out.println(x);
            //方式2
            int y = Integer.parseInt(s);
            System.out.println(y);
        }
    }

Case : Sort the data string

needs: there is a string "9127463850", please write programs to achieve the final output is "2738465091"

    public class IntegerTest {
        public static void main(String[] args) {
            //定义一个字符串
            String s = "91 27 46 38 50";
            
            //把字符串中的数据存储到一个int类型的数组中
            String[] strArray = s.split(" ");
    //        for (int i = 0; i < strArray.length; i++) {
    //        System.out.println(strArray[i]);
    //        }
            
            //定义一个int数组,把String[]数组中的每一个元素存储到int数组中
            int[] arr = new int[strArray.length];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = Integer.parseInt(strArray[i]);
            }
    //        for (int i = 0; i < arr.length; i++) {
    //            System.out.println(arr[i]);
    //        }
            
            //对int数组进行排序
            Arrays.sort(arr);
            
            //把排序后的int数组中的元素进行拼接得到一个字符串,这里拼接采用StringBuilder来实现
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < arr.length; i++) {
                if(i == arr.length - 1) {
                    sb.append(arr[i]);
                }else {
                    sb.append(arr[i]).append(" ");
                }
            }
            String result = sb.toString();
            
            //输出结果
            System.out.println("result:" + result);
            //result:27 38 46 50 91
        }
    }

Automatic boxing and unboxing

  • Packing: converting the basic data types of corresponding type packaging
  • Unpacking: The wrapper class type into a corresponding elementary data types

    NOTE : When using the type of packaging, if you do the operation, it is best determined whether null, is recommended, as long as the object, must be carried out prior to use the determination is not null
    public class IntegerDemo {
        public static void main(String[] args) {
            //装箱:把基本数据类型转换为对应的包装类类型
            Integer.valueOf(100);
            Integer ii = 100;//Integer.valueOf(100);
            
            //拆箱:把包装类类型转换为对应的基本数据类型
            //ii += 200;
    //        ii = ii.intValue() + 200;
            ii += 200;
            System.out.println(ii);
            
            Integer iii = null;
            if(iii != null) {
                iii += 300;
            }       
        }
    }

Date class

Date class constructor and Overview

Date represents a specific time, to the millisecond

Method name Explanation
public Date() A Date object assigned and initialized, so that it represents the time it was divided to the nearest millisecond
public Date(long date) Assign a Date object, and initializes it is a specified time in milliseconds from the normalized value

Date class common method

Method name Explanation
public long getTime() Date of acquisition is subject to 00:00:00 January 1 millisecond value from 1970
Public void setTime(long time) Set time value in milliseconds is given

SimpleDateFormat class overview

1.SimpleDateFormat is a concrete class that is used to set the region sensitive manner and parse the date format.

2. the date and time specified by the date and time format pattern string, date and time of the pattern string, from 'A' to 'Z' and from 'a' to 'z' quoted letter to be construed as a date or time time letter string pattern assembly

3. the letter and the corresponding common mode relationship is as follows:

  • y Year
  • M May
  • d day
  • When H
  • m min
  • s seconds

SimpleDateFormat constructor

Method name Explanation
public SimpleDateFormat() Construct a SimpleDateFormat, use the default mode and date format
public SimpleDateFormat(String pattern) Construct a SimpleDateFormat using the given pattern and the default date format

SimpleDateFormat formatting and parsing dates

1. Format (from to Date String)

public String Final the format (DATE Date) : date formatted date / time string

2. Parse (from String to Date)

public Date the parse (String Source) : from a given string the start parsing the text to generate date

Calendar of common methods

Method name Explanation
public int get(int field) Returns the value of a given calendar field
public abstract void add(int field,int amount) According to the calendar rules to add or subtract a given calendar field specified amount of time
public final void set(int year,int mouth,int date) Set the current calendar date

Guess you like

Origin www.cnblogs.com/energy-xjq/p/fourth_day.html
Recommended