Learning about strings in Java (iv) Basic data types Study and use of wrapper classes

Source: Internet
Author: User

Basic data type Object wrapper class

According to the Java object-oriented principle, everything is an object, which includes the basic data types.

BYTE byte

Short Short

Integer int

Long Long

Boolean Boolean

float float

Double Double

Character Char

Next, take int as an example to explain:

In the Java.lang package, the integer class

The integer class wraps a primitive type int value in the object. An object of type integer contains a field of type int.

has a fixed maximum minimum value.

The most common role of the basic data type wrapper class

is to convert between the base data type and the string type.

The base data type is converted to a string.

Basic data type + "";

Base data type. toString (base data type value)

such as: Integer.tostring (34);//Will 34

The string is converted to the base data type.

Everyday applications: We enter numbers in the text box when we surf the Internet. Like age 24. The data type in the text box is full of strings. Then we need to determine whether the age of input is greater than 20 years. This is a mathematical operation. So we need to turn the string 24 into an integer 24.

There are methods in the integer class

The Static int parseint (string s) converts the string to the int type.

Other basic data types also go like this.

Two Special:

Static Boolean Parseboolean (string s): Converts a string to a Boolean value.

Character no method of Parsexxxxx ()

If there are other non-numeric characters in the passed-in string, such as ABCCFF, the number format exception is reported.

Java.lang.NumberFormatException.

That is, you must pass in a numeric format string.

The basic data wrapper class encapsulates many methods, many of which are static, many of which are methods of manipulating data, which are convenient for us to use.

Decimal into other binary

Tobinarystring ();

Tohexstring ();

Tooctalstring ();

Other binary into decimal:

parseint (String Str,radix);

A numeric format exception is also reported if a given range of the input is exceeded.

The basic data type wrapper class string into the basic data type, if you do not adopt the static parsexxxx () method, you can also use the first string or integer encapsulated into the corresponding object, and then call the Xxxvalue () method through the object can also achieve the purpose.

Basic data type Object wrapper class new attribute.

Here's a little exercise:

Integer x = new Integer ("123");

Integer y = new integer (123);

System.out.println (x==y);

Sytem.out.println (X.equals (y));

Results

False

True

Because X, Y is a two different object.

The integer class is a duplicate of the Equals () method in the parent class, which compares the values.

New features that occur after the JDK1.5 version.

1.5 before: integer x = new Integer (4);

1.5 later; Integer x = 4;x is a reference data type. The 4 on the right is the object, because the class type variable definitely points to the object. This is automatic boxing. In fact, this 4 = new Integer (4), but this action does not have to be explicitly done, the JDK implicitly help us do. This is simplifying writing.

At the same time, this x can be calculated directly.

X = x+2,//x+2x should first become an integer before the operation. The automatic unpacking is carried out. becomes an int type. and 2 for addition operations. The result is then automatically boxed to X.

So how does it break the box?

X is an object, and what you do here is actually x.intvalue ().

It is important to note that if you want to use the new feature after version 1.5. , you need to be aware of the value range of x, and the integer object x will have one more null value: null. A null pointer exception appears. Make an empty operation.

Small case:

Integer m = 128;

Integer n = 128;

Integer a = 127;

Integer B = 127;

M==n false

A==b true

The first one is false or because M and n are different objects.

The second true is because both A and B point to the same integer object. Because when the value is within the byte range, for a new attribute, if the value already exists, no new space will be opened.

This new feature, the interview may ask, development is uncommon. Generally Equlas ().

Learning about strings in Java (iv) Basic data types Study and use of wrapper classes

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.