You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.
class 18:-
==========
What is a String in Java?
In Java, String is a class from the java.lang package that represents a sequence of characters. It is immutable, meaning once a String object is created, its value cannot be changed.
1. String Creation
======================
There are two main ways to create a string in Java:
a) Using string literal
========================
String str1 = "Hello";
Stored in String Constant Pool (SCP).
Reuses object if it already exists.
b) Using new keyword
======================
String str2 = new String("Hello");
Creates a new object in the heap, even if the value is the same and for the future purpose, it will also be created in SCP area if no value is already there. And, implicit reference will be given by JVM.
2. String is Immutable
========================
Once a String is created, you cannot change its characters. If you try to modify it, a new object is created instead.
String s = "Java";
s.concat(" Programming");
System.out.println(s); // Output: Java
Use s = s.concat(" Programming"); to get Java Programming.
Difference Between == and .equals() in Strings
==============================================
In Java, both == and .equals() are used to compare strings, but they serve different purposes and behave differently.
== Operator:
============
-The == operator is used to compare reference equality, i.e., whether two string variables point to the same memory location.
-It returns true only if both string references refer to the exact same object in memory.
-It does not compare the actual content or characters of the string.
-This can lead to unexpected results, especially when strings are created using the new keyword.
.equals() Method:
=================
The .equals() method is used to compare the actual content of two strings, i.e., it performs a character-by-character comparison.
It returns true if the contents of both strings are identical, even if they are stored in different memory locations.
This is the recommended way to compare string values in Java.
Please be informed that .equals() method in String overrides the equals() method of Object class. The Object class equals() method is meant for reference comparison.
Example
=======
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2); // true (same object in SCP)
System.out.println(s1 == s3); // false (s3 is a new object in heap)
System.out.println(s1.equals(s3)); // true (same value)
Explanation
===========
-s1 == s2
Both point to the same object in String Constant Pool, so it returns true.
-s1 == s3
s3 is explicitly created using new, so it is in heap memory.
-s1 is in String pool, hence different memory locations → returns false.
-s1.equals(s3)
Compares character-by-character values.
Since "Hello" equals "Hello", returns true.
What is Immutability in Java String?
Immutability means once an object is created, its state (data) cannot be changed. In Java, the String class is immutable, meaning:
After a String object is created, you cannot modify the characters inside it. Any operation that appears to change a string actually creates a new String object.
Why are Strings Immutable in Java?
1. Security
Strings are often used for file paths, database URLs, user credentials, etc.
Making them immutable ensures that once a value is assigned, it cannot be changed or hacked.
2. String Pooling
Java uses a String Constant Pool (SCP) to reuse string objects.
Immutable strings ensure the shared strings in the pool are safe to reuse without accidental modification.
StringBuilder explain in Detail:-
=================================
What is StringBuilder in Java?
StringBuilder is a mutable class in Java that is used to create and manipulate strings efficiently when multiple modifications (like append, insert, delete, etc.) are required.
It is part of the java.lang package.
Introduced in:Java 1.5
Why use StringBuilder?
Unlike String, which creates a new object with every change, StringBuilder modifies the existing object — improving performance.
Ideal for single-threaded environments where thread-safety is not a concern.
Is StringBuilder Immutable?
No, StringBuilder is not immutable.
In fact, its key feature is mutability — meaning the object’s internal character sequence can be changed after it is created.
Behavior of == and .equals() in StringBuilder
======================================
1. == Operator
Compares object references (i.e., memory locations).
Returns true only if both references point to the same object.
2. .equals() Method:
In StringBuilder, .equals() is not overridden.
It uses the default Object class implementation → behaves like ==.
Информация по комментариям в разработке