int and integer Love hate hatred

Source: Internet
Author: User

As the oldest data type in Java, int is everywhere, since Jdk5 was born an integer, never alone.

Why design an integer? What's the difference between it and int?

Integer is the wrapper type of int, is a reference type, and int is a value type.

It is derived from the following characteristics:

(1) When the integer compares the address, int compares the comparison value, how does the integer compare with int? Leave a question, and then discuss it.

(2) An integer initial default value of Null,int is 0 when the property of an object exists, and in the method scope, the variable is to be initialized manually, and if only declared, it is not possible to use the compiler later.

Second, for data conversion considerations

The integer provides two methods, parseint and valueof, to see below:

String s = "1"

int a = Integer.parseint (s)

Integer B = integer.valueof (s)

Integer C = integer.valueof (1)

1) parseint receives an argument of type string, returns an int

2) valueof receives the arguments of the type string and int, returns an integer

PS: Of course, your writing is also set up: int d = integer.valueof (s), the compiler is implicitly unboxing, the Integer is converted to int.

Third, Comparison

1, int and integer

int a = 1;

Integer B = 1;

A==b for True,b advanced unpacking, compared with a is the value

2, Integer and integer

Integer a = new integer (1)

Integer B = 1

Integer C = 1

A==b? False

Solution: A exists in the memory heap, b exists in the constant pool, and the address is different. Any new object will be created, and an Integer B = 1 is implicitly boxed (the ValueOf method is called)

B==c? True

Solution: In memory, there will be a block called constant pool, specifically for the form of B and C, the definition of the object, initialization will go to the constant pool to find 1, if already exist, direct point to 1, if not present, generate object 1 into a constant pool.

Look at this again:

Integer d = 128

Integer e = 128

D==e?false

Why does this not fit the constant pool above? The reason is that a constant pool specifies a range of values: -128~127.

When this range is exceeded, a new object is generated.

3. Compare two integer values

Integer a = new integer (1)

Integer b = new Integer (2)

Can do this: a.intvalue () = = B.intvalue () or a.equals (b)

Iv. obtaining the maximum and minimum values is so simple

int a = Integer.max_value; 2^31-1

int b = Integer.min_value; -2^31

V. Limitations of usage scenarios

1) Many method parameters only receive object types

2) generic type only supports object types

3) Consider that some numeric columns in the database design can be null, use reference types in persistent session objects, and receive null values

Attention to old ginger talk about technology, number: Helojava, or scan the following QR code.


One post per day, technical chicken soup.

int and integer Love hate hatred

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.