This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parseInt()

 
sriram gupta
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled the following code and the output is as follows?
Can someone explain why is it so?
__________________________________________________________

public class BinDec{
public static void main(String argv[]){
System.out.println(Integer.parseInt("011",3));
System.out.println(Integer.toString(64,2));
}
}
__________________________________________________________

If you compile and run this program the output will be
4
1000000
CAN SOMEONE EXPLAIN WHY?? if anybody can tell me how do we use parseInt() it will be very helpful...
thanks in advance...
 
Cherry Sta. Romana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the first statement
System.out.println(Integer.parseInt("011",3));
The output is 4 because you specified a radix/base of 3,
011 = 1*3 + 1 = 4
In the second case
System.out.println(Integer.toString(64,2));
The output is 1000000 because you specified base 2 and 64 in binary is 1000000.
If you want to simply convert to integer, do not put any radix.
I hope this helps.


 
sriram gupta
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi cherry,
thanks for ur help.. cna u tell me where i can read more about this.... i have R&H but i didn't find anything there and i am appearing for paper on this monday...
bye
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sriram,
The best place to get information on the Math and wrappper classes is straight from the API.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited August 14, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic