Sunday 27 July 2014

Examples on String Class in Java

In this post we will see some examples on String Class in java

How to reverse a String


public class StringReverseExample{
   public static void main(String[] args){
      String string="abcdef";
      String reverse = new StringBuffer(string).
      reverse().toString();
      System.out.println("\nString before reverse:
      "+string);
      System.out.println("String after reverse:
      "+reverse);
   }
}



OutPut:

String before reverse:abcdef
String after reverse:fedcba


How to compare performance of string creation


public class StringComparePerformance{
   public static void main(String[] args){    
      long startTime = System.currentTimeMillis();
      for(int i=0;i<50000;i++){
         String s1 = "hello";
         String s2 = "hello";
      }
      long endTime = System.currentTimeMillis();
      System.out.println("Time taken for creation"
      + " of String literals : "+ (endTime - startTime)
      + " milli seconds" );    
      long startTime1 = System.currentTimeMillis();
      for(int i=0;i<50000;i++){
         String s3 = new String("hello");
         String s4 = new String("hello");
      }
      long endTime1 = System.currentTimeMillis();
      System.out.println("Time taken for creation"
      + " of String objects : " + (endTime1 - startTime1)
      + " milli seconds");
   }
}


OutPut:

Time taken for creation of String literals : 0 milli seconds
Time taken for creation of String objects : 16 milli seconds




How to optimize string creation



public class StringOptimization{
   public static void main(String[] args){
      String variables[] = new String[50000];
      for( int i=0;i <50000;i++){
         variables[i] = "s"+i;
      }
      long startTime0 = System.currentTimeMillis();
      for(int i=0;i<50000;i++){
         variables[i] = "hello";
      }
      long endTime0 = System.currentTimeMillis();
      System.out.println("Creation time"
      + " of String literals : "+ (endTime0 - startTime0)
      + " ms" );
      long startTime1 = System.currentTimeMillis();
      for(int i=0;i<50000;i++){
         variables[i] = new String("hello");
      }
      long endTime1 = System.currentTimeMillis();
      System.out.println("Creation time of"
      + " String objects with 'new' key word : "
      + (endTime1 - startTime1)
      + " ms");
      long startTime2 = System.currentTimeMillis();
      for(int i=0;i<50000;i++){
         variables[i] = new String("hello");
         variables[i] = variables[i].intern();
      }
      long endTime2 = System.currentTimeMillis();
      System.out.println("Creation time of"
      + " String objects with intern(): "
      + (endTime2 - startTime2)
      + " ms");
   }
}


OutPut:

Creation time of String literals : 0 ms
Creation time of String objects with 'new' key word : 31 ms
Creation time of String objects with intern(): 16 ms



How to format strings



import java.util.*;

public class StringFormat{
   public static void main(String[] args){
      double e = Math.E;
      System.out.format("%f%n", e);
      System.out.format(Locale.GERMANY, "%-10.4f%n%n", e);
   }
}


OutPut:

2.718282
2,7183


No comments:

Post a Comment