Posts

Showing posts with the label concatanation

StringBuffer vs StringBuilder : Performance improvement figures

IN previous post StringBuffer vs StringBuilder , I have explained benefits of using StringBuilder over StringBuffer. Here are some performance figure using both for concatenating string. package com.test;   public class StringConcatenateTest { public static void main(String[] args) { System.out.println( "Using StringBuffer" ); long bufferTime = usingStringBuffer(); System.out.println( "Using String Builder" ); long builderTime = usingStringBuilder(); double perc = (( double )(bufferTime-builderTime)/bufferTime)* 100; System.out.println( "Improved in performance = " + perc + "%" ); } public static long usingStringBuffer(){ long startTime = System.currentTimeMillis(); System.out.println( "\tStart time = " + startTime); StringBuffer sb = new StringBuffer(); for ( int i=0;i<1000000;i++){ sb.append( "\tSample ...

Java Puzzle : String concatenation

I have simple program to print date after month from now. 1: package com.test; 2:   3: import java.text.ParseException; 4: import java.text.SimpleDateFormat; 5: import java.util.Date; 6:   7: public class Test{ 8: public static void main(String[]args) throws ParseException{ 9: Date currentDate = new Date(); 10: int d = currentDate.getDate(); 11: int month = currentDate.getMonth()+1; 12: int year = currentDate.getYear() +1900; 13: //trying to update to next moth same date 14: Date nextMonthDate = new SimpleDateFormat( "dd-MM-yyyy" ) 15: .parse(d + "-" 16: + month+1 + "-" // increasing month by one to get next month 17: + year); 18: System.out.println( "Current Date = " + currentDate); 19: System.out.println( "Next Month Date = " +nextMonth...