• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Double wrapper object.

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As we all know the two ways to create Double wrapper object.
Those are,
1) Double dbl = new Double(4.5);
2) Double dbl1 = new Double("4.5");
Below one will give the NumberFormatException.
Double dbl2 = new Double("abcd");
Till now every thing is fine, now please go through the below one,
Double dbl2 = new Double(null); Will it give NullPointerException or NumberFormatException. It is compiling fine, only run time error. My doubt is how is it compiling, there is no constructor to take null reference. Why is ot giving compiler error.
What is rationale behind the answer? ( Please give me your exaplanation to support). I feel it should be NumberFormatException.

Thanks,
Narasimha.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
narasimha, the Double class constructor signatures are:
1) Double(double value)
2) Double(String s)
As you can see the second constructor accepts String as a parameter, and as we all know String are objects in java, so it is totally legal to send the null reference to them, this is why the compiler doesn't complain about the null argument.
Trying to access an object that is not there will always throw a NullPointerException, when you try to send "ABC" to the constructor, it will not throw NullPointerException because the object is there, it is simply not of the type expected.
Hope this helps.
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vicken,
Thanks for your reply. Now i got,
String str = "4.5"; // 1
Double dbl = new Double(str); // 2
At line 2, in the constructor, we can pass any valid string object, for successful compilation and will check the validity of string at run time.
Now look into the below one,
Double dbl1 = new Double(null); //3
Here at line 3, i am passing one object (which hold null reference) and as we all know null can be substituted for any reference, here we are substituting null for string reference, hence no compilation problem. But when we can to run time, it should check the validatity of the string, for this it should get the string value, but when access any thing from null reference you will get NullPointerException, before throwing the NumberFormatException.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic