💻Step 20: String Handling -13th hour + code

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

FeatureString LiteralString Class’s Constructor
Stored in string poolYesNo
Created at compile timeYesNo
Can be modifiedNoYes
Can represent a string not known at compile timeNoYes
Memory usageLowHigh
Access speedFastSlow

💡1:- String literal:-

op1

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.

output1

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:

PropertyStringStringBuilderStringBuffer
ClassStringStringBuilderStringBuffer
MutabilityImmutableMutableMutable
Thread SafetyThread-safeNot thread-safeThread-safe
PerformanceSlowerFasterSlower
MethodsFewerMoreFewer
Use CasesWhen the value of the string is not going to changeWhen the value of the string will be modified multiple times and thread-safety is not a concernWhen 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"));
}
}

Run this Program ▶️

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());
}
}

Run this Program ▶️

Interview Questions —

  • What is String in java?
  • What is difference between String and String builder/buffer?

Leave a Reply

Your email address will not be published. Required fields are marked *