Stringby
Stringby
Java
String
v/s
StringBuilder
v/s
StringBuffer
@techwithvishalraj
String
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
String s4 = new String("hello");
String s5 = new String("hello").intern();
s1 == s2 -------> true
s3 == s4 -------> false
s1 == s3 -------> false
s1.equals(s2) -------> true
s3.equals(s4) -------> true
s1.equals(s3) -------> true
s1.equals(s5) -------> false
String
@techwithvishalraj
int i = 200;
String s = "hello";
String s1 = "hello";
String s2 = new String("hello").intern();
String s3 = new String("hello");
String s4 = new String("hello"); Heap
String pool
Stack
hello
i=200
hello
s
s1
hello
s2
hello
s3
s4
@techwithvishalraj
StringBuffer
StringBuffer class is used to create mutable (modifiable) objects.
StringBuffer is synchronized i.e. thread safe. It means two threads
can't call the methods of StringBuffer simultaneously.
All methods of StringBuffer are synchronized.
StringBuffer is less efficient than StringBuilder.
StringBuffer was introduced in Java 1.0
String class overrides the equals() method of Object class. So you can
compare the contents of two strings by equals() method.
StringBuilder
String StringBuffer and StringBuilder
charAt(int index) charAt(int index)
length() length()
substring(int start) substring(int start)
substring(int start, int end) substring(int start, int end)
insert(int offset, String str)
append(String str)
deleteCharAt(int index)
delete(int start, int end)
indexOf(String str), indexOf(String str),
lastIndexOf(String str) lastIndexOf(String str)
startsWith(String prefix)
endsWith(String suffix)
replace(char old, char new) replace(int start, int end, String str)
isBlank(), isEmpty() isEmpty()
matches("[A-Za-z]*") ,
contains("asd")
reverse()
equals(Object anObject)
toLowerCase(), toUpperCase()
toString()
trim(), strip(), stripLeading()
stripTrailing()
@techwithvishalraj
Thank
you!
vishal-bramhankar
techwithvishalraj
Vishall0317