Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

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

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

Marshal
Posts: 57499
175
Did you look in the API documentation? This link and the links in it, explains your question.

lowercase baba
Bartender
Posts: 12602
50

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.

 God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS