Crazy Java Learning Notes-----------wrapper class and anonymous class __java

Source: Internet
Author: User
Tags character classes wrapper
Packing Class (wrapper class)

The wrapper class is a wrapper for the native data type.

Because there are 8 native data types, there should be 8 wrapper classes.

All packing classes (8) are located under Java.lang.

The 8 wrapper classes in Java are:Byte, Short, Integer, Long, Float, Double, Character, Boolean, and they are used in the same way, You can implement bidirectional conversions of native data types and wrapper types.

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;

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

The following are examples of the main integer classes. Integer

The integer class wraps the value of type int into an object.

An integer constructs an object of the corresponding integer by using the following construction method:

public Integer (int value);

The public int intvalue () method returns the integer value that is wrapped by this wrapper class. Automatic boxing/unboxing (autoboxing/unboxing)

A new feature of JDK5.0 is automatic boxing and automatic unpacking.

Automatic boxing/unboxing greatly facilitates the use of basic types of data and their wrapper classes.

  Automatic Boxing : The basic type is automatically converted into a packing class (int >> Integer)

  Automatic unboxing : Packing class automatically converted to basic type (Integer >> int)

For example, the following examples:

Import java.util.ArrayList;
Import java.util.Collection;

public class Boxtest
{public
    static void Main (string[] args)
    {
        collection<integer> c = new Arraylist<integer> ();
    
        C.add (3);//convert 3 of int type to integer type and put in the collection        
    }
}
Type of package: byte,integer,short,long,boolean,character,float,double

Base type (primitive type)
Instead of creating a variable with new, you create an automatic variable that is not a reference. This variable has its "value" and is placed in the heap

Stacks, and therefore more efficient. To determine the size of storage space for each of the base types.
Base type size minimum maximum wrapper type
Boolean--Boolean
Char 6bit Unicode 0 UNIC Ode 2 (1 Character)
byte 8bit-128 +127 byte
Short 16bit-2 (2)-1 short
int 32bit-2 (2)-1 Integer
Long 64bit-2 (2)-1 long
float 32bit IEEE754 IEEE754 float
Double 64bit IEEE754 IEEE754 double
void---void

Java Integral type

Int 4 bytes (32-bit) -2147483648 ~ 2147483647 (just over 2 billion)
Short 2 bytes (16-bit) -32768 ~ 32767
Long 8 bytes (64-bit) -9223372036854775808 ~ 9223372036854774807
Byte 1 bytes (8-bit) -128 ~ 127



Floating-point types

Float
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.