Int para String
Pessoal, como passo de Int para String?
Grato
Daniel Gomes
Curtidas 0
Respostas
Edinei Manica
09/04/2009
[quote="dgomesbr"]Pessoal, como passo de Int para String?
Grato
int teste = 10; String newString = teste.toString();
GOSTEI 0
Luiz Pereira
09/04/2009
Para int
ou
Para Integer
ou
int teste = 10; String str = "" + teste;
int teste = 10; String str = String.valueOf( teste );
Integer teste2 = new Integer( 10 ); String str = teste2.toString();
Integer teste2 = new Integer( 10 ); String str = String.valueOf( teste2 );
GOSTEI 0
Edinei Manica
09/04/2009
[quote="lap_junior"]
ou
ou
int teste = 10; String str = "" + teste;
int teste = 10; String str = String.valueOf( teste );
int teste =10; String str = ""+teste;
GOSTEI 0
Thiago_sis
09/04/2009
int i = 10; String s = String.valueOf(i);
GOSTEI 0
Daniel Gomes
09/04/2009
Ficaria bem mais fácil assim:
int numero = 10;
String teste = ""+numero+"";
Valeu pessoal.
GOSTEI 0
Bruno Borges
09/04/2009
[quote="dgomesbr"]Ficaria bem mais fácil assim:
int numero = 10;
String teste = ""+numero+"";
Valeu pessoal.
se vc sabia a solucao, pq veio aqui perguntar?
e outra: te interna...
GOSTEI 0
Daniel Gomes
09/04/2009
Descobri essa solução agora irmão, se eu soubesse com certeza não teria perguntado.
Descobri essa solução em outro lugar seu prisioneiro de Javafree. Tenho outros Foruns para me comunicar.
Se eu dependesse de um ignorante como vc "miojo" estaria a ver navios.
GOSTEI 0
Bruno Borges
09/04/2009
[quote="Edinei Manica"]
nossa, q compilador Java aceita isso?!?!?! o meu nao aceita, de CERTEZA!!!!
int teste = 10; String newString = teste.toString();
GOSTEI 0
Edinei Manica
09/04/2009
[quote="dgomesbr"]Descobri essa solução agora irmão, se eu soubesse com certeza não teria perguntado.
Descobri essa solução em outro lugar seu prisioneiro de Javafree. Tenho outros Foruns para me comunicar.
Se eu dependesse de um ignorante como vc "miojo" estaria a ver navios.
Ele só falou brincando !
GOSTEI 0
Daniel Gomes
09/04/2009
a tá, me desculpe "miojo".
GOSTEI 0
Bruno Borges
09/04/2009
[quote="dgomesbr"]Ficaria bem mais fácil assim:
int numero = 10;
String teste = ""+numero+"";
Valeu pessoal.
sua solucao, alem de deselegante, tem péssima performance...
faça isso zilhoes de vezes e vc vai ver q eh o pior codigo pra se fazer conversao... concatenacao de literal string com primitivo gera um bytecode que chama StringBuffer...
public class Teste {
public void foo() {
int x = 10;
String s = "" + x + "";
}
public void bar() {
int x = 10;
String s = Integer.toString(x);
}
}GOSTEI 0
Bruno Borges
09/04/2009
Resultado do [b]javap[/b]
D:\>javap -c Teste
Compiled from "Teste.java"
public class Teste extends java.lang.Object{
public Teste();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
[color=darkred:930645eff4]public void foo();
Code:
0: bipush 10
2: istore_1
3: new #2; //class StringBuffer
6: dup
7: invokespecial #3; //Method java/lang/StringBuffer."<init>":()V
10: ldc #4; //String
12: invokevirtual #5; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
15: iload_1
16: invokevirtual #6; //Method java/lang/StringBuffer.append:(I)Ljava/lang/StringBuffer;
19: ldc #4; //String
21: invokevirtual #5; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
24: invokevirtual #7; //Method java/lang/StringBuffer.toString:()Ljava/lang/String;
27: astore_2
28: return[/color:930645eff4]
[color=darkblue:930645eff4]public void bar();
Code:
0: bipush 10
2: istore_1
3: iload_1
4: invokestatic
#8; //Method java/lang/String.valueOf:(I)Ljava/lang/String;
7: astore_2
8: return[/color:930645eff4]
}
GOSTEI 0
Bruno Borges
09/04/2009
experimente esse codigo pra testar a performance:
public class Teste {
public static void foo() {
int x = 10;
String s = "" + x + "";
}
public static void bar() {
int x = 10;
String s = Integer.toString(x);
}
public static void main(String a[]) {
long ops = 4000000;
System.out.println("Total de operacoes por metodo: "+ops);
for(int i=0;i<5;i++) {
System.out.println();
teste(ops);
}
}
public static void teste(long ops) {
long begin = System.currentTimeMillis();
for(long i=0;i<ops;i++)
bar();
System.out.println("Time for bar(): "+(System.currentTimeMillis() - begin) + "ms");
begin = System.currentTimeMillis();
for(long i=0;i<ops;i++)
foo();
System.out.println("Time for foo(): "+(System.currentTimeMillis() - begin) + "ms");
}
}GOSTEI 0
Daniel Gomes
09/04/2009
falou miojo, em 2005 eu testo isso que vc mandou.
vc é um gênio.
GOSTEI 0
Daniel Gomes
09/04/2009
Assunto respondido e fechado.
GOSTEI 0