• 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

Wrapper class Double - doubt

 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

As I'm studying Wrapper Classes, I have come to know that the functionality of Long and Double are different in the sense of taking String argument in their constructors. Below is the difference.

new Long("2f");// This will throw Runtime Exception, NumberFormatException
// Also 2F, 2l, 2L, 2d, 2D won't work
new Double("2f");// This will work at Runtime
// This will also work for 2F, 2d, 2D but NOT for 2l and 2L

Why the java compiler is designed so confusingly. Is it a really confusion with java compiler or me? Somebody please explain this.

Thanks,
Narendranath
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c'mon yar.. how can u expect a long constructor to take a string representing float num and create a long num? also the same applies for the double constructor.
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is Fine. But atleast Long constructor should take 2l or 2L. Right!

Regards,
Narendranath
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
public Long(String s) constructs a newly allocated Long object that represents the long value indicated by the String parameter. The string is converted to a long value in exactly the manner used by the parseLong method for radix 10.

In parseLong doc:
...
Note that neither the character L ('\u004C') nor l ('\u006C') is permitted to appear at the end of the string as a type indicator, as would be permitted in Java programming language source code - except that either L or l may appear as a digit for a radix greater than 22.

....
 
A tiny monkey bit me and I got tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic