data Type wrapper class
I. OverviewJava provides a simple type of basic data for the data, as well as the corresponding wrapper class. With basic simple data types, you can improve the performance of your system and meet the needs of most applications. However, simple types do not have the attributes of an object and do not meet certain special requirements. For example, many of the methods of many classes in Java have parameter types that are objects, such as integer classes to wrap integers, and we can use an integer object to easily convert integers to strings.
two. Integer Class (Java.lang.Object-java.lang.Number) The integer class wraps the value of the original type int object, an object of an integer type that contains a field of type int. The wrapper class provides several methods for converting integers to string or string to integer data, and so on.
(1) Constructors
integer (int value): Creates an integer object with the content of the object as an int value |
Integer(String s):创建一个Integer对象,对象的内容为一个Sring对象指定的字符串(如Integer("123")) |
(2) Common member functions A. int/long/float/double int/long/float/doublevalue (): converts integer data corresponding to an integer object into a int/long/float/double basic type data ; B. static Integer decode (String nm) : Decodes a String object into an integer object C. int hashcode () : Returns the hash value of an integer object d. static int parse int/ Parseunsignedint (String s/...) : the string type ... convert to Integer data E. static int sum (int a,int b) : Calculates the sum of two integer data A, b F. String toString () : Converts the integer value to a string object that contains the value G. static integer valueOf (int i): Gets the integer value corresponding to the integer object H. static Integer valueOf (String s) : returns an Integer object with an integer value of the specified string
third, application examples
1. Converting a string to an integer valuemethod One: int w=integer.parseint ("0"); get "0" ASCII code =48=w;method Two: int h=new Integer ("0"). Intvalue (); will be gets the value of an integer object with the return type int type method Three: int h=integer.valueof ("0"). Intvalue (); Effect same as method two, only method three calls an integer static method valueOf Returns an integer objectSummary: Implement a conversion between a string to a basic data type (such as an integral type)int w=xxx wrapper class. Parsexxx (String str), such as int w=integer.parseint ("0"), where Parsexxx () Method is a static method of the wrapper class.
2. Converting wrapper classes to basic datainteger i=new Integer ("0");int h=i.intValue(); Summary: The basic data Type =xxxx wrapper class object. xxxvalue;
3. Source code : Implement a program on the screen to print a rectangle consisting of an asterisk (*), the width and height of the rectangle is specified by the parameters passed by the program at run time.
Class testinteger{public static void Main (string[] agrs) { //1. Converts the characters entered into int h=integer.parseint ( Agrs[0]); The rectangle's high int w=new Integer (). Intvalue (agrs[1]);//Rectangle width //2. Use StringBuffer to create a variable array space that stores a row of * for (int i=0;i
Reference: Http://docs.oracle.com/javase/8/docs/api/index.htmlJava notes nine. Common Apiの data Type wrapper class