The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java String vs StringBuffer vs StringBuilder(Performance)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java String vs StringBuffer vs StringBuilder(Performance) Posted: May 11, 2016 6:39 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java String vs StringBuffer vs StringBuilder(Performance)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube : 
https://www.youtube.com/watch?v=IWRh7OVJPbA&list=UUhwKlOVR041tngjerWxVccw

PerformanceTest.java
public class PerformanceTest
{

/*
* String is slow and consumes more memory when you
* concat too many strings because every time it creates
* new instance.
*/

public static String concatenatingWithString()
{
String str = "Welcome";
for (int i = 0; i < 70000; i++)
{
str = str + "Peter";
}
return str;
}

/*
* StringBuffer is fast and consumes less memory when
* you cancat strings.
*
* StringBuffer is faster than String.
*/

public static String concatenatingWithStringBuffer()
{
StringBuffer sb = new StringBuffer("Welcome");
for (int i = 0; i < 70000; i++)
{
sb.append("Peter");
}
return sb.toString();
}

/*
* StringBuilder is fast and consumes less memory when
* you cancat strings.
*
* StringBuilder is faster than StringBuffer.
*/

public static String concatenatingWithStringBuilder()
{
StringBuilder sb = new StringBuilder("Welcome");
for (int i = 0; i < 70000; i++)
{
sb.append("Peter");
}
return sb.toString();
}

public static void main(String[] args)
{
long startTime = System.currentTimeMillis();

concatenatingWithString();

System.out
.println("Time taken by Concatenating with String : "
+ (System.currentTimeMillis() - startTime)
+ "ms");

startTime = System.currentTimeMillis();

concatenatingWithStringBuffer();

System.out
.println("Time taken by Concatenating with StringBuffer : "
+ (System.currentTimeMillis() - startTime)
+ "ms");

startTime = System.currentTimeMillis();

concatenatingWithStringBuilder();

System.out
.println("Time taken by Concatenating with StringBuilder: "
+ (System.currentTimeMillis() - startTime)
+ "ms");
}
}
Output
Time taken by Concatenating with String       : 17394ms
Time taken by Concatenating with StringBuffer : 20ms
Time taken by Concatenating with StringBuilder: 10ms
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/StringBuilderDemo_Perf_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StringBuilderDemo_Perf_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/c539eeaf4c6ff331d8e2aa2333826ea70c999093/BasicJava/StringBuilderDemo_Perf_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Read: Java Tutorial : Java String vs StringBuffer vs StringBuilder(Performance)

    Topic: 6 traits engineers need to succeed as IT leaders Previous Topic   Next Topic Topic: 35% off Logitech G230 Stereo Gaming Headset - Deal Alert

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use