Results 1 to 5 of 5
- 09-17-2012, 09:21 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
can you guess the output of this program?
Java Code:public class Init{ public static void main(String[] args){ Integer a =10; Integer b = 10; if(a==b)System.out.println("same object"); Integer c = 1000; Integer d = 1000; if(c!=d)System.out.println("not same object"); } }
- 09-17-2012, 09:47 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 787
- Rep Power
- 9
Re: can you guess the output of this program?
?
-Djava.lang.Integer.IntegerCache.high=1000 and your statement isn`t longer correct :D
- 09-17-2012, 09:52 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,641
- Rep Power
- 10
Re: can you guess the output of this program?
See the JLS
Chapter*5.*Conversions and Promotions
to quote:
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2Last edited by doWhile; 09-17-2012 at 09:54 PM.
- 09-18-2012, 12:37 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: can you guess the output of this program?
my bad. still weird though and figured i would share
- 09-18-2012, 09:39 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
Re: can you guess the output of this program?
Java Code:Integer a = 10; ... Integer c = 1000;
As part of autoboxing it essentially replaces the int with:
Java Code:Integer a = Integer.valueOf(10);
Java Code:public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; // <-- get it from our cache. else return new Integer(i); }
Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
School program GUI output problem
By nanderson05 in forum New To JavaReplies: 1Last Post: 06-08-2012, 01:54 AM -
Output of the program
By Sheenu Gupta in forum New To JavaReplies: 1Last Post: 07-25-2011, 08:32 AM -
Need help with input/output program error
By stefan2892 in forum New To JavaReplies: 2Last Post: 02-07-2011, 07:57 PM -
Help about Guess the Numbers Program in java
By macfrik in forum New To JavaReplies: 6Last Post: 03-25-2009, 03:59 AM -
Program can run but output all null
By matt_well in forum New To JavaReplies: 15Last Post: 07-24-2008, 08:48 AM
Bookmarks