Java Integer.valueOf() Method

The valueOf() method is a static method that returns the relevant Integer Object holding the value of the argument passed. The argument can be a primitive data type, String, etc. Java's valueOf() function is an essential tool for mapping different data types-such as strings and primitives-into their respective wrapper objects. There are three different variations of the valueOf() method inside the Integer class alone, each designed to handle a certain kind of input parameter. Comprehending these variations together with their corresponding use cases is crucial for effective data manipulation and conversion in Java programs. In this section, we will discuss the valueOf() method, its use, functionality, and subtleties related to the many parameter types it supports.

There are three different types of Java valueOf() method which can be differentiated depending on its parameter.

These are:

  1. Java Integer valueOf(int i) Method
  2. Java Integer valueOf(String s) Method
  3. Java Integer valueOf(String s, int radix) Method

1. Java Integer.valueOf(int i) Method

The valueOf(int i) method of Java Integer class returns an Integer instance representing the specified int value. This method will always accept values in the range -128 to 127 and may cache other values outside of this range.

2. Java Integer.valueOf(String s) Method

The valueOf(String s) is an inbuilt method of Java which is used to returns an Integer object holding the value of the specified string. The argument is interpreted as a signed decimal integer. In other words, this method returns an Integer object equal to the value of:

It is used to convert a string containing a numeric value into its corresponding Integer object. For example:

3. Java Integer.valueOf(String s, int radix) Method

The valueOf(String s, int radix) method is used to return an Integer object holding the value extracted from the specified string when parsed with the radix given by the second argument. In other words, this method returns an Integer object equal to the value of:

This method is particularly useful when dealing with non-decimal number systems like binary, octal, or hexadecimal. For example:

Syntax:

Following are the declaration of valueOf() method:

Parameter:

DataTypeParameterDescriptionRequired/Optional
intiIt is an int value specified by the user and used in converting the Integer object.Required
StringsIt is a type of String which will be parsed into an integer object.Required
intradixThis is of integer type and used in converting the string object.Required

Returns:

MethodReturns
valueOf(int i)Returns an Integer instance holding the value of the specified parameter int i.
valueOf(String s)Returns an Integer instance holding the value represented by the string argument.
valueOf(String s, int radix)Returns an Integer instance holding the value represented by the string argument in the specified radix.

Exceptions:

NumberFormatException: It throws exception when the input String with respect to specified radix is not a parsable int.

Compatibility Version:

Java 1.5 and above

Performance Implications of Using the valueOf() Method

The process of automatically transforming wrapper class objects into their corresponding primitive data types is called unpacking, and the process of automatically transforming wrapper class objects back into their primitive data types is called autoboxing. When using the valueOf() method, giving primitive data types as parameters triggers autoboxing, and getting primitive values from the wrapper objects the method returns triggers unpacking.

Although autoboxing and unpacking are convenient, they can have an adverse effect on performance, particularly when working with large datasets or in speed-critical areas of the code. The reason for this is that autoboxing and unpacking entail the generation and destruction of wrapper objects, which may result in overhead related to trash collection and memory allocation.

Example 1

File Name: IntegerValueOfExample1.java

Test it Now

Output:

Value = 2
Value = -5

Explanation

The valueOf() method from the Integer class is used in this Java code example. Two Integer objects, a and b, are instantiated in the main method with the integer values 35 and -45, respectively. Next, we call these objects' valueOf() function with the given inputs. Nevertheless, the valueOf() function given by the Integer class is not the same as the one used in the code sample.

Rather, it appears that the strategy is being misunderstood or misused. The valueOf() function in the standard Integer class is static and requires no parameters. All that is returned is an instance of an Integer that represents the given int value. Consequently, the code won't compile as written.

Example 2

File Name: IntegerValueOfExample2.java

Test it Now

Output:

Output Value = 355
Output Value = -355

Explanation

In this instance, the value 10 is used to instantiate an integer object (i). The values "355" and "-355" for two strings, str1 and str2, are stated. After that, the Integer object i is called using the valueOf() function, passing each string as an argument. An integer's string representation is changed into an integer object using this function. The output shows that the strings "355" and "-355" were successfully converted into the appropriate integer values.

Example 3

File Name: IntegerValueOfExample3.java

Test it Now

Output:

Desired Value is: 234
Base Number is: 8
Integer Value: 156

Explanation

In this case, an integer radix of 8 is specified, and a string strValue is initialised with the value "234". The radix and the string are passed as arguments to the valueOf() function. This function returns an Integer object that represents the value of strValue after interpreting it as an integer in the specified radix. The output shows the integer value 156 that is produced after converting the string to its decimal counterpart, together with the base number (8) and the target value (234).

Example 4

File Name: IntegerValueOfExample4.java

Test it Now

Output:

Enter Desired Value: CDEF
Enter Base Number: 16
Output Value: 52719

Explanation

In this example, the console asks the user to provide a base number and a desired value. After that, a scanner is used to read the inputs. The valueOf() function is used to convert a number's textual representation to its decimal representation. It takes two parameters: the input string and radix. The converted value is then displayed on the console. Users can dynamically enter values into this programme to get their decimal equivalents.

Example 5

File Name: IntegerValueOfExample5.java

Test it Now

Output:

Enter Desired Value: ABCDEF
Exception in thread "main" java.lang.NumberFormatException: For input string: "ABCDEF"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Integer.parseInt(Integer.java:652)
	at java.base/java.lang.Integer.valueOf(Integer.java:983)
	at myPackage.IntegerValueOfExample5.main(IntegerValueOfExample5.java:13)

Explanation

Here, the user is prompted by the programme to enter a desired value from the console. A scanner is used to read the input as a string. The input string is then passed as an argument to the valueOf() function that converts it into the appropriate integer value. The integer value is finally displayed on the terminal. With the help of this application, users may easily input values from the console and get their integer equivalents.

Conclusion

The article concludes with a thorough description of the valueOf() method in Java's Integer class, including all of its variations and applications. Developers can efficiently modify and convert data within Java programmes by being aware of these variances. Furthermore, the examples show real-world uses for the approach as well as potential dangers to watch out for. In general, Java programming is made more flexible and efficient by the valueOf() method, which is a useful tool for converting various data types into their appropriate integer representations.