Java wrapper class

Source: Internet
Author: User
Tags static class

What is the difference between 1.ValueOf and strong turn?

Case1: What needs to be emphasized is the string.valueof () method, when the argument is the type is object, and the value is null when he handles the way

public static String valueOf (Object obj) {        return (obj = = null)? "Null": Obj.tostring ();          If object is null it will get the string ' null ', the program does not error but the logic error, so this time to use a strong go}    

CASE2: The handling of valueof (object) of the basic wrapper type (Long,integer, etc.) is different from string, and object is NULL to return NULL instead of ' null '

Summary: Encounter an object to a string as far as possible to turn

When you encounter an object to the basic packing type, it is best to go strong. If necessary, you can use instanceof to determine if the type is modified.

2. Packaging type of unboxing

Boxing: the base type is converted into a wrapper type. Unpacking: package type converted to basic type

The valueof (int) method of the integer is automatically called when boxing. In the case of unpacking the automatic call is an integer Intvalue method, the other wrapper class similar

3. The type of packaging = = Judgment

1. The valueof () method is called when the wrapper type is first created.

For example, Integer i = 100; Equivalent to integer.valueof (i);

★ To know that there is a Integercache static class in the Integer wrapper class, the virtual machine loads the integer object to store the value of [-128,127] in the integer cache[]

The 2,valueof method first determines whether the int value is in the Integercache cache if the data in the cache pool is returned, and if not, returns an integer object

So Integer x = 200; Integer y = 200; y = = X is false;

Special cases

1.Int x = 100; Integer y = 100; x = = Y is True

Reason = = can only be used in numeric type judgments or object type judgments

Determine whether the values are equal. The object determines whether the address is consistent.

If the value is a numeric wrapper type, the wrapper class is disassembled, and the value is judged.

2. If the wrapper type is compared to equals, the type is judged first, and if the type inconsistency returns false directly, the content judgment will not continue.

Integer x = 100; Short s = 100; X.equlas (s); Value is False

The data originates from the network, if has the infringement contact Delete (^^ ゞ!

Java wrapper class

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.