posted 9 years ago
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.
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.
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
posted 9 years ago
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
See: http://java.sun.com/j2se/1.3/docs/api/java/lang/Boolean.html
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rajshekhar Paul
Ranch Hand
Posts: 140
posted 9 years ago
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.
The output will be false.
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
posted 9 years ago
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");
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");
SCJP 6
Rajshekhar Paul
Ranch Hand
Posts: 140
posted 9 years ago
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".
- 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".
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Punit Singh
Ranch Hand
Posts: 952
Rajshekhar Paul
Ranch Hand
Posts: 140
posted 9 years ago
It's in here.
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.

It is sorta covered in the JavaRanch Style Guide. |