Created
August 23, 2019 19:17
-
-
Save soheil-ghahremani/ba4c63a5b676137cf6e96fedc380a55c to your computer and use it in GitHub Desktop.
difference between String and StringBuilder and StringBuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.soheil_gh; | |
public class StringTest { | |
public static void main(String[] args) { | |
long t1 = System.currentTimeMillis(); | |
String initialString = ""; | |
for(int i=0; i<50000; i++ ){ | |
initialString += i; | |
} | |
long t2 = System.currentTimeMillis(); | |
System.out.println(t2 - t1 + " milliseconds"); | |
StringBuilder initialStringBuilder = new StringBuilder(); | |
long t3 = System.currentTimeMillis(); | |
for(int i=0; i<50000; i++ ){ | |
initialStringBuilder.append(i); | |
} | |
long t4 = System.currentTimeMillis(); | |
System.out.println(t4 - t3 + " milliseconds"); | |
StringBuffer initialStringBuffer = new StringBuffer(); | |
long t5 = System.currentTimeMillis(); | |
for(int i=0; i<50000; i++ ){ | |
initialStringBuffer.append(i); | |
} | |
long t6 = System.currentTimeMillis(); | |
System.out.println(t6 - t5 + " milliseconds"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output :
7079 milliseconds
2 milliseconds
3 milliseconds