-3

I am trying to see the execution speed of StringBuffer and StringBuilder but every time I am getting different results.

CASE ONE

     long startTime=System.currentTimeMillis();     
     StringBuilder builder=new StringBuilder();
     StringBuffer buffer=new StringBuffer();

    for(int i=0;i<1000;i++){
            builder.append("Okay");}

    System.out.println("Builder Elapsed Time  :"+(System.currentTimeMillis()- 
    startTime));
    
    long bufferStartTime=System.currentTimeMillis();
    for(int i=0;i<1000;i++){
    buffer.append("Okay");}
    System.out.println("Buffer Elapsed Time  :"+(System.currentTimeMillis()- 
    bufferStartTime));
    
    OUTPUT:-
    Builder Elapsed Time:1
    Buffer Elapsed Time :0

CASE TWO

    StringBuilder builder=new StringBuilder();
    StringBuffer buffer=new StringBuffer();
    long startTime=System.currentTimeMillis();
    for(int i=0;i<1000;i++) {
    buffer.append("Okay");
    }
    System.out.println("Buffer Elapsed Time  :"+(System.currentTimeMillis()- 
    startTime));    
    long builderStartTime=System.currentTimeMillis();
    for(int i=0;i<1000;i++){
    builder.append("Okay");}
    System.out.println("Builder Elapsed Time  :"+(System.currentTimeMillis()- 
    builderStartTime));

   OUTPUT:-
   Buffer Elapsed Time:1
   Builder Elapsed Time:0

I have tried two cases but getting different results from anyone please help me why it's happening.

kundan kamal
  • 674
  • 7
  • 16

2 Answers2

0

No, you can not override static method in Java, though you can declare method with same signature in sub class.

As per Java coding convention, static methods should be accessed by class name rather than object. In short Static method can be overloaded, but can not be overridden in Java

(source)

Community
  • 1
  • 1
Nani
  • 11
  • 5
0

No, you can not override static method in Java, though you can declare method with same signature in sub class. It won't be overridden in exact sense, instead that is called method hiding. But at same time, you can overload static methods in Java, there is nothing wrong declaring static methods with same name, but different arguments

Read more: https://javarevisited.blogspot.com/2013/03/can-we-overload-and-override-static-method-java.html#ixzz6DXBkqxOh

juzraai
  • 5,693
  • 8
  • 33
  • 47
Mehedi Hasan
  • 137
  • 1
  • 5