This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boolean instead of boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boolean instead of boolean" Watch "Boolean instead of boolean" New topic

Boolean instead of boolean

Dmitryi Neverov

Joined: Nov 28, 2005
Posts: 18
Explain please why

compiles and works fine (prints "It's work!").
[ January 17, 2006: Message edited by: Dmitryi Neverov ]

SCJP - 98%<br />SCWCD - ...
Prash Gali

Joined: Dec 20, 2005
Posts: 22
The reason why the code works is because of Autoboxing feature of java 5.0.

new Boolean("TRuE") - creates a new Boolean object and when used with if condition, autoboxing takes place and the object is automatically converted to a primitive boolean value, which in this case is true and hence the following printf statement is executed.

Ranchers correct me if i am wrong.

Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

Does this compile ? I don't think it will with Java 1.4 or less.
Are you using Java 1.5 ?
In this case, the Boolean class will be unboxed to a boolean primitive, which value is set to "true".

[My Blog]
All roads lead to JavaRanch
Dmitryi Neverov

Joined: Nov 28, 2005
Posts: 18
Yes, I'm using 1.5 jdk, but I'm preparing for 310-035, and didn't know about this behavior of 1.5 jdk.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

If you're preparing for 310-035, you'd better use JDK1.4 then
Good luck.
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

Its legal.
true if the string argument is not null and is equal, ignoring case, to the string "true".
Alex Khvatov
Ranch Hand

Joined: Nov 07, 2005
Posts: 36
I agree with Satou, you'd better off using JDK 1.4. Tiger (1.5 or 5.0) is a new release that has major changes introduced. In 1.5 some things would give warnings if used the 1.4 way.

SCJP 5<br />SCWCD 5
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
Prash is right. This will work only with JDK 1.5, wont compile with 1.4 and it works coz of auto-unboxing feature

SCJP 1.5<br />SCWCD 1.4
I agree. Here's the link:
subject: Boolean instead of boolean
Similar Threads
Boolean unboxing.
Boolean mapping
'new' acceptable with primitive? - expected compiler error
sheer mind-searing frustration - arghh