• 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

Integer.valueOf("11111111111111111111111110000000",2) ; // NFE , why?

 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Consider the method call to create a Wrapper :


Here, the string i passed to valueOf() contains 25 1's and 7 0's , which means -128.
When it is executed , it is giving NumberFormatException exception. Why it is so ?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the string i passed to valueOf() contains 25 1's and 7 0's , which means -128.



Wrong assumption. Read the documentation.

... except that the first character may be an ASCII minus sign ...



The String you posted represents the integer value 4294967168, which is greater than Integer.MAX_VALUE.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Long instead of Integer.
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

... except that the first character may be an ASCII minus sign ...



The String you posted represents the integer value 4294967168, which is greater than Integer.MAX_VALUE.



Sir , here what is meant by "except that the first character may be an ASCII minus sign" and how the given string in my post is evaluated to 4294967168 ?

and how -128 value ( in binary ) can be passed to valueOf("",2) method ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashanth Patha wrote:
Sir , here what is meant by "except that the first character may be an ASCII minus sign" and how the given string in my post is evaluated to 4294967168 ?

and how -128 value ( in binary ) can be passed to valueOf("",2) method ?



You are overthinking here -- the valueOf() method that takes a radix simply does base conversion. It doesn't actually use the internal binary representation for base 2, nor does it understand twos complement.

Negative 128 in base two, as a string is simply .... "-10000000".

Henry
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your reply and i got the concept now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic