×

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

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

Vigesimal in Java

A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty...

4 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.