Results 1 to 4 of 4
- 09-12-2011, 11:08 PM #1
- 09-12-2011, 11:39 PM #2
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.
- 09-12-2011, 11:46 PM #3
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:
The autoboxing will change true to Boolean.True.Java Code:ArrayList<Boolean> list= new ArrayList<Boolean> (); list.add (true);
- 09-13-2011, 12:01 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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:
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.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; }
I wrote this up real quickly to illustrate:
I'll also probably post up more soon; and hopefully others will enlighten me if I am giving inadequate advice.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); } } }Last edited by sunde887; 09-13-2011 at 12:18 AM.
Similar Threads
-
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 09:04 AM -
how to balance true and false instances per id ?
By aneuryzma in forum New To JavaReplies: 1Last Post: 03-27-2011, 02:35 PM -
my simple boolean code keeps saying true
By shazakala in forum New To JavaReplies: 8Last Post: 03-27-2011, 10:30 AM -
Boolean always true?? What am I doing wrong?
By Forty0ztoFreedom in forum New To JavaReplies: 4Last Post: 03-02-2011, 06:13 PM -
Prime Number - true , false
By pinkdreammsss in forum Java AppletsReplies: 11Last Post: 05-04-2010, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks