Wrapper classes for basic data types in Java and conversions between basic data types

Source: Internet
Author: User
Tags character classes wrapper
Java Basic Data type wrapper class

The Java language is an object-oriented language, but the basic data types in Java are not object-oriented, and this is a lot of inconvenience in the actual use, in order to solve this problem, in the design of the class for each basic data type design a corresponding class to represent, These eight classes corresponding to the basic data type are collectively referred to as the wrapper class (wrapper class), and some places are also translated into the outer-covering class or data type class.

The wrapper classes are located in the Java.lang package, and the corresponding relationship between the wrapper class and the base data type is shown in the following table:

Wrapper class corresponding Table

Basic data types

Packing class

Byte

Byte

Boolean

Boolean

Short

Short

Char

Character

Int

Integer

Long

Long

Float

Float

Double

Double

In these eight class names, in addition to the integer and character classes, the class names and basic data types of the other six classes have been all along, only the first letter of the class name is capitalized.

For the wrapper class, the purpose of these classes consists of two main types:

A, as the class type corresponding to the basic data type exists, to facilitate the operation of the object involved.

b, the related attributes of each basic data type such as the maximum value, the minimum value, and so on, as well as related methods of operation.

Since the use of eight wrapper classes is similar, the following is an example of the most commonly used integer class to describe the actual use of the wrapper class.

1. Implement the conversion between int and integer classes

In the actual conversion, the conversion between these types is implemented using the constructor method of the integer class and the Intvalue method inside the integer class, and the implementation code is as follows:

int n = 10;

Integer in = new Integer (100);

Converts an int type to an integer type

Integer in1 = new integer (n);

Convert an object of type integer to an int type

int m = In.intvalue ();

2, the general method of the internal integer class

There are some methods in the integer class that are related to int operations, and here are some of the more common methods:

A, parseint method

public static int parseint (String s)

The purpose of this method is to convert a numeric string to an int value. In future interface programming, converting a string to the corresponding int number is a more common operation. Use the example below:

String s = "123";

int n = integer.parseint (s);

The value of the INT variable n is 123, which actually implements the conversion between the string and the int, and if the string contains not all numeric characters, the program execution will appear unexpectedly. (Description: The concept of an exception is described in the next chapter)

Another parseint method:

public static int parseint (String s, int radix)

The implementation converts the string to int as specified by the parameter radix, using the following example:

Converts the string "120" to int by decimal, the result is 120

int n = integer.parseint ("120", 10);

Converts the string "12" to int by 16, the result is 18

int n = integer.parseint ("12", 16);

Converts the string "FF" to an int by 16, then the result is 255

int n = integer.parseint ("FF", 16);

This enables more flexible conversions.

B, ToString Method

public static String toString (int i)

The purpose of this method is to convert the int type to the corresponding string type.

Use the sample code as follows:

int m = 1000;

String s = integer.tostring (M);

The value of the string s is "1000".

Another ToString rule implements a string that converts an int value to a specific feed:

public static int parseint (String s, int radix)

Use the sample code as follows:

int m = 20;

String s = integer.tostring (M);

The value of the string s is "14".

In fact, since the JDK 1.5 (5.0) version, the introduction of the Automatic Disassembly box syntax, that is, the basic data types and corresponding packaging class conversion, the system will automatically, which will greatly facilitate the programmer's code writing. Use the sample code as follows:

The int type is automatically converted to an integer type

int m = 12;

Integer in = m;

An integer type is automatically converted to an int type

int n = in;

So the type conversion will be very simple in actual use, and the system will automatically implement the corresponding conversion


conversion between strings and other data types in Java

The conversion between strings and other data types is provided in Java, especially with the conversion between the base data type and the wrapper class, which is often used.

1. Convert other data types to strings

There are two ways to convert other data types to strings: to invoke the ToString () method of the class and to invoke the valueof () method of the String class.

(1) invoke the ToString () method of the class. if the class is the wrapper class for the base data, you can use the ToString () method with or without parameters.

The ToString () method with no parameters is a non-static method of the class, and therefore must be invoked through the class object. For example, to convert an integer object to a string:
Java code integer num = new integer (300); System.out.println ("result=" + (num.tostring () +200));

The results of the operation are as follows:
result=300200

The ToString () method with parameters is a static method of the class, so it can be called directly from the class. The parameter in the method should be the basic data type variable or literal constant that corresponds to the wrapper class. For example, convert a float type data to a string:
Java code float f=12.345f; System.out.println ("result=" + (float.tostring (f) +1.02f));

The results of the operation are as follows:
result=12.3451.02


Typically, the ToString () method without parameters is used to convert the wrapper class data to a string, and the ToString () method with parameters is used to convert the base data type to a string. &NBSP
provides a ToString () method with no parameters in the Java.lang.Object class in Java, which returns a string representation of the current object in the form of a class name + "@" + hexadecimal number Represents the address of an object. All subclasses derived from the object class can call ToString () to return a string representation of the class object, and subclasses can override the ToString () method to return other values. In addition, in some cases, the object's ToString () method, such as the print () method and the string "+" operation, is automatically invoked. For example:  
Java code public class  conversiondemo1{ public static  void  main (String args[]) {ConversionDemo1 c= new  conversiondemo1 (); Create ConversionDemo1 Class object string s= "output:" The +c;//system invokes the C object's ToString () method before the string "+" Operation System.out.println (c); The system first invokes the ToString () method of the C object and then outputs the return value of the method System.out.println (s); } }

The

results are as follows:  
conversiondemo1@7d8a992f 
Output: conversiondemo1@7d8a992f 

(2) Call the valueof () method of the String class. you can convert the wrapper type data to a string by calling the ValueOf () method of the String class, or you can convert the base data type or literal constant to a string, and, when converting other reference type objects, it is equivalent to invoking the ToString () method of the object. For example, convert float data to string, convert double type data to string, convert ConversionDemo2 class object to string:  
Java code public class  conversiondemo2{ public static void  main (String args[)) { float  f = 45.678f;//defines a float variable and assigns a value of 45.678 Double d =  new  double (12.345) ;//Create a Double object ConversionDemo2 c= new  conversiondemo2 ();//Create a ConversionDemo2 class object System.out.println (String.valueof (f) +10);//Invoke String's valueof (float f) method to convert float data to string and then to integer 10 System.out.println (string.valueof (d) +10);//Invoke String's valueof (Object obj) method converts a double object to a string and then an integer 10 operation System.out.println (String.valueof (c));//Invoke String's valueof (Object obj) method to convert Class object C to string, then output}}

The results of the operation are as follows:  
45.67810 
12.34510 
conversiondemo2@8dc8569 

Basic data types in Java can be converted to strings by the corresponding valueof () method in string, whereas valueof (Byte B) and valueof are not provided in the String class (short s) method to convert byte and short data to strings, but the following method invocation is legal:  
Java code byte  b= ' a '; Short  s=123; System.out.println (string.valueof (b));//Output Result: System.out.println (string.valueof (s));//output result: 123

This is because byte and short are automatically converted to the int type, thus calling the valueof (int i) method. &NBSP

2, converting a string to a base data type  

(1) converts a string to the base data type of the wrapper class by wrapping the Parsexxx () method of the class , However, the requirement string must be a numeric form, for example int num = Integer.parseint ("123"); is legal, and int num = Integer.paseint ("abc"); Java.lang.NumberFormatException:For input string: "ABC" exception. &NBSP
The Parsexxx () method that each wrapper class has is shown in the following table:  




Note: Where the string argument in the Parseboolean () method evaluates to "True" (uppercase or lowercase), The result is true, otherwise the result is false. &NBSP

(2) invokes the Xxxvalue () method by wrapping the object of the class.  
For example: int i = new Integer ("123"). Intvalue ();  
The Xxxvalue () method of each wrapper class is as follows in table 2:  




You can see from table 2 that the basic data types can be converted to each other except for the Boolean and char types, and that the wrapper class object can be converted to the corresponding base data type.

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.