String
class in Java represents a sequence of characters enclosed in double quotes (" "
).
String
class is present in java.lang
package.
package java.lang;
public final class String extends Object implements Serializable, Comparable, CharSequence
{
// Fields
// Constructors
// Methods
}
int rollno = 101; // Primitive data type
String name = "Deepak"; // Reference type (object)
String s1 = "Hello";
String s2 = "Hello"; // s1 and s2 point to the same object in SCP
new
Keyword:
String s3 = new String("Hello"); // stored in heap
String
Class:
String
class with examples:
Method | Description | Example | Output |
---|---|---|---|
length() |
Returns the number of characters in the string. |
String s = "Java"; |
4 |
charAt(int index) |
Returns the character at the specified index (0-based). |
String s = "Java"; |
v |
substring(int begin, int end) |
Returns a substring from begin index (inclusive) to end index (exclusive). |
String s = "Programming"; |
Progra |
equals(Object obj) |
Compares two strings for equality (case-sensitive). |
String s1 = "Java"; |
true |
equalsIgnoreCase(String str) |
Compares two strings ignoring case. |
String s1 = "java"; |
true |
toUpperCase() |
Converts all characters to uppercase. |
String s = "hello"; |
HELLO |
toLowerCase() |
Converts all characters to lowercase. |
String s = "HELLO"; |
hello |
trim() |
Removes leading and trailing spaces. |
String s = " Java "; |
Java |
concat(String str) |
Concatenates (joins) two strings. |
String s1 = "Hello"; |
Hello World |
replace(char old, char new) |
Replaces all occurrences of a character with another. |
String s = "Java"; |
Jovo |
contains(CharSequence s) |
Checks if the string contains a sequence of characters. |
String s = "Programming"; |
true |
compareTo(String another) |
Compares strings lexicographically (alphabetical order). |
String s1 = "Apple"; |
-1 (since "Apple" < "Banana") |
intern() |
Returns the canonical representation of a string from the String Constant Pool (SCP). Helps save memory by avoiding duplicate String objects. |
String s1 = new String("Hello"); String s2 = s1.intern(); String s3 = "Hello"; System.out.println(s1 == s2); System.out.println(s2 == s3); |
false true
|
String
Class:
String
objects are immutable, meaning once a String
object is created, its value cannot be changed.
String
objects store text as UTF-16 encoded Unicode characters, allowing support for multiple languages and symbols.
String
objects are immutable, they are inherently thread-safe. Multiple threads can safely access the same string without the need for synchronization.
String
class is final
?
String
class final ensures that its methods cannot be overridden,
preserving its immutable behavior.
String
is widely used for sensitive data like usernames, passwords, file paths,
database URLs, and network connections.
If it were not final, someone could subclass it and compromise security.
String
objects are immutable and cannot be changed, they are automatically
thread-safe.
Declaring String
as final ensures no subclass can alter this behavior.
String
being immutable.
If String
were mutable, reusing objects in the pool would not be safe,
and performance benefits would be lost.
String
class caches its hashcode after the first calculation.
This is only possible because strings are immutable and final
ensures
no subclass can break this mechanism.
String
is heavily used in class loading, reflection, collections, etc.,
making it final ensures consistent and predictable behavior everywhere.
CharSequence
is a read-only interface that represents a sequence of characters.
String
, StringBuilder
, StringBuffer
and CharBuffer
implements the CharSequence
Interface.
length()
β Returns the number of characters.charAt(int index)
β Returns the character at the specified index.subSequence(int start, int end)
β Returns a subsequence of the character sequence.toString()
β Converts the sequence into a String
object.CharSequence seq = "Deepak";
System.out.println(seq.length()); // 6
System.out.println(seq.charAt(0)); // D
System.out.println(seq.subSequence(0, 4)); // Deep
System.out.println(seq.toString()); // Deepak
CharSequence
allows uniform access to different character sequence implementations without modifying them.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.