01. What is a String in Java?
String is a non-Primitive data type.
String is a class.
The package name is java.lang
A string is a sequence of characters.
But in Java, a string is an object that represents a sequence of characters.
The Java.lang. -The String class is used to create a string object.
02. How to create a string object?
There are two ways to create a String object:
- By string literal
- By a new keyword
03. How to create a String Literal in Java?
A Java String literal is created by using double quotes.
For Example:
String s = "welcome"; String Literal.
04. Why is String immutable in Java?
String objects are immutable.
Immutable means unchangeable.
Once a string object is created, it cannot be changed.
05. String Literals (Without the new keyword):
Each time we create a string literal, the JVM checks the "string constant pool" first.
If the string already exists in the pool, a reference to the pooled instance is returned.
If the string doesn't exist in the pool, a new string instance is created and placed in the pool.
Note: String objects are stored in a special memory area known as the "string constant pool".
06. Advantage of string
immutable
Reduce memory usage by reusing existing strings.
**07. Disadvantage of string:
**Memory Consumption
Uses extra memory due to immutability
Performance Overhead
Slower for modifications & concatenation
Security Risks
Encoding Issues
08. How to create a string (With a new keyword)?
Creates a new object in heap memory.
String s1= new String("Hello")
Top comments (0)