logo

Méthode Java Integer valueOf()

Le valeur de() La méthode est une méthode statique qui renvoie l'objet entier pertinent contenant la valeur de l'argument passé. L'argument peut être un type de données primitif, une chaîne, etc. Il existe trois différents types de méthode Java valueOf() qui peuvent être différenciées en fonction de son paramètre.

Ceux-ci sont:

convertir une chaîne en date
  1. Méthode Java Integer valueOf(int i)
  2. Méthode Java Integer valueOf(String s)
  3. Méthode Java Integer valueOf(String s, int radix)

1. Java Integer valueOf(int i)Méthode

Le valeurDe(int i) méthode de Java entier class renvoie une instance Integer représentant la valeur int spécifiée. Cette méthode acceptera toujours les valeurs comprises entre -128 et 127 et pourra mettre en cache d'autres valeurs en dehors de cette plage.

2. Méthode Java Integer valueOf(String s)

Le valeurDe(Chaîne s) est une méthode intégrée de Java qui est utilisé pour renvoyer un objet Integer contenant la valeur de la chaîne spécifiée. L'argument est interprété comme un entier décimal signé. En d’autres termes, cette méthode renvoie un objet Integer égal à la valeur de :

 new Integer(Integer.parseInt(s)). 

3. Méthode Java Integer valueOf(String s, int radix)

Le valueOf(String s, base int) La méthode est utilisée pour renvoyer un objet Integer contenant la valeur extraite de la chaîne spécifiée lors de l'analyse avec la base donnée par le deuxième argument. En d’autres termes, cette méthode renvoie un objet Integer égal à la valeur de :

 new Integer(Integer.parseInt(s, radix)) 

Syntaxe:

Voici la déclaration de valeur de() méthode:

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

Paramètre:

Type de données Paramètre Description Obligatoire/Facultatif
int je Il s'agit d'une valeur int spécifiée par l'utilisateur et utilisée lors de la conversion de l'objet Integer. Requis
Chaîne s C'est un type de String qui sera analysé en un objet entier. Requis
int base Il s'agit d'un type entier utilisé pour convertir l'objet chaîne. Requis

Retour:

Méthode Retour
valeurDe(int i) Renvoie une instance Integer contenant la valeur du paramètre spécifié int i.
valeurDe(Chaîne s) Renvoie une instance Integer contenant la valeur représentée par l'argument chaîne.
valueOf(String s, base int) Renvoie une instance Integer contenant la valeur représentée par l'argument chaîne dans la base spécifiée.

Des exceptions:

NumberFormatException : Il lève une exception lorsque la chaîne d'entrée par rapport à la base spécifiée n'est pas un entier analysable.

Version de compatibilité :

Java 1.5 et supérieur

tableau de retour Java

Exemple 1

 public class IntegerValueOfExample1 { @SuppressWarnings('static-access') public static void main(String[] args) { Integer a = 35; Integer b = -45; //It returns a Integer instance representing the specified int value System.out.println('Value = ' + a.valueOf(2)); System.out.println('Value = ' + b.valueOf(-5)); } } 
Testez-le maintenant

Sortir:

exemple de format json
 Value = 2 Value = -5 

Exemple 2

 public class IntegerValueOfExample2 { @SuppressWarnings('static-access') public static void main(String[] args) { Integer i = 10; String str1 = '355'; String str2 = '-355'; // It will return a Integer instance representing the specified string System.out.println('Output Value = ' + i.valueOf(str1)); System.out.println('Output Value = ' + i.valueOf(str2)); } } 
Testez-le maintenant

Sortir:

 Output Value = 355 Output Value = -355 

Exemple 3

 public class IntegerValueOfExample3 { public static void main(String[] args)throws NumberFormatException { String strValue = '234'; System.out.print('Desired Value is: '+strValue); int radix = 8; System.out.print('
Base Number is: '+radix); // print the value in decimal format System.out.println('
Integer Value: ' + Integer.valueOf(strValue, radix)); } } 
Testez-le maintenant

Sortir:

 Desired Value is: 234 Base Number is: 8 Integer Value: 156 

Exemple 4

 import java.util.Scanner; public class IntegerValueOfExample4 { public static void main(String[] args)throws NumberFormatException { //Input desired value from the console System.out.print('Enter Desired Value: '); Scanner scan = new Scanner(System.in); String strValue = scan.nextLine(); //Input base number from the console System.out.print('Enter Base Number: '); int radix = scan.nextInt(); scan.close(); // print the output in decimal format System.out.println('Output Value: ' +Integer.valueOf(strValue, radix)); } } 
Testez-le maintenant

Sortir:

 Enter Desired Value: CDEF Enter Base Number: 16 Output Value: 52719 

Exemple 5

 import java.util.Scanner; public class IntegerValueOfExample5 { public static void main(String[] args)throws NumberFormatException { //Enter input from user console System.out.print('Enter Desired Value: '); Scanner scan = new Scanner(System.in); String strVal = scan.nextLine(); scan.close(); //Print the output value in decimal format System.out.println('Integer Value:' + Integer.valueOf(strVal)); } } 
Testez-le maintenant

Sortir:

 Enter Desired Value: ABCDEF Exception in thread 'main' java.lang.NumberFormatException: For input string: 'ABCDEF' at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.valueOf(Integer.java:983) at myPackage.IntegerValueOfExample5.main(IntegerValueOfExample5.java:13)