- IL java.lang.Integer.toString() è un metodo integrato in Java che viene utilizzato per restituire l'oggetto String che rappresenta il valore di questo numero intero.
Sintassi:
public static String toString()>
parametri: Il metodo non accetta alcun parametro.
Valore di ritorno: Il metodo restituisce l'oggetto stringa del particolare valore intero.
Il programma seguente illustra il metodo Java.lang.Integer.toString():
// Java program to illustrate the>// toString() Method>import>java.lang.*;>>public>class>Geeks{>>public>static>void>main(String[] args) {>>>Integer obj =>new>Integer(>8>);>>>//It will return a string representation>>String stringvalue1 = obj.toString();>>System.out.println(>'String Value= '>+>>stringvalue1);>>>>Integer obj3 =>new>Integer(>10>);>>>//It will return a string representation>>String stringvalue3 = obj3.toString();>>System.out.println(>'String Value = '>+>>stringvalue3);>>}>}>>
>
Produzione:String Value= 8 String Value = 10>
- IL java.lang.Integer.toString( int a ) è un metodo integrato in Java che viene utilizzato per restituire un oggetto String, che rappresenta l'intero specificato nel parametro.
Sintassi:public static String toString(int a )>
parametri: Il metodo accetta un parametro UN di tipo intero e si riferisce all'intero necessario da convertire in stringa.
Valore di ritorno: Il metodo restituisce la rappresentazione di stringa dell'argomento in una base particolare.
scarica video di youtube su vlc
Esempi:
For base 8: Input: int a = 75 Output: '75' For base 10: Input: int a = -787 Output: '-787'>
I seguenti programmi illustrano il metodo Java.lang.Integer.toString(int a):
Programma 1:
// Java program to illustrate the>// toString(int a) method>import>java.lang.*;>>public>class>Geeks{>>public>static>void>main(String[] args) {>>>Integer obj =>new>Integer(>8>);>>>// It will return a string representation>>// in base 8>>String stringvalue1 = obj.toString(>75>);>>System.out.println(>'String Value = '>+>>stringvalue1);>>>Integer obj2 =>new>Integer(>8>);>>>// It will return a string representation>>// in base 2>>String stringvalue2 = obj2.toString(>6787>);>>System.out.println(>'String Value = '>+>>stringvalue2);>>>>Integer obj3 =>new>Integer(>10>);>>>// It will return a string representation>>// in base 10>>String stringvalue3 = obj3.toString(->787>);>>System.out.println(>'String Value = '>+>>stringvalue3);>>}>}>>
>
Produzione:String Value = 75 String Value = 6787 String Value = -787>
Programma 2: Per parametri decimali e stringa.
Nota: Ciò si traduce in un errore e anche nell'assenza di un costruttore intero adeguato.
// Java program to illustrate the>// Java.lang.Integer.toString(int a)method>import>java.lang.*;>public>class>Geeks{>>public>static>void>main(String[] args) {>>Integer obj =>new>Integer(>8>);>>String stringvalue1 = obj.toString(>58.5>);>>System.out.println(>'String Value = '>+>>stringvalue1);>>>Integer obj2 =>new>Integer(>8>);>>String stringvalue2 = obj2.toString(>'317'>);>>System.out.println(>'String Value = '>+>>stringvalue2);>>>>// Empty constructor will result in an error>>Integer obj3 =>new>Integer();>>String stringvalue3 = obj3.toString(->787>);>>System.out.println(>'String Value = '>+>>stringvalue3);>>}>}>>
enumerazioni Java
>
Produzione:
prog.java:8: error: incompatible types: possible lossy conversion from double to int String stringvalue1 = obj.toString(58.5); ^ prog.java:12: error: incompatible types: String cannot be converted to int String stringvalue2 = obj2.toString('317'); ^ prog.java:17: error: no suitable constructor found for Integer(no arguments) Integer obj3 = new Integer(); ^ constructor Integer.Integer(int) is not applicable (actual and formal argument lists differ in length) constructor Integer.Integer(String) is not applicable (actual and formal argument lists differ in length) Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 3 errors> - IL java.lang.Integer.toString( int a, int base ) è un metodo integrato in Java che viene utilizzato per restituire una rappresentazione di stringa dell'argomento UN nella base, specificata dal secondo argomento base . Se la radice/base è inferiore a Character.MIN_RADIX o maggiore di Character.MAX_RADIX, viene utilizzata la base 10. I caratteri ASCII utilizzati come cifre: da 0 a 9 e da a a z.
Sintassi:public static String toString(int a, int base)>
Parametro: Il metodo accetta due parametri:
- UN : È di tipo intero e si riferisce al valore intero che deve essere convertito.
- base : Anche questo è di tipo intero e si riferisce alla base che si intende utilizzare per rappresentare le stringhe.
Valore di ritorno: Il metodo restituisce una rappresentazione di stringa dell'argomento specificato nella base specificata.
Esempi:
Input: a = 71, base = 2 Output: 1000111 Input: a = 314, base = 16 Output: 13a>
I seguenti programmi illustrano il metodo Java.lang.Integer.toString(int a, int base):
Programma 1:
pulsante centrale nei css
// Java program to illustrate the>// toString(int, int) Method>import>java.lang.*;>>public>class>Geeks{>>public>static>void>main(String[] args) {>>>Integer a =>new>Integer(>10>);>>>// It returns a string representation>>// in base 2>>String returnvalue = a.toString(>5254>,>2>);>>System.out.println(>'String Value = '>+>>returnvalue);>>>// It returns a string representation>>// in base 8>>returnvalue = a.toString(>35>,>8>);>>System.out.println(>'String Value = '>+>>returnvalue);>>>// It returns a string representation>>// in base 16>>returnvalue = a.toString(>47>,>16>);>>System.out.println(>'String Value = '>+>>returnvalue);>>>// It returns a string representation>>// in base 10>>returnvalue = a.toString(>451>,>10>);>>System.out.println(>'String Value = '>+>>returnvalue);>}>}>>
>
Produzione:String Value = 1010010000110 String Value = 43 String Value = 2f String Value = 451>
Programma 2:
// Java program to illustrate the>// toString(int, int) Method>import>java.lang.*;>>public>class>Geeks{>>public>static>void>main(String[] args) {>>>Integer a =>new>Integer(>10>);>>>// It returns a string representation>>// in base 2>>String returnvalue = a.toString(->525>,>2>);>>System.out.println(>'String Value = '>+>>returnvalue);>>>// It returns a string representation>>// in base 8>>returnvalue = a.toString(>31>,>8>);>>System.out.println(>'String Value = '>+>>returnvalue);>>>// It returns a string representation>>// in base 16>>returnvalue = a.toString(>28>,>16>);>>System.out.println(>'String Value = '>+>>returnvalue);>>>// It returns a string representation>>// in base 10>>returnvalue = a.toString(>29>,>10>);>>System.out.println(>'String Value = '>+>>returnvalue);>}>}>>
>
Produzione:String Value = -1000001101 String Value = 37 String Value = 1c String Value = 29>