String
โ String is reference
- String is reference value
- can create String as
literal
or asinstance
1
2
String str1= "hello"; //literal
String str2 = new String("hello"); //instance
- ๐ฐ two code is same
โ String and equals()
==
: compare memoryequals()
: compare valuefor comparing
String
, useequals()
- โ๏ธ 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
}
==
: falseequals()
: 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
}
==
: trueequals()
: true- if String is
literal
, Java tries to use memory efficiently - Java has
String Pool
saved inheap
- if same
literal
exists inString 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 createa 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.