Java package and api getting started

Source: Internet
Author: User

Java package and api getting started

Java is an object-oriented language, but the basic data type in Java is not object-oriented, which causes a lot of inconvenience in actual use. To solve this problem, in the design Class, a corresponding Class is designed for each basic data type, so that the eight classes corresponding to the basic data type are collectively referred to as the Wrapper Class ), in some cases, it is also translated as an overwriting class or a data type class.

The packaging class is located in the java. lang package. the correspondence between the packaging class and the basic data type is shown in the following table:

Packaging class table

Basic Data Type

Packaging

Byte

Byte

Boolean

Boolean

Short

Short

Char

Character

Int

Integer

Long

Long

Float

Float

Double

Double

Among the eight class names, except for the Integer and Character classes, the class names and basic data types of the other six classes are kept, but only the first letter of the class name can be capitalized.

For packaging classes, these classes are mainly used in two ways:

A. The class type corresponding to the basic data type exists to facilitate operations on objects.

B. related attributes of each basic data type, such as the maximum and minimum values, and related operation methods.

Since the use of the eight packaging classes is similar, the following uses the most common Integer class as an example to introduce the actual use of the packaging class.

 

1. Implement conversion between int and Integer classes

 

In actual conversion, use the constructor of the Integer class and the intValue method inside the Integer class to convert these types. The implementation code is as follows:

 

/*** Method Name: intAndInteger </br> * Details: the constructor of the Integer class and the intValue method in the Integer class implement mutual conversion between these types </br> * developer: souvc </br> * Creation Time: february 1, 2016 </br> * @ throws */@ Test public void intAndInteger () {int intToInteger = 10; Integer in = new Integer (intToInteger ); // convert int type to Integer type System. out. println (in); Integer integerToInt = new Integer (100); int m = integerToInt. intValue (); // converts an Integer type object to an int type System. out. println (m );}

2. Common internal Integer Methods

 

The Integer class contains some int-related methods. The following describes some common methods:

 

A. parseInt Method

Public static int parseInt (String s)

 

This method is used to convert a numeric string to an int value. In future interface programming, converting a string to a corresponding int number is a common operation. Example:

 

/*** Method Name: intParseInt </br> * Details: Convert the string to the corresponding int number </br> * developer: liuhf </br> * Creation Time: february 1, 2016 </br> * @ throws */@ Test public void intParseInt () {String s = "123"; int n = Integer. parseInt (s); System. out. println (n );}

 

The value of int Variable n is 123. This method actually converts the string to the int type. If not all strings contain numerical characters, the program runs abnormally. (Note: The concept of exceptions will be described in the next chapter)

 

Another parseInt method:

Public static int parseInt (String s, int radix)

 

Converts a string to an int in hexadecimal format specified by the radix parameter. The following is an example:

 

/*** Method Name: intParseInt </br> * Details: Convert the string to the corresponding int number </br> * developer: liuhf </br> * Creation Time: february 1, 2016 </br> * @ throws */@ Test public void intParseInt2 () {int n1 = Integer. parseInt ("120", 10); // convert string "120" to int in decimal format, and the result is 120 System. out. println (n1); int n2 = Integer. parseInt ("12", 16); // convert string "12" to int in hexadecimal notation, and the result is 18 System. out. println (n2); int n3 = Integer. parseInt ("ff", 16); // convert string "ff" to int in hexadecimal notation, and the result is 255 System. out. println (n3 );}

 

This allows for more flexible conversion.

 

B. toString Method

1. public static String toString (int I) This method is used to convert the int type to the corresponding String type.

The sample code is as follows:

Int m = 1000;

String s = Integer. toString (m );

The value of string s is "1000 ".

 

2. Another toString method converts the int value to a specific hexadecimal string:

Public static int parseInt (String s, int radix)

The sample code is as follows:

Int m = 20;

String s = Integer. toString (m );

The value of string s is "14 ".

 

In fact, JDK 1.5 (5.0) has introduced the Automatic Disassembly box syntax, that is, the system will automatically perform the conversion of basic data types and corresponding packaging classes, this will greatly facilitate the coding of programmers.

The sample code is as follows:

// The int type is automatically converted to the Integer type.

Int m = 12;

Integer in = m;

// The Integer type is automatically converted to the int type.

Int n = in;

Therefore, in actual use, the type conversion becomes very simple, and the system will automatically implement the corresponding conversion.

 

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.