×

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

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 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.

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.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Reentrant Lock in Java

The Lock interface is implemented by the ReentrantLock class, which also provides methods for synchronising access to shared resources. The code that controls the resource has calls to the lock...

4 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.