Results 1 to 5 of 5
  1. #1
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default 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");
    	}
    }
    apparently when a Boolean, Byte, Char, Short, Long, or Integer wrapper classes hold data that's a byte or smaller == is no longer a comparison between classes its between there primitive values but this doesn't apply to Double ha ha ha How in the word am I suppose to remember this stuff? No wonder the SCJP is supposed to be one of the hardest exams in the IT industry. Anyone have any interesting memorization strategies.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: can you guess the output of this program?

    ?
    -Djava.lang.Integer.IntegerCache.high=1000 and your statement isn`t longer correct :D

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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 == r2
    The assumption it compares primitives is incorrect - it compares two identical (cached) objects
    Last edited by doWhile; 09-17-2012 at 09:54 PM.

  4. #4
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: can you guess the output of this program?

    my bad. still weird though and figured i would share

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: can you guess the output of this program?

    Java Code:
    Integer a = 10;
    ...
    Integer c = 1000;
    In both those cases the compiler is autboxing the primitive on the right.
    As part of autoboxing it essentially replaces the int with:
    Java Code:
    Integer a = Integer.valueOf(10);
    valueOf() looks like this:
    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);
        }
    which is where the magic that eRaaa and doWhile talk about occurs.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. School program GUI output problem
    By nanderson05 in forum New To Java
    Replies: 1
    Last Post: 06-08-2012, 01:54 AM
  2. Output of the program
    By Sheenu Gupta in forum New To Java
    Replies: 1
    Last Post: 07-25-2011, 08:32 AM
  3. Need help with input/output program error
    By stefan2892 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 07:57 PM
  4. Help about Guess the Numbers Program in java
    By macfrik in forum New To Java
    Replies: 6
    Last Post: 03-25-2009, 03:59 AM
  5. Program can run but output all null
    By matt_well in forum New To Java
    Replies: 15
    Last Post: 07-24-2008, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •