Win a copy of Design for the Mind this week in the Design forum!

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

viki Bhardwaj
Ranch Hand
Posts: 74
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.

regards:
Vivek

Joel Christophel
Ranch Hand
Posts: 250
1
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
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
Posts: 48652
56
Did you look in the API documentation? This link and the links in it, explains your question.

fred rosenberger
lowercase baba
Bartender
Posts: 12098
30
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
fred rosenberger wrote:Don't you mean the case of "100" and 2, you want it in binary?

Good catch, sir.