Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How java converts int type into string type
How java converts int type into string type
In JAVA, there are three common methods to convert an int type into a String type:

1、String.valueOf(int i)

2、Integer.toString(int i)

3. I+"; //i is of type int.

The efficiency order of the three methods is:

Integer.tostring (int I) > string.valueof (int I) > I+";

Code test:

Public static void main(String[] args) {

int[]int arr = new int[ 1000000];

String[] strArr 1 = new string [1000000]; //To be fair, define three arrays respectively.

String[] strArr2 = new string [1000000];

String[] strArr3 = new string [1000000];

//assignment

long t 1 = system . current time millis();

for(int I = 0; I< 1000000; i++){

intArr[I]= I+ 1;

}

long T2 = system . current time millis();

for(int I = 0; I< 1000000; i++){

strarr 1[I]= string . value of(intArr[I]);

}

long T3 = system . current time millis();

for(int I = 0; I< 1000000; i++){

strarr 2[I]= integer . tostring(intArr[I]);

}

long T4 = system . current time millis();

for(int I = 0; I< 1000000; i++){

strarr 3[I]= intArr[I]+" ";

}

long t5 = system . current time millis();

system . out . println(" t 1 = "+t 1 ");

system . out . println(" T2 = "+T2 ");

system . out . println(" T3 = "+T3);

system . out . println(" T4 = "+T4);

system . out . println(" t5 = "+t5 ");

System.out.println ("assignment:"+(t2-t1));

system . out . println(" string . value of(I):"+(T3-T2));

system . out . println(" integer . tostring(I):"+(T4-T3));

system . out . println(" I+\ " \ ":"+(t5-T4));

}

My test results are:

t 1 = 1298798872078

t2 = 1298798872093

t3 = 1298798872656

t4 = 1298798873000

t5 = 129879887367 1

Distribution: 15

String.valueOf(i):563

Integer.toString(i):344

i+"":67 1