Friday, 22 August 2014

parseInt Vs valueOf

  1. parseInt method parses the given string argument to signed decimal value.
    ValueOf method returns an Integer instance (Not a primitive value) representing the specified int value.
  1. ParseInt method available in below forms.
      public static int parseInt(String s)
      public static int parseInt(String s, int radix)
        valueOf method available in below forms.
              public static Integer valueOf(int I)
              public static Integer valueOf(String s)
              public static Integer valueOf(String s, int radix)

public class ParseInt {
    public static void main(String args[]){
        String str1 = "101";
        
        int a = Integer.parseInt(str1);
        int b = Integer.parseInt(str1, 16);
        
        Integer c = Integer.valueOf(str1);
        Integer d = Integer.valueOf(a);
        Integer e = Integer.valueOf(str1, 16);
        
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
        System.out.println("e = " + e);
    }
}

Output
a = 101
b = 257
c = 101
d = 101
e = 257






                                                             Home

No comments:

Post a Comment