JAVA Foundation--java API Common objects (wrapper classes and regular) 12

Source: Internet
Author: User
Tags float double wrapper

first, the basic type of packaging class 1. Basic Type packing class introduction   

8 Basic types:

byte short int long float double char Boolean

These 8 basic types they can only be used in a program to standardize the data types of a certain space, and they cannot manipulate these types in the same way as classes.

int A; In memory you need to open an int type of space, the space name is a

So sun provided their respective class types for these 8 types of primitives. The unification of these types is called the basic type wrapper class.

byte------"byte

Short------"Short"

int------"Integer

Long------"long

float------"float"

Double------Double

char-------"Character

Boolean------"Boolean

The wrapper classes for the eight basic types, except the int and char types, are capitalized in the first letter of the keyword. Six

int Integer

Char Charater

2. Basic type packing class display

The base type corresponds to the wrapper class, providing the member variable that holds the maximum and minimum static variables that can be stored in the current type.

  

     

It is found that the basic type wrapper class does not provide a method for constructing null parameters. Because the object of each basic type wrapper class needs to encapsulate a corresponding base type value. If an empty argument is constructed, the object created does not know that it will encapsulate that base type as an object.

  

1 /*2 * Demo Basic type wrapper class3  */4  Public classIntegerdemo {5      Public Static voidMain (string[] args) {6         7         intx = 123;8         //convert basic type data to corresponding wrapper type9Integer i =NewInteger (x);Ten         /* One * When using the basic type wrapper class to turn a string into the corresponding wrapper class object A * requires that the data in the specified string must be converted to a number if there is an additional non- - * Numeric data, an exception occurs when the wrapper class object is created.  -          */ theInteger i2 =NewInteger ("123"); - System.out.println (i2); -         //Calling member variables - System.out.println (integer.max_value); + System.out.println (integer.min_value); - System.out.println (i); +          A System.out.println (integer.tobinarystring (x)); at System.out.println (integer.tohexstring (x)); - System.out.println (integer.tooctalstring (x)); -     } -}

3. Conversion between basic data types and wrapper classes A. Basic type turn into wrapper class

1, the use of packaging class construction method

2. Using static methods in the class valueof

    

B. Turning a wrapper type into a basic type

Use the Xxxvalue method in the wrapper class corresponding to the current base type to complete

    

4. String and base type data, wrapper type conversion a. String to basic type

Use the Parsexxxx method in the wrapper class that corresponds to the base type.

      

B. Basic type-to-string

B.1 the static valueof provided in the string class, you can turn the base type into a string

B.2 using the + sign link

B.3 using the static ToString method in the wrapper class

      

C. String-to-package type

The construction method of C.1 and packing class

Static methods in C.2 and packaging classes valueof

    

D. Wrapper type to string

D.1, using the static ValueOf method in the String class

D.2, using the ToString method of the wrapper class to duplicate the object

    

E. Summary

Conversions between basic types, wrapper types, and strings:

    

5. Automatic box packing and unpacking

In JDK5, the conversion between the basic type data and the corresponding wrapper type is simplified.

Auto-Boxing: You can assign a basic type of data directly to a reference to the corresponding wrapper type.

      

Automatic unpacking: You can assign a reference to a wrapper type directly to the base type variable.

6. Practice

  

1 /*2 * Requirement: The numeric data in the string is sorted according to their literal value. 3 * "11-11-1 -101 123 101"4 * Analysis:5 * The string needs to be cut to get an array of strings, and this object cannot be sorted directly6 * Because direct ordering is the size comparison of the encoded values of the characters at the same location as each string in the array7 * and cannot be compared by the number specific values in each string8  * 9 * We'll need to convert each string in the current string array to a number of type int, then sort in the comparison sizeTen  */ One  Public classTest { A      Public Static voidMain (string[] args) { -          -String s = "11-11-1-101 123 21 12 101"; the          -         //use space to cut -string[] STRs = S.split (""); -          +         //defines an array of type int that holds each int data after the turn -         int[] arr =New int[strs.length]; +          A         //loops out each string data in a string array at          for(inti = 0; i < strs.length; i++) { -             //converts a string into an int value in an ARR array -             intx =Integer.parseint (Strs[i]); -Arr[i] =x; -         } -         //The end of the loop means that every string in the current STRs array has been converted to data of type int saved in arr in         //Sort arr - Arrays.sort (arr); to          +         //convert an array to a string -         //use a string buffer to temporarily store data that needs to be stitched in a buffer theStringBuilder SB =NewStringBuilder (); *         //loop out data for each int type in arr $          for(inti = 0; i < arr.length; i++) {Panax Notoginseng sb.append (Arr[i]); -Sb.append (""); the         } +          A         //to convert data in a string buffer into string data theString SS =sb.tostring (); +          - SYSTEM.OUT.PRINTLN (ss); $     } $}

  

    

JAVA Foundation--java API Common objects (wrapper classes and regular) 12

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.