提问者:小点点

StringBuffer和StringBuilder哪个在执行过程中花费更多的时间?[副本]


     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
    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

共1个答案

匿名用户

不,您不能在Java中重写静态方法,尽管您可以在子类中声明具有相同签名的方法。

按照Java编码惯例,静态方法应该通过类名而不是对象来访问。简而言之,Java中静态方法可以重载,但不能重写

(资料来源)