- A String = sequence of characters (text)
Example:
String name = "Maha";
Here, "Maha" is a string.
- Strings are IMMUTABLE
Once created, they cannot be changed
Example:
String str = "Hello";
str.concat(" World");
System.out.println(str);
Output:
Hello
Why?
concat() creates a new string
Original string remains unchanged
✔ Correct way:
str = str.concat(" World");
- String is NOT a primitive
Unlike int, float, or char, a String is actually a class (object) in Java.
That means:
It has methods
It behaves differently from basic data types
🔹 Ways to create String
1_. Literal (most used)_
String a = "Hello";
Stored in String Pool (memory efficient)
2._ Using new_
String b = new String("Hello");
Creates a new object every time (less efficient)
Top comments (0)