Results 1 to 4 of 4
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    10

    Default Boolean.True and Boolean.False, why do some people use these?

    Why not just use the primitive true and false?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Boolean.True and Boolean.False, why do some people use these?

    If you need to store an object in a collection, use a wrapper class.

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    10

    Default Re: Boolean.True and Boolean.False, why do some people use these?

    you mean primitive? There is autoboxing/autounboxing, so it should not be needed.
    In this case, it should not be necessary to use the wrapper class.
    We can make like this:

    Java Code:
    		ArrayList<Boolean> list= new ArrayList<Boolean> ();
    		list.add (true);
    The autoboxing will change true to Boolean.True.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,068
    Blog Entries
    3
    Rep Power
    14

    Default Re: Boolean.True and Boolean.False, why do some people use these?

    I believe auto unboxing is worth avoiding when possible. If you know you plan to add it to some collection I believe it takes less space to use the Boolean.TRUE, or Boolean.FALSE.

    Here is some useful information: http://stackoverflow.com/questions/4...d-true-in-java

    It seems that the Boolean.TRUE, and Boolean.FALSE are singletons, so there will only be one instance, rather than 3243146789126 primitives.

    Here is some of the code from the source:
    Java Code:
     /** 
         * The <code>Boolean</code> object corresponding to the primitive 
         * value <code>true</code>. 
         */
        public static final Boolean TRUE = new Boolean(true);
    
        /** 
         * The <code>Boolean</code> object corresponding to the primitive 
         * value <code>false</code>. 
         */
        public static final Boolean FALSE = new Boolean(false);
    
        /**
         * The Class object representing the primitive type boolean.
         *
         * @since   JDK1.1
         */
        public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
    
        /**
         * The value of the Boolean.
         *
         * @serial
         */
        private final boolean value;
    
        /** use serialVersionUID from JDK 1.0.2 for interoperability */
        private static final long serialVersionUID = -3665804199014368530L;
    
        /**
         * Allocates a <code>Boolean</code> object representing the 
         * <code>value</code> argument. 
         *
         * <p><b>Note: It is rarely appropriate to use this constructor.
         * Unless a <i>new</i> instance is required, the static factory
         * {@link #valueOf(boolean)} is generally a better choice. It is
         * likely to yield significantly better space and time performance.</b>
         * 
         * @param   value   the value of the <code>Boolean</code>.
         */
        public Boolean(boolean value) {
    	this.value = value;
        }
    Basically the Boolean.TRUE, and Boolean.FALSE are statically declared and initialized and it appears you get the same instance from every call to it.

    I wrote this up real quickly to illustrate:

    Java Code:
    import java.util.*;
    
    public class Bool{
    	public static final Bool TRUE = new Bool(true);
    	public static final Bool FALSE = new Bool(false);
    	
    	private boolean value;
    	
    	public Bool(boolean value){
    		this.value = value;
    		System.out.println("Initializing bool: " + value);
    	}
    	public static void main(String[] args){
    		List<Bool> bools = new ArrayList<Bool>();
    		for(int i = 0; i < 100; ++i){
    			bools.add(Bool.TRUE);
    			bools.add(Bool.FALSE);
    		}
    	}
    }
    I'll also probably post up more soon; and hopefully others will enlighten me if I am giving inadequate advice.
    Last edited by sunde887; 09-13-2011 at 12:18 AM.

Similar Threads

  1. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  2. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM
  3. my simple boolean code keeps saying true
    By shazakala in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 10:30 AM
  4. Boolean always true?? What am I doing wrong?
    By Forty0ztoFreedom in forum New To Java
    Replies: 4
    Last Post: 03-02-2011, 06:13 PM
  5. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 02:49 PM

Posting Permissions

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