Post

String

βœ… String is reference

  • String is reference value
  • can create String as literal or as instance
1
2
String str1= "hello";  //literal
String str2 = new String("hello"); //instance
  • 🟰 two code is same

βœ… String and equals()

  • ==: compare memory
  • equals(): compare value
  • for comparing String, use equals()

  • βœ”οΈ String as instance
1
2
3
4
5
6
7
public static void main(String[] args) {
    String str1 = new String("hello");
    String str2 = new String("hello");

    System.out.println(str1 == str2); //false, different memory
    System.out.println(str1.equals(str2)); //true
}
  • ==: false
  • equals(): true

  • ❗️ String as literal
1
2
3
4
5
6
7
public static void main(String[] args) {
    String str1 = "hello";
    String str2 = "hello";

    System.out.println(str1 == str2); //true
    System.out.println(str1.equals(str2)); //true
}
  • ==: true
  • equals(): true

  • if String is literal, Java tries to use memory efficiently
  • Java has String Pool saved in heap
  • if same literal exists in String Pool, does not create again
  • thus, str1, str2 has SAME memory value
  • πŸ‘πŸ» save memory for same string literal value

βœ… String is immutable

  • once String value is set, CAN NOT change
  • immutable
  • if want to change, CREATE NEW STRING
1
2
3
4
5
6
7
8
//⭐️ String is immutable
String str = "hello";
str.concat("Java"); //try to add "Java"
System.out.println(str); //return hello, does not change

//⭐️ If want to change immutable string, create new String instance
String newStr = str.concat("Java");  //add "Java"
System.out.println(newStr); //return helloJava

βœ… Stringbuilder

  • used to change String without creating new string instance
  • NOT immutable

  • πŸ‘ŽπŸ» String is immutable side effect
  • πŸ‘ŽπŸ» everytime you want to change String, need to create a new string instance
1
2
3
String str = "A" + "B" + "C" + "D"
//to do this need to create A, AB, ABC, ABCD
// πŸ‘ŽπŸ» do not need A, AB, ABC
  • πŸ‘ŽπŸ» not efficient memory use
  • πŸ’Š string builder


  • βœ”οΈ StringBuilder is NOT immutable
  • can change without creating new String instance
  • πŸ‘πŸ» String을 자주 바꿀거라면 String Builder을 μ‚¬μš©ν•˜λŠ”κ²Œ 더 효과적
1
2
3
4
5
6
7
8
9
10
11
12
13
StringBuilder sb = new StringBuilder();
sb.append("A");
sb.append("B");
sb.append("C");
sb.append("D");  //ABCD

sb.insert(4, "Java"); //ABCDJava

sb.delete(4, 8); //ABCD

sb.reverse(); //DCBA

String result = sb.toString() //이제 더 μ•ˆ λ°”κΏ€κΊΌλ©΄ toString으둜 string으둜 λ°”κΎΈκΈ°
  • βœ”οΈ When is StringBuilder used?
  • loops
  • changing a long String many many times

  • πŸ†š StringBuffer: synchronization, in multi-thread, but slower
  • StringBuilder: can run at same time, might have problems in multi-thread, but faster

βœ… StringBuilder and Method Chaining

  • βœ”οΈ Method Chaining
  • chain methods into one line
1
2
3
4
5
6
7
8
9
ValueAdder adder = new ValueAdder();
adder.add(1);
adder.add(2);
adder.add(3);
int result = adder.getValue();

//method chaining
ValueAdder adder = new ValueAdder();
int result = adder.add(1).add(2).add(3).getValue();
  • βœ”οΈ StringBuilder supports method chaining
1
2
3
4
5
6
7
8
StringBuilder sb = new StringBuilder();
sb.append("A");
sb.append("B");
sb.append("C");
sb.append("D");

//method chaining
sb.append("A").append("B").append("C").append("D");

βœ…

βœ…

βœ…

βœ…

βœ…

This post is licensed under CC BY 4.0 by the author.