Java Integer.valueOf(String s, int radix) example

Java Integer.valueOf(String s, int radix) example

By Yashwant Chavan, Views 12, Date 21-Apr-2018

In this java tutorial, You will learn how to use valueOf(String s, int radix) method of class Integer. This method returns Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed integer in the radix specified by the second argument, exactly as if the arguments were given to the parseInt(java.lang.String, int) method. The result is an Integer object that represents the integer value specified by the string.

tags Java

Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

Syntax

public static Integer valueOf(String s, int radix) throws NumberFormatException

Parameters

s - the string to be parsed.

radix - the radix to be used in interpreting s.

Returns

An Integer object holding the value represented by the string argument in the specified radix.

Throws

NumberFormatException - if the String does not contain a parsable int.

Java Integer valueOf(String s, int radix) example

  • Assign two integer values.
  • Integer.valueOf(firstValue, 16);It will return the integer value of string firstValue with radix 16.
  • Integer.valueOf(secondValue, 32);It will return the integer value of string secondValue with radix 32.
  • Print the values.
package com.technicalkeeda;

public class App {

    public static void main(String[] args) {


        String firstValue = "45";
        String secondValue = "80";

        Integer result1 = Integer.valueOf(firstValue, 16);
        Integer result2 = Integer.valueOf(secondValue, 32);

        System.out.println("The integer value of firstValue with the radix 16 :- " + result1);
        System.out.println("The integer value of secondValue with the radix 32:- " + result2);
    }
}

output

The integer value of firstValue with the radix 16 :- 69
The integer value of secondValue with the radix 32:- 256
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]