What is a string in Java?
In Java, a String is an object that represents an immutable sequence of characters. The String class is part of the java.lang package, which is automatically imported into all Java programs.
Strings are used for storing text.
A String variable contains a collection of characters surrounded by double quotes (""):

Creating Strings in Java
String Literal:
- A string literal is a string created using double quotes. These strings are stored in the String Constant Pool to save memory and reuse objects.
String s1 = "Hello";
Using new Keyword:
- When we create a string using the new keyword, Java creates a new object in heap memory even if the same value already exists.
String s2 = new String("Hello");
Key Characteristic's **
**1. Strings are Immutable
- Once a String object is created, its value cannot be changed.
String s = "Hello";
s.concat(" World");
System.out.println(s);
Output:
Hello
2. String is a Class (not primitive)
- String is an object of String class from java.lang package.
String name = "Manoj";
3. Strings are Thread Safe
- Because they are immutable, they are automatically thread safe.
Reference
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://www.geeksforgeeks.org/java/strings-in-java/
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html


Top comments (0)