How Integer.ParseInt Method Works In Java

Integer.ParseInt is very commonly used method in java.The parameter to this method will be String. So It works on String only.But what kind of String??
Those which can represent ASCII values for digit(0-...9). Remember Integer.ParseInt only works on string representation of numeric value but not something like "five".

int x=Integer.parseInt("9");
int y=Integer.parseInt("nine");
//invalid compilation error

Let us check the syntax:

  • static int parseInt(String s) // here s is the string representation of Numeric value
  • static int parseInt(String s, int radix) //This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.
Let us check out one example:

public class parseTest{ 

   public static void main(String args[]){
      int x =Integer.parseInt("5");
      double c = Double.parseDouble("8");
      int b = Integer.parseInt("444",16);


How Integer.ParseInt Method Works In Java How Integer.ParseInt Method Works In Java Reviewed by Animesh Chatterjee on May 20, 2013 Rating: 5

No comments:

Powered by Blogger.