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.