In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The comparison and problem solving of value type and reference type in Java. Aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
I. description of the problem
A few days ago, Bug appeared because of a requirement. The advanced point is also very advanced, and to put it bluntly, it is also very simple. In fact, it is a very simple difference between value types and reference types when getting started with Java. Just when the development of their own problems, leading to the emergence of minor problems. Fortunately, I suddenly remembered that I had read a blog explaining the problem before, so that I could quickly locate the location of the problem. To prevent the next offense, by the way, take this as a note and put it in your Bug collection.
Comparison of value types and reference types
Everyone should have no problem with this. It's very simple. The value type comparison is the comparison value, and the reference type is the comparison address. For normal operations, we can directly use = = to compare value types, and there will be no problem comparing reference types using equals.
Reference type
/ * Test Integer * / public static void test_Integer () {Integer number_01 = 10; Integer number_02 = 10; System.out.println (number_01.equals (number_02));}
The above test results are obviously true, there is absolutely no problem.
Value Typ
/ * Test int * / public static void test_Int () {int number_01 = 10; int number_02 = 10; System.out.println (number_01 = = number_02);}
The above test results are obviously true, there is absolutely no problem.
III. Problems
But the problem is that in order to prevent the use of null from being replaced by 0 by the system, the Integer type is used for operation and = = is used when comparing. This is very embarrassing, there is no problem at the beginning of the self-test, because the number of records has not been reached. Very happy, submit the code after work, all right. But something embarrassing happened, and the test report appeared in the mailbox.
A situation where there is no problem at first.
/ * Test Integer * / public static void test_Integer () {Integer number_001 = 10; Integer number_002 = 10; System.out.println (number_001 = = number_002);}
Results:
A problem occurs when the record exceeds a certain number
/ * Test Integer * / public static void test_Integer () {Integer number_001 = 128; Integer number_002 = 128; System.out.println (number_001 = = number_002);}
Results:
IV. Solution
On second thought, the problem was quickly identified. Is their own carelessness, lazy use of =, causing the emergence of this problem, when changed to equals can properly go home. No problem at the beginning of the self-test is mainly because of Integer's cache. Pull to the source code of Integer and find that the cache mechanism is used to cache the value of-128 / 127. if you use = = to compare in this value range, the value is compared, so it is thought that there is no problem at first, and then there is a problem after running for a period of time. Equals must be used to complete the comparison when it is not in the value range.
Private static class IntegerCache {
Static final int low =-128,
Static final int high
Static final Integer cache []
Static {
/ / high value may be configured by property
Int h = 127h
String integerCacheHighPropValue = sun.misc.VM.getSavedProperty ("java.lang.Integer.IntegerCache.high")
If (integerCacheHighPropValue! = null) {try {int I = parseInt (integerCacheHighPropValue)
I = Math.max (I, 127)
/ / Maximum array size is Integer.MAX_VALUE h = Math.min (I, Integer.MAX_VALUE-(- low)-1)
} catch (NumberFormatException nfe) {
/ / If the property cannot be parsed into an int, ignore it.
}
} high = h
Cache = new Integer [(high-low) + 1]
Int j = low
For (int k = 0; k
< cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127
}
Private IntegerCache () {
}
}
This is the end of the comparison of Java value types and reference types and problem-solving solutions. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.