Integer valueOf() Method in Java
Last Updated :
14 Dec, 2021
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
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
Integer obj = new Integer( 10 );
System.out.println( "Output Value = " +
obj.valueOf( 85 ));
}
}
|
Output:
Output Value = 85
Program 2: For a negative number.
Java
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
Integer obj = new Integer( 10 );
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
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
Integer obj = new Integer( 8 );
String str = "424" ;
System.out.println( "Integer Value = " +
obj.valueOf(str));
}
}
|
Output:
Integer Value = 424
Program 2: For a negative number.
Java
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
Integer obj = new Integer( 8 );
String str = "-6156" ;
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
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
Integer val1 = Integer.valueOf( "1010" , 8 );
System.out.println(val1);
Integer val2 = Integer.valueOf( "1011" , 16 );
System.out.println(val2);
Integer val3 = Integer.valueOf( "1010" , 2 );
System.out.println(val3);
Integer val4 = Integer.valueOf( "1021" , 10 );
System.out.println(val4);
}
}
|
Similar Reads
BigInteger valueOf() Method in Java
The java.math.BigInteger.valueOf(long value) method returns a BigInteger whose value is equal to value of long passed as parameter. This method is static method so It is not necessary to create object of BigInteger class to use this method.we can call this function by BigInteger.valueOf(long value)
2 min read
Integer shortValue() Method in Java
The Integer.shortValue() is an inbuilt method of java.lang which returns the value of this Integer in the short type . Syntax: public short shortValue() Parameters: The method does not take any parameters. Return Value: The method returns the integer value represented by this object after converting
2 min read
Integer sum() Method in Java
The java.lang.Integer.sum() is a built-in method in java that returns the sum of its arguments. The method adds two integers together as per the + operator. Syntax : public static int sum(int a, int b) Parameter: The method accepts two parameters that are to be added with each other: a : the first i
2 min read
Integer rotateLeft() Method in Java
Bit shifting is a type of bitwise operation which is performed on all the bits of a binary value by moving the bits by a definite number of places towards left or right. Java has a single Logical left shift operator (<< ). When the value 0001(i.e., 1) is shifted left, it becomes 0010(i.e., 2)
3 min read
Integer reverse() Method In Java
The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value. Syntax: public static int reverse(int a) Parameters: The parameter a is an integer value whose bits are to be rev
2 min read
BigInteger or() method in Java
Prerequisite: BigInteger Basics The java.math.BigInteger.or(BigInteger val) method is used to perform bitwise OR of two BigIntegers. One of the BigInteger is passed in parameter and the other on which the function is called. This method returns a negative number if either of the BigIntegers used wit
2 min read
Integer toOctalString() Method in Java
Octal is the base-8 number system and uses the digits 0 to 7 in operation. Octal is widely used in computing on systems like the PDP-8, and IBM mainframes employed 12-bit, 24-bit or 36-bit words. The Java.lang.Integer.toOctalString() method is a built-in function in Java that is used to return a str
3 min read
BigDecimal valueOf() Method in Java
java.math.BigDecimal.valueOf(long val) is an inbuilt method in Java that translates a long value into a BigDecimal value with a scale of zero. It allows us, the reuse of frequently used BigDecimal values and hence this "static factory method" is provided in preference to a (long) constructor. Packag
3 min read
Java Integer bitCount() Method
The bitCount() method of Integer class of java.lang package returns the count of set bits in a positive number. For negative numbers, it returns the count of set bits in its 2s complement form. This function is sometimes referred to as the population count. Syntax: public static int bitCount(int n)
2 min read
Integer decode() Method in Java
It is often seen that any integer within (" ") is also considered as string, then it is needed to decode that into the integer. The main function of java.lang.Integer.decode() method is to decode a String into an Integer. The method also accepts decimal, hexadecimal, and octal numbers. Syntax : publ
2 min read