Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaInteger valueOf() Method in Java

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.

Java




// Java program 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));
    }
}


Output:

Output Value = 85

 

Program 2: For a negative number.

Java




// Java program 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));
    }
}


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.

java




// Java program 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));
    }
}


Output:

Integer Value = 424

 

Program 2: For a negative number.

Java




// Java program 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));
    }
}


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:

Java




// Java program 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);
    }
}


Output:

520
4113
10
1021

 

RELATED ARTICLES

Most Popular

Recent Comments