Skip to content

Commit 8e3cb22

Browse files
committed
Immutability of String
1 parent 17f76ca commit 8e3cb22

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Modifying String object creates Garbage objects.
3+
* Java recommends to use either StringBuffer or StringBuilder.
4+
*
5+
* @author L.Gobinath
6+
*/
7+
public class BestPractice {
8+
public static void main(String[] args) {
9+
StringBuilder builder = new StringBuilder();
10+
11+
for (int i = 0; i < 10; i++) {
12+
builder.append(i);
13+
}
14+
String result = builder.toString();
15+
System.out.println(result);
16+
}
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* The == operator is used to compare two primitive data types.
3+
* If it is used with references, it checks whether two references are refering the same object.
4+
*
5+
* @author L.Gobinath
6+
*/
7+
public class EqualityOperator {
8+
public static void main(String[] args) {
9+
Student stu1 = new Student(100);
10+
Student stu2 = new Student(100);
11+
Student stu3 = stu1;
12+
if (stu1 == stu2) { // false
13+
System.out.println("stu1 == stu2");
14+
}
15+
if (stu1.equals(stu2)) { // true
16+
System.out.println("stu1 equals stu2");
17+
}
18+
if (stu1 == stu3) { // true
19+
System.out.println("stu1 == stu3");
20+
}
21+
}
22+
}
23+
24+
class Student {
25+
int index;
26+
27+
public Student(int index) {
28+
this.index = index;
29+
}
30+
31+
@Override
32+
public boolean equals(Object obj) {
33+
if (obj instanceof Student) {
34+
Student stu = (Student) obj;
35+
if (stu.index == this.index) {
36+
return true;
37+
}
38+
}
39+
return false;
40+
}
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Modifying String object creates Garbage objects.
3+
* It is not recommended to modify a String object so many times.
4+
* This code is an example of bad practice in Java.
5+
*
6+
* @author L.Gobinath
7+
*/
8+
public class GarbageGenerator {
9+
public static void main(String[] args) {
10+
String result = "";
11+
12+
for (int i = 0; i < 10; i++) {
13+
// Every time creates a new object
14+
result += i;
15+
}
16+
System.out.println(result);
17+
}
18+
}

stringimmutability/StringPool.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Same "Java" object is shared by both references.
3+
*
4+
* @author L.Gobinath
5+
*/
6+
public class StringPool {
7+
public static void main(String[] args) {
8+
String x = "Java";
9+
String y = "Java";
10+
11+
System.out.println(x == y); //true
12+
System.out.println(x.toLowerCase()); // java
13+
System.out.println(x); // Java
14+
System.out.println(y); // Java
15+
x = x.toLowerCase(); // Update the reference
16+
System.out.println(x); // java
17+
System.out.println(y); // Java
18+
}
19+
}

0 commit comments

Comments
 (0)