Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of judging equality between two integer data of java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the method of judging the equality of two integer data in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problem case

Let's take a simpler example.

Public static void main (String [] args) {for (int I = 0; I < 150; iSum +) {Integer a = i; Integer b = I; System.out.println (I + "" + (a = = b));}}

The value of I is from 0 to 150, the values of an and b are equal in each cycle, and the output a = = b. Running result:

0 true

1 true

2 true

3 true

...

126 true

127 true

128 false

129 false

130 false

...

From 128, an and b are no longer equal.

Cause analysis

First of all, review the automatic packing. For the following line of code

Integer a = 1

The variable an is of type Integer and 1 is of type int, and there is no inheritance relationship between Integer and int. According to the general handling method of Java, this line of code should report an error.

However, because of the autoboxing mechanism, when a variable of type Integer is assigned a value of type int, Java automatically converts the type of int to type Integer, that is,

Integer a = Integer.valueOf (1)

The valueOf () method returns an Integer value and assigns it to the variable a. This is the automatic packing of int.

Let's take a look at the initial example:

Public static void main (String [] args) {for (int I = 0; I < 150; iSum +) {Integer a = i; Integer b = I; System.out.println (I + "" + (a = = b));}}

In each loop, Integer a = I and Integer b = I trigger auto-boxing, and auto-boxing converts int to the Integer type value and returns; we know that the two new objects in Java will return fasle anyway because of different instances. such as

New Integer (1) = = new Integer (1)

False will be returned.

Then the variables an and b generated by the automatic boxing of Integer a = I and Integer b = I should not be the same object, and the result of = = should be false. False is easy to understand above 128, but why does it return true from 0 to 127? = = the only case in which true is returned is that the two objects compared are the same object, so print out the memory addresses of both an and b in the example:

For (int ionome0witi = IntegerCache.low & & I = 127;} private IntegerCache () {}}

Sure enough, direct Integer type variables of-128to127are generated at one time in its static block and stored in cache []. For int types between-128to127, the same Integer type object is returned.

The whole process is that when Integer.class loads (the Java virtual machine starts), the static block of its internal type IntegerCache starts to execute, instantiates and temporarily stores Integer type objects with values between-128and 127s. When the auto-boxing int value is between-128and 127, the Integer type object temporarily stored in IntegerCache is returned directly.

Solution method

Since our purpose is to compare whether the values are equal, not to judge whether they are the same object, and automatic boxing does not guarantee that the Integer of the same value must be the same object or not, then instead of using = =, just use equals (). In fact, Integer overrides the equals () method to directly compare whether the values of objects are equal.

For (int I = 0; I < 150; iSum +) {Integer a = i; Integer b = I; System.out.println (I + "" + (a.equals (b);} / so the return values are all true. Private final int value;public boolean equals (Object obj) {if (obj instanceof Integer) {return value = = ((Integer) obj) .intValue ();} return false;} public int intValue () {return value;} remarks

Not only the other 7 basic types in int,Java can be boxed and unboxed automatically, but caching is also used. See the following table:

Basic type packing type value range byteByte-128 ~ 127float short-2 ^ 15 ~ 1 is-128 ~ 127 intInteger2 ^ 31 ~ (2 ^ 31-1) is-128 ~ 127 long long-2 ^ 63 ~ (2 ^ 63-1) is-128 long 127 float float-No-- doubleDouble-- No-- booleanBooleantrue, false is true, falsecharCharacter\ u0000 ~\ uffff

This is the end of the content of "what is the method of judging equality between two integer data of java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report