Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Recently,I want change some arguments of JVM so that I can change the cache for Integer value , but some problems have appeared:


Sourse code:


Java
public class test {

	public static void main(String[] args) {
		Integer i1=300;
		Integer i2=300;
		System.out.println(i1==i2);
	}

}
If I use "java test" to run this procejure , and normally it will print "false".

Details:

1:javac test.java
2:jvva -server test.java
3:java -Djava.lang.Integer.IntegerCache.high=200 test
4:java -server -Djava.lang.Integer.IntegerCache.high=200 test
5:java -XX:AutoBoxCacheMax=200 test
6:java -server -XX:AutoBoxCacheMax=200 test
7:java -server -XX:+AggressiveOpts test

And when I changed all the arguments and it should print "false" , but to my surprise it should be "true" . And I know
if i1 is 100 and i2 is 100 , then I use "java test" to run this procejure getting the result of "true" cuz they are
between -128~127 . And if i1 is 200 and i2 is 200 , then the result will be "false" cuz they are beyond the rainge of
-128~127 , however when I change the final varible high to 200 , the result will be "true" . So all the conclusion
above you think right , ok ? But now , I have made a change again , i1 is 300 and i2 is 300 , and I just keep the value
of final varible high(200) , and why it should be "true" at last ? Cuz i1 and i2 are beyond the rainge of -128~200 , so
what is the problem with it ?
Posted
Updated 21-Aug-12 4:46am
v2
Comments
[no name] 21-Aug-12 10:49am    
Perhaps you are confusing the limit of a byte and the limit of a integer? http://www.leepoint.net/notes-java/data/basic_types/21integers.html
mqslll 21-Aug-12 11:18am    
I just confused why I had changed the IntegerCache.high to 200 , it still print"true", was it the JVM problem or any other? Do you think my procejures of configure JVM arguments are right ?
[no name] 21-Aug-12 11:21am    
I think that you are confused about what the IntegerCache is, see http://tech-read.com/2010/02/03/integercache-in-jdk1-5/ for an explanation.
mqslll 22-Aug-12 10:57am    
Many thanks !

1 solution

In your example, you are assigning both objects to the same instance. As with String, if you use a literal it will ALWAYS be the same instance. If you used new Integer(300) it would guarantee unique instances.
 
Share this answer
 
Comments
Espen Harlinn 21-Aug-12 13:38pm    
Vacation is over then? BTW a 5
Nagy Vilmos 21-Aug-12 13:41pm    
Back in lovely London looking for a job.
Espen Harlinn 22-Aug-12 5:56am    
There seems to be a few c++ jobs available
Nagy Vilmos 22-Aug-12 5:58am    
My C++ coding in anger ended about 15 years ago. Now I prefer managed code - C# or Java. Heck, I'd go back to VB before returning to C++.
Espen Harlinn 22-Aug-12 6:06am    
There is a dearth of good C++ developers, while there seems to be no shortage when it comes to c# and vb developers. Wages seems to be significantly better too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900