Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in Boolean wrapper object creation  RSS feed

 
Rajshekhar Paul
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.
 
Wouter Oet
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.
 
Punit Singh
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",'#');
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!