• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain Integer.valueOf("100",2) how it works??

 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!
I have a silly doubt about Integer class static method valueOf(String s,int radix) ,let me show you the sample code ...


here i'm not getting how the valueOf() is working and how the output is producing, i have searched about my this problem but i did't get right answer whatever want to know ,so please Guys help me to clear my doubt ,i would be very thankful for your reply.
thanks in advance..

regards:
Vivek
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first parameter is a String that is formatted like an int, and the second is the base system being used to produce the value of the String. In the case of "100" and 2, you're saying that you want to get the value of 100 in binary. If you know anything about base systems, it's simple math: (1 * 2^2) + (0 * 2^1) + (0 * 2^0) = 4 + 0 + 0 = 4.

Let's do 101 in the base 4 system: (1 * 4^2) + (0 * 4^1) + (1 * 4^0) = 16 + 0 + 1 = 17.

Make sense?
 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:The first parameter is a String that is formatted like an int, and the second is the base system being used to produce the value of the String. In the case of "100" and 4, you're saying that you want to get the value of 100 in binary. If you know anything about base systems, it's simple math: (1 * 2^2) + (0 * 2^1) + (0 * 2^0) = 4 + 0 + 0 = 4.

Let's do 101 in the base 4 system: (1 * 4^2) + (0 * 4^1) + (1 * 4^0) = 16 + 0 + 1 = 17.

Make sense?




Thank you very much Joel, you explained well ,it's much enough to understand the logic behind this, thanks for giving your valuable time to explain this thread.

-vivek
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look in the API documentation? This link and the links in it, explains your question.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote: In the case of "100" and 4, you're saying that you want to get the value of 100 in binary.

Don't you mean the case of "100" and 2, you want it in binary?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Don't you mean the case of "100" and 2, you want it in binary?


Good catch, sir.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic