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.