Java se-basic data type corresponding wrapper class

Source: Internet
Author: User
Tags float double java se

Packing class

Java is an object-oriented programming language, and the appearance of wrapper classes is a better embodiment of this idea.

Second, the wrapper class as a class is a property has a method, the function is more powerful than the basic data type.

wrapper classes for eight basic data types
byte--byte short--short int--integer long--long float--float double--double char--character Boolean--bo Olean

Box packing and unpacking

Boxing refers to the conversion of a basic data type to a corresponding wrapper type. For example, int is wrapped as an object of an integer class.
Unpacking refers to the conversion of a wrapper class into a basic data type. For example, the object of an integer class is simplified to int.

"The basic data type becomes the benefit of the object wrapper class"
you can convert basic data to and from strings to each other.        Such as: What is the value range of int type? This should be to check integer--integer.max_value;
   basic data types The primary role of wrapper classes: making conversions between basic data types and strings "Three Ways to convert basic type data to Strings"
1. The basic data type object wraps the method in the class static String toString (base type);  
2.  method in the String class: static string ValueOf (base type)
3. Basic type Data + "";
the key points to be mastered are:
String----> Basic type Data.  Returns the value type--basic type, and the parameter-to-string. Basic data type variable name = Basic data Type wrapper class. Parsexxx (xxxstring); Long L = Long.parselong ("4546545646546");
System.out.println (Integer.parseint ("23") + 1);
There is
only one object in the eight base type Object wrapper class without the Parse method, which is character.   
"type conversion error"   numberformatexception--number Format exception-I ran into it because I added a space. "The method to master in integer "Involves a problem with a binary conversion.
decimal--and other binary. Binary: static string tobinarystring (int), 16 binary: static string tohexstring (int), octal: static string tooctalstring (int), other binary- turn into decimal; Integer.tostring (6, 2); Converts 6 to binary.
parseint (intstring, Radix); System.out.println (Integer.parseint ("110"), 2);
"The basic type is better than the packing type, pay attention to the reasonable use of packing type"

Description: Java has two types, the base type (Primitive type), and the reference type (Reference type). Basic types such as Boolean,int, double, reference types such as string,list. Each base type has its corresponding wrapper type (Wrapper classes), such as Integer that corresponds to int.

In many cases, the base type is better than the boxed base type because:

1. The features of automatic boxing and unpacking are added in JDK 1.5 and later versions. However, improper parallel use of basic types and wrapper types can result in a large amount of implicit boxing and unboxing operations. As in the following for statement, because the loop variable is the base type and sum is the wrapper type, it causes frequent automatic boxing and unboxing operations, resulting in performance degradation.


for (long


SYSTEM.OUT.PRINTLN (sum);

2, for the package type, use "= =" Comparison may not get the expected results. such as: New integer ==new integer (42) will return false. You should use the Equals method to make a comparison of boxing types.

scenarios where a reasonable package type is used are:

1. As elements, keys, and values in a collection

2, generics, must use the type of packaging, such as list<integer>

3, when the method of reflection to adjust the time

Java se-basic data type corresponding wrapper class

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.