×

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns an Integer object which represents the specified String when parsed with the given radix. Syntax
  1. public static Integer valueOf (int i)
  2. public static Integer valueOf(String s) throws NumberFormatException
  3. public static Integer valueOf(String s, int radix) throws NumberFormatException
 Parameters i- an int value s- the string to be parsed radix- the radix to be used in interpreting s Throws The valueOf() method throws: NumberFormatException- if the string does not contain a parsable int.  Return Value This method returns an int object holding the value represented by ‘i’ or by the string or by the string argument in the specified radix. Example 1
public class JavaIntegerValueOfExample1 {
    public static void main(String[] args) {
        // returns a Integer object which represents the specified int
        int val=12;
        System.out.println("valueOf() method returns : "+Integer.valueOf(val));
    }
}
Output
valueOf() method returns : 12
Example 2
public class JavaIntegrValueOfExample2 {
    public static void main(String[] args) {
        // returns a Integer object which represents the specified String
        String str="369876543234567890";
        //return an exception if the string does not contain a parsable byte
        System.out.println("valueOf() method returns : "+Integer.valueOf(str));
    }
}
Output
Exception in thread "main" java.lang.NumberFormatException: For input string: "369876543234567890"
              at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
              at java.lang.Integer.parseInt(Integer.java:583)
              at java.lang.Integer.valueOf(Integer.java:766)
              at com.TutorialAndExample.JavaIntegerValueOfExample2.main(JavaIntegerValueOfExample2.java:8)
Example 3
public class JavaIntegerValueOfExample3 {
    public static void main(String[] args) {
        // returns a Integer object which represents the specified String when parsed with the given radix
        String str="36";
        int radix=Character.MAX_RADIX;
        //return an exception if the string does not contain a parsable byte
        System.out.println("valueOf() method returns : "+Integer.valueOf(str,radix));
    }
}
Output
valueOf() method returns : 114
Example 4
public class JavaIntegerValueOfExample4 {
    public static void main(String[] args) {
        // returns a Integer object which represents the specified String
        String str="369";
        int radix=98;
        //return an exception if the radix is greater than Character.MAX_RADIX
        System.out.println("valueOf() method returns : "+Integer.valueOf(str,radix));
    }
}
Output
Exception in thread "main" java.lang.NumberFormatException: radix 98 greater than Character.MAX_RADIX
              at java.lang.Integer.parseInt(Integer.java:551)
              at java.lang.Integer.valueOf(Integer.java:740)
              at com.TutorialAndExample.JavaIntegerValueOfExample4.main(JavaIntegerValueOfExample4.java:9)

Related Topics

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Upcasting in Java

To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java. Casting The process of providing or replacing a reference variable of...

3 minutes read.