How does the Java Boolean wrapper class get instantiated?
Asked Answered
D

6

11

In java, I can write code like this

Boolean b = true ;

And it will work. I now have an object that holds the value "true".

How does that work? Why don't I have to pass the value through a constructor? Like so:

Boolean b = new Boolean( true ) ;

Also, can I make custom classes that I can instantiate in a similar way? If so what is that called?

So that I can do something like this:

Foobar foobar = "Test" ; 

And thus have my own wrapper class.

Thanks

Disclose answered 3/2, 2011 at 19:37 Comment(4)
This is called autoboxing. Googling java autoboxing will provide a lot of information on what you're witnessing.Sendal
The latter part needs more machinery than Java provides. In scala, for example this could probably be done with implicitsAmbrotype
Thanks everyone, I never heard the term "autoboxing" before.Disclose
Unless you have a compelling reason, don't use the Boolean class's constructors at all (even the javadoc BOLDLY says so :). Use valueOf(true), Boolean.TRUE or the autoboxing.Deletion
T
14

No you can't do the latter.

The former is called autoboxing and was introduced in Java v1.5 to auto wrap, primitives in their wrapper counterpart.

The benefit from autoboxing could be clearly been seen when using generics and/or collections:

From the article: J2SE 5.0 in a Nutshell

In the "Autoboxing and Auto-Unboxing of Primitive Types" sample we have:

Before (autoboxing was added)

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, new Integer(42)); 
int total = (list.get(0)).intValue();

After

 ArrayList<Integer> list = new ArrayList<Integer>();
 list.add(0, 42);
 int total = list.get(0);

As you see, the code is clearer.

Just bear in mind the last note on the documentation:

So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.

Temperamental answered 3/2, 2011 at 19:39 Comment(0)
K
3

How does that work?

It's a compiler-feature. The compiler will automatically generate the boxing-operation. What it'll actually do is to generate

Boolean.valueOf(true);

Because this way the existing (immutable) instances Boolean.TRUE and Boolean.FALSE will be used instead of creating a new one.

Keneth answered 3/2, 2011 at 19:44 Comment(0)
J
1

It's the same way that you can make String objects as such:

String s = "foobar"

It's just a perk in Java, really. I'm not sure why you would want to make your own wrapper class, either, considering that any primitive data type already has a predefined wrapper...

Jacobsen answered 3/2, 2011 at 19:40 Comment(1)
I don't have a practical need for it, but I realized I don't know how it works, and without knowing the phrase "autoboxing" there were a few fruitless google searchesDisclose
F
1

This feature was added to Java 1.5 and it's called Autoboxing. This kind of magic available only to primitive values and correspondent wrappers.

And you can't do it yourself in Java. If you still want it, than go for Scala - it's great! Namely you can use feature called implicit conversions. Here is small example for your case:

case class Foobar(value: String)
implicit def convertStringToFoobar(s: String) = Foobar(s)
val foobar: Foobar = "Test";
Frosted answered 3/2, 2011 at 19:45 Comment(0)
C
0

No, it's just compiler magic; it treats these as special cases (known as autoboxing). See e.g. http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html.

Candlemas answered 3/2, 2011 at 19:40 Comment(0)
P
0

It's called autoboxing and is basically just something the compiler does for you. It was added only in Java 5, before that you did have to use new Boolean( true ), or (better) Boolean.TRUE.

And no, you cannot have it for your own classes, it's only done for the primitive wrapper classes.

Pathogenesis answered 3/2, 2011 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.