Introduction of String
- Get link
- X
- Other Apps
Introduction of String
In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string.
For example:
char[] ch={’l',’e',’a',’r',’w',’i',’t',’h',’r',’e’,’s’,’h’,’u’};
String s=new String(ch);
is same as:
String s=”learnwithreshu";
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
What is String in java?
Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. then Question arises that
How to create a string object?
There are two ways to create String object:
By string literal
By new keyword
String Literal
Java String literal is created by using double quotes.
For Example:
String s=”Reshu";
Each time you 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. For example:
String s1=“Welcome";
String s2=“Welcome";
s2 doesn't create a new instance
String objects are stored in a special
memory area known as the
"string constant pool".
By new keyword
In such case, JVM will create a new string object in normal (non-pool) heap memory, and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in a heap (non-pool).
String s=new String("Welcome"); //creates two objects and one reference variable
If you like this article please subscribe my blogs to encourage me to create more content like this.
If you want to understand this in hindi Please click the play button.
Thanks for Watching!!!!!!
Best Regards
Learn with Reshu
#learnwithreshu
- Get link
- X
- Other Apps
Comments
Post a Comment
Please do not comment any spam link in the comment box