0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Java Tips

Posted at

String
 ・文字列の操作は、StringBuilderを利用することでパフォーマンスが向上
  →特にループ内で+での文字列結合を行わないこと

bad.java
    static String concat(String[] array) {
        String result = "";
        for (String s : array) {
            result += s;  // 
        }
        return result;
    }

better.java
    static String concat(String[] array) {
        StringBuiler result = new StringBuiler();
        for (String s : array) {
            result.append(s);
        }
        return result.toString();
    }

※StringBuiler 比較は、注意が必要

StringBuiler_equals.java
        StringBuilder sb1 = new StringBuilder("ABC");
        StringBuilder sb2 = new StringBuilder("ABC");

        if(sb1 == sb2){
            System.out.println("sb1 == sb2" + " is OK.");
        }else{
            System.out.println("sb1 == sb2" + " is NG.");//FALSE
        }

        if(sb1.equals(sb2)){
            System.out.println("sb1 equals sb2" + " is OK.");
        }else{
            System.out.println("sb1 equals sb2" + " is NG.");//FALSE
        }

        if(sb1.toString().contentEquals(sb2)){
            System.out.println("sb1 contentEquals sb2" + " is OK.");//TRUE
        }else{
            System.out.println("sb1 contentEquals sb2" + " is NG.");
        }   

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?