Integer valueOf() Method in Java

  1. The java.lang.Integer.valueOf(int a) is an inbuilt method which is used to return an Integer instance representing the specified int value a.

    Syntax :

    public static Integer valueOf(int a)

    Parameters : The method accepts a single parameter a of integer type representing the parameter whose Integer instance is to be returned.



    Return Value : The method returns an Integer instance representing a.

    Examples :

    Input: a = 65
    Output: 65
    
    Input: a = -985
    Output: -985
    

    Below programs illustrate the java.lang.Integer.valueOf(int a) method.
    Program 1: For a positive number.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java praogram to illustrate the
    // java.lang.Integer.valueOf(int a)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            Integer obj = new Integer(10);
      
            // Returns an Integer instance  
            // representing the specified int value
            System.out.println("Output Value = "
                                obj.valueOf(85));
        }
    }

    chevron_right

    
    

    Output:

    Output Value = 85
    

    Program 2: For a negative number.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java praogram to illustrate the
    // java.lang.Integer.valueOf(int a)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            Integer obj = new Integer(10);
      
            // It will return a Integer instance 
            // representing the specified int value
            System.out.println("Output Value = "
                                obj.valueOf(-9185));
        }
    }

    chevron_right

    
    

    Output:

    Output Value = -9185
    
  2. The java.lang.Integer.valueOf(String str) is an inbuilt method which is used to return an Integer object, holding the value of the specified String str.

    Syntax:

    public static Integer valueOf(String str)

    Parameters: This method accepts a single parameter str of String type that is to be parsed.

    Return Value: The method returns an Integer object holding the value represented by the string argument.



    Examples:

    Input: str = "65"
    Output: 65
    
    Input: str = "-452"
    Output: 452
    

    Below programs illustrate the java.lang.Integer.valueOf(String str) method:
    Program 1: For a positive number.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java praogram to illustrate the
    // java.lang.Integer.valueOf(String str)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            Integer obj = new Integer(8);
      
            String str = "424";
            // It will return  a Integer instance
            // representing  the specified string
            System.out.println("Integer Value = "
                                obj.valueOf(str));
        }
    }

    chevron_right

    
    

    Output:

    Integer Value = 424
    

    Program 2: For a negative number.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java praogram to illustrate the
    // java.lang.Integer.valueOf(String str)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            Integer obj = new Integer(8);
      
            String str = "-6156";
            // It will return  a Integer instance
            // representing the specified string
            System.out.println("Output Value = "
                                obj.valueOf(str));
        }
    }

    chevron_right

    
    

    Output:

    Output Value = -6156
    
  3. The java.lang.Integer.valueOf(String s, int radix) is an inbuilt method which returns an Integer object, holding the value extracted from the specified String when parsed with the base given by the second argument.

    Syntax :

    public static Integer valueOf(String str, int base)

    Parameter: The method accepts two parameters:

    • str: This is of String type which is to be parsed.
    • base This is of Integer type and refers to the base to be used to interpret str.

    Return Value : The method returns an Integer object holding the value represented by the string argument in the specified base or radix.

    Examples:

    Input: str = "1101"
           base = 2
    Output: 13
    
    Input: str = "101"
           base = 4
    Output: 17
    

    Below program illustrates the java.lang.Integer.valueOf(String str, int base) method:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java praogram to illustrate the
    // java.lang.Integer.valueOf(String str, int base)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
            // Base = 2
            Integer val1 = Integer.valueOf("1010", 8);
            System.out.println(val1);
      
            // Base = 16
            Integer val2 = Integer.valueOf("1011", 16);
            System.out.println(val2);
      
            // Base = 2
            Integer val3 = Integer.valueOf("1010", 2);
            System.out.println(val3);
      
            // Base = 10
            Integer val4 = Integer.valueOf("1021", 10);
            System.out.println(val4);
        }
    }

    chevron_right

    
    

    Output:

    520
    4113
    10
    1021
    


My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.