logo

Metoda wartości całkowitej Java typu valueOf().

The wartość() metoda jest metodą statyczną, która zwraca odpowiedni obiekt typu Integer, zawierający wartość przekazanego argumentu. Argumentem może być prymitywny typ danych, ciąg znaków itp. Istnieją trzy różne typy metody Java valueOf(), które można rozróżnić w zależności od jej parametru.

To są:

ciąg konkatenacyjny w Javie
  1. Metoda Java Integer valueOf(int i).
  2. Metoda Java Integer valueOf(String s).
  3. Metoda Java Integer valueOf(String s, int radix).

1. Metoda Java Integer valueOf(int i).

The wartośćOf(int i) metoda Liczba całkowita Java class zwraca instancję Integer reprezentującą określoną wartość int. Ta metoda zawsze akceptuje wartości z zakresu od -128 do 127 i może buforować inne wartości spoza tego zakresu.

2. Metoda Java Integer valueOf(String s).

The wartość(String s) jest wbudowaną metodą Jawa który służy do zwracania obiektu Integer zawierającego wartość określonego ciągu. Argument jest interpretowany jako dziesiętna liczba całkowita ze znakiem. Innymi słowy, ta metoda zwraca obiekt Integer równy wartości:

 new Integer(Integer.parseInt(s)). 

3. Metoda Java Integer valueOf(String s, int radix).

The wartośćOf(String s, int radix) Metoda służy do zwrócenia obiektu typu Integer zawierającego wartość wyodrębnioną z określonego ciągu znaków po przeanalizowaniu z podstawą podaną w drugim argumencie. Innymi słowy, ta metoda zwraca obiekt Integer równy wartości:

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

Składnia:

Poniżej znajdują się oświadczenia wartość() metoda:

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

Parametr:

Typ danych Parametr Opis Wymagane/opcjonalne
wew I Jest to wartość typu int określona przez użytkownika i używana przy konwersji obiektu Integer. Wymagany
Strunowy S Jest to typ String, który zostanie przetworzony na obiekt będący liczbą całkowitą. Wymagany
wew źródło Jest to typ całkowity i używany do konwersji obiektu typu string. Wymagany

Zwroty:

metoda Zwroty
wartośćOf(int i) Zwraca instancję typu Integer przechowującą wartość określonego parametru in i.
wartość(String s) Zwraca instancję typu Integer zawierającą wartość reprezentowaną przez argument w postaci ciągu znaków.
wartośćOf(String s, int radix) Zwraca instancję Integer przechowującą wartość reprezentowaną przez argument łańcuchowy w określonej podstawie.

Wyjątki:

NumberFormatWyjątek: Zgłasza wyjątek, gdy wejściowy ciąg znaków w odniesieniu do określonej podstawy nie jest analizowalną wartością typu int.

Wersja kompatybilności:

Java 1.5 i nowsze

Linux $home

Przykład 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)); } } 
Przetestuj teraz

Wyjście:

string.replaceall w Javie
 Value = 2 Value = -5 

Przykład 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)); } } 
Przetestuj teraz

Wyjście:

 Output Value = 355 Output Value = -355 

Przykład 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)); } } 
Przetestuj teraz

Wyjście:

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

Przykład 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)); } } 
Przetestuj teraz

Wyjście:

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

Przykład 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)); } } 
Przetestuj teraz

Wyjście:

 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)