This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Doubt in Boolean wrapper object creation  RSS feed

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K.B. book -> Chapter 3: Assignments -> Certification Objective ´┐ŻUsing Wrapper Classes and Boxing (Exam Objective 3.1)

The lines mentioned below are the above section.

"For the exam you need to understand the three most common approaches for creating wrapper objects. Some approaches take a String representation of a primitive as an argument. Those that take a String throw NumberFormatException if the String provided cannot be parsed into the appropriate primitive. For example "two" can't be parsed into "2"."

Now my question is whether there is any way to get the NumberFormatException in case of creating a Boolean wrapper object. It seems silly though, I need others' opinion in this regard.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope because none of the functions in the class Boolean throw a NFE.
See: http://java.sun.com/j2se/1.3/docs/api/java/lang/Boolean.html
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had raised this question because that statement in the book talks about all wrapper classes in general. It seemed to me that it's not always true. If "23" is passed to the Boolean wrapper constructor, the Boolean wrapper object will have a value of boolean false though the constructor cannot parse "23" to boolean primitive false.



The output will be false.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice info Raj.
From your question I have one more doubt.

Boolean b=new Boolean("false");
System.out.println(b);//1----> prints false
System.out.printf("%b","false");//2----> prints true

Why line 2 prints true.
Even this line prints true, what is the funda behind that? while line 1
prints false.
System.out.printf("%b","12345");
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the printf() method while doing the boolean conversion,
- if null value is passed, outcome will be "false"
- if boolean or Boolean is passed than outcome will be String.valueOf() for the passed value
- if "#" flag is passed, FormatFlagsConversionMismatchException will be thrown
- anything other than the above two scenarios, the outcome will be "true"

In your case, you have passed "12345" which falls into the last category, so, result is "true".
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("\"#\": %b\n","#");

this is returning:
"#": true

where is the documentation for this behavior?
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's in here.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok
# is flag here, I thought it is argument.

System.out.printf("\"#\": %#b\n",'#');
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!