
Play Store Application link – https://play.google.com/store/apps/details?id=com.ideepro.java25hours
Learn with youtube video-
💡String:–
Generally string is a sequence of characters, but in java, strings are immutable objects. Immutable simply means unmodifiable/unchangeable.
💡Creation of String object:-
1:- By string literal
2:- By new keyword
Feature | String Literal | String Class’s Constructor |
---|---|---|
Stored in string pool | Yes | No |
Created at compile time | Yes | No |
Can be modified | No | Yes |
Can represent a string not known at compile time | No | Yes |
Memory usage | Low | High |
Access speed | Fast | Slow |
💡1:- String literal:-

String s1=”Welcome”; //1 object created
String s2=”Welcome”; //no object will be created
Description:- When a String is created, JVM checks it in string constant pool if it exists it returns reference else string object is submitted to pool.
💡2:- By new keyword:-
String s=new String(“Welcome”); //Creates 2 objects
String s1=new String(“Welcome”); //1 object
Description:- new String object is created in nonpool(heap memory) and literal “Welcome” is created in string constant pool. The variable s is reference variable for object created in nonpool.

Methods of String class– There are many functions which can be performed on string such as concatenation, trim, split, substring etc.
Note – Comparing two string objects –1- ==
with strings created using new
: Compares object references, so it returns false even if the content is the same.
2- ==
with string literals: Compares references, so it returns true because both literals refer to the same object in the string pool.
3- equals()
: Always compares the actual content of the strings, so it returns true if the content is the same, regardless of how the strings were created.
💡String Buffer & String Builder –
Quick comparison of String
, StringBuilder
, and StringBuffer
in Java:
Property | String | StringBuilder | StringBuffer |
---|---|---|---|
Class | String | StringBuilder | StringBuffer |
Mutability | Immutable | Mutable | Mutable |
Thread Safety | Thread-safe | Not thread-safe | Thread-safe |
Performance | Slower | Faster | Slower |
Methods | Fewer | More | Fewer |
Use Cases | When the value of the string is not going to change | When the value of the string will be modified multiple times and thread-safety is not a concern | When the value of the string will be modified multiple times and thread-safety is a concern |
Both classes are used to create mutable strings.
Unlike Strings these are stored in heap memory.
Both classes have same methods, but each method in StringBuffer is synchronized so StringBuffer is thread safe.
StringBuilder is fast in performance.
code samples are here
1-Mostly Used String methods-
package strn; public class ExStrings { public static void main(String[] args) { String nameLiteral = "kuldeep"; // 1 object creation String nameobject = new String("kaushik"); // 2 object creation System.out.println(nameLiteral.charAt(4)); System.out.println(nameLiteral.concat(nameobject)); String[] arr1 = nameLiteral.split("d"); System.out.println(arr1[0] + " and " + arr1[1]); System.out.println(nameLiteral.length()); String fortrim = " deep "; System.out.println(fortrim); System.out.println(fortrim.trim()); System.out.println(nameLiteral.equals(nameobject)); System.out.println(nameLiteral.indexOf("e")); System.out.println(nameLiteral.isEmpty()); System.out.println(nameLiteral.length()); System.out.println(nameLiteral.substring(4)); System.out.println(nameLiteral.substring(2, 5)); System.out.println(nameLiteral.compareTo(nameobject)); System.out.println(nameLiteral.contains("ul")); System.out.println(nameLiteral.replace("ul", "lu")); } }
2-Mostly Used StringBuilder/StringBuffer methods-(both classes have same methods)
package strn; public class ExStringB { public static void main(String[] args) { StringBuilder sb1=new StringBuilder("java"); System.out.println(sb1.reverse()); StringBuffer sb2=new StringBuffer("core"); System.out.println(sb2.reverse()); } }
Interview Questions —
- What is String in java?
- What is difference between String and String builder/buffer?