String Class in Java
String class in Java:
String:
1. String is a sequence of character enclosed within double quote.
2. String class is a non- premitive data type.
3. String class present in a java.lang package.
4. String is a immutable
i.e when we create a String object then we can't change in it.
that means we can assign new value to that String object but we can't perform any change in previous value.
for e.g.
String str = "nil"; // str pointing to the nil value.
String str = "java";
// now str pointing to the java value but we cannot assign java with nil we create new object java . nil is still present in String Constant Pool .
String creation:
We can Create String in 2 ways:
1. using new keyword
2. without using new keyword
1] when we create String using new keyword then 2 objects are created.
which is first in heap area & other created in SCP (String Constant Pool) Area
for e.g. String str = new String("java");
2] when we create String without using new keyword then 1 objects are created.
that object created in only SCP (String Constant Pool) Area
for e.g. String str = "java";
NOTE:
SCP is a String Constant Pool which is present inside method area before 1.6 version
After 1.7 version onwards SCP present inside heap area.
In SCP , if we create Two String Object with same value then it will store only one value in
SCP and both String objects pointing to the same value.
Comments
Post a Comment
hey