Skip to content

Commit fa47caf

Browse files
authored
Create 5 Golden Rules of Strings in Java.md
1 parent 8503130 commit fa47caf

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## 5 Golden Rules of Strings in Java
2+
3+
4+
1. If we create a string without using `new` operator the string object will be created in the **constant pool**.
5+
```
6+
String s1 = "Spring";
7+
```
8+
9+
2. If we create a string using `new` operator the string object will be created in the **non-constant pool**.
10+
```
11+
String s2 = new String("Java");
12+
```
13+
14+
3. If we create a string by **concatinating two raw strings**, the string object will be created in the **constant pool**.
15+
```
16+
String s3 = "Mark" + "Down";
17+
```
18+
19+
4. If we crate a string by **concatinating the two strings by using the reference variables**, then the string object will be created in the **non-constant pool**.
20+
```
21+
String s4 = "Two";
22+
String s5 = "Thousand";
23+
String s6 = s4 + s5;
24+
```
25+
26+
5. If we assign **string reference variable to another string reference variable**, the refence would be copied, then the **both reference variable refer to the same string object**.
27+
```
28+
String s7 = "Keyboard";
29+
String s8 = s7;
30+
```

0 commit comments

Comments
 (0)