Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default Best Practice wrt Boxing / Unboxing

    Some people say that everything should be boxed.
    So instead of using primatives like int, you should always use their wrappers.
    But suppose I don't always want to copy references of my primatives into Hashtables and PriorityQueues etc.
    I might need to store some integers in a big 3 dimensional array like this:

    int[][][] arr = new int[64][64][64];

    Surely using the Integer object for this would be madness, wouldn't it create a lot of overhead?
    Unless that is if the compiller was able to automatically convert those Integer objects to ints, but does this really happen and if so how does it make that decision? Can I really be sure it will make the right choice?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Some people say that everything should be boxed.

    Which people? And for what reasons?

    One could make a case for a smalltalk-like "everything is an object" language (Sun bought LongView Technologies in 1997). But the Java we have is the Java we have and I can't see the gain in just not using some of the types it provides.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Note that the Integer class also caches some values for Integer objects; i.e. the value [0, 127] are kept in a cache and the method Integer.valueOf(int i) returns the same object over and over again for ints in the mentioned range. The choice what to autobox and to what wrapper type may not be intuitive; have a look at this cryptic example:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
     
    public class Autoboxing {
    
    	public static void main(String[] args) {
    
    		Object foo = new Long(0xcafebabedeadbeefL);
    		List<Object> bars = new ArrayList<Object>();
            
    		bars.add(true ? (Long)foo : (Long)foo);
    		bars.add(true ? (Long)foo : (Number)foo);
    		bars.add(true ? (Long)foo : (Double)foo);
    		bars.add(true ? (Long)foo : ((Long)foo).longValue());
    
    		System.out.print("==    :");
    		for (Object bar : bars)
    			System.out.print(" "+(foo == bar));
    		System.out.println();
    
    		System.out.print("equals:");
    		for (Object bar : bars)
    			 System.out.print(" "+foo.equals(bar));
    		System.out.println();
    	}
    }
    I find autoboxing a mixed blessing. In SmallTalk no primitives exist so this little problem doesn't exist in that language.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. GUI - is this bad practice?
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-01-2011, 01:40 PM
  2. Combo box trouble turning into a boxing match with java
    By Embercloud in forum New To Java
    Replies: 1
    Last Post: 01-05-2011, 04:46 PM
  3. Best practice for packaging add-ons
    By Goose in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2009, 11:51 PM

Tags for this Thread

Posting Permissions

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