×

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

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.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.