Java Integer valueOf() MethodThe valueOf() method is a static method which returns the relevant Integer Object holding the value of the argument passed. The argument can be a primitive data type, String, etc. 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) MethodThe 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) MethodThe 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: 3. Java Integer valueOf(String s, int radix) MethodThe 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: Syntax:Following are the declaration of valueOf() method: Parameter:
Returns:
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 Example 1Test it NowOutput: Value = 2 Value = -5 Example 2Test it NowOutput: Output Value = 355 Output Value = -355 Example 3Test it NowOutput: Desired Value is: 234 Base Number is: 8 Integer Value: 156 Example 4Test it NowOutput: Enter Desired Value: CDEF Enter Base Number: 16 Output Value: 52719 Example 5Test it NowOutput: 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)
Next Topic#
|