String class is encapsulated in java.lang package. every string that u create is an actually object of type String .
String object is that Objects are immutable i.e means once string object is created it can't be altered .
What is an Immutable Object ?
An object whose state can't be changed after it is created is known as immutable object.String,Integer, like all wrapper classes .
creating immutable class :
public final String Test
{
final String str;
Test(String s)
{
this.str = s;
}
public String get()
{
return str;
}
}
Note : Test is an immutable class its state cannot be changed .
Creating a String Object is the following ways.
1) Using literal : enclosed with " " (double quotes).
String str1 = "Hello" ;
2) Using another String object :
String str2 = new String (str1);
3) Using new Keyword :
String str3 = new String ("JAVA");
4) Using + Operator (Concatenation)
String str4 = str1+str2 ;
or
String str5 = "Hello" +"JAVA ";
Each time you creates a String pool(literal) , the jvm checks the string pool first . if the already exists in the pool . a reference to the pool instance is returned .
If String does not existing in the pool , new string object is created ,and placed in the pool . String objects are stored in special memory area known as String constantPool(string literal pool) in side the heap memory .
String str = "Hello" ;
And we created as the same object with same string ,then reference of the string literal already present in string pool is returned .
String str = str ;
But if we change the new String its reference should be modified .
str2 = str2.concat("world");
Concatenating String :
1 ) Using concat() method
2 ) Use + operator
1) Using concat() method:
String s = "Hello";
String str = "JAVA";
String str2 = s.concat(str);
String str1 = "Hello".concat("JAVA"); //works with string literals too.
2) Using + operator :
String str1 = "RAHUL";
String str2 = "DRAVID";
String s3 = str1+str2;
String st ="RAHUL"+"DRAVID";
String Comparison:
1) Using equals() method
2) Using == operator
3) By CompareTo() method
Using equals() method:
equal method compares two strings for equality .
Syntax : boolean equals(Object str)
it compares the content of 2 strings . it will return true if string matches , else return false .
String s = "Hello ";
String s1 = "Hello";
String s2 ="Hello";
s1.equals(s2);//true
s.equals(s1); // false
Using == operator:
String s1 ="Kavya";
String s2 ="Kavya";
String s3 = "Kavya";
test(s1==s2); //true
test(s1==s3) ; //false
By CompareTo() method:
Syntax : int compareTo(String str)
To use this function you have to implement from Comparable interface . compareTo() the only function in comparable interface .
String s1 = "Sachin";
String s2 = "Tendulkar";
String s3 = "Sachin";
s1.compareTo(s2); // returns -1 becoz s1<s2
s1.compareTo(s3); // returns 0 becoz s1==s3
s2.compareTo(s1) ; //returns 1 becoz s2>s1
String object is that Objects are immutable i.e means once string object is created it can't be altered .
What is an Immutable Object ?
An object whose state can't be changed after it is created is known as immutable object.String,Integer, like all wrapper classes .
creating immutable class :
public final String Test
{
final String str;
Test(String s)
{
this.str = s;
}
public String get()
{
return str;
}
}
Note : Test is an immutable class its state cannot be changed .
Creating a String Object is the following ways.
1) Using literal : enclosed with " " (double quotes).
String str1 = "Hello" ;
2) Using another String object :
String str2 = new String (str1);
3) Using new Keyword :
String str3 = new String ("JAVA");
4) Using + Operator (Concatenation)
String str4 = str1+str2 ;
or
String str5 = "Hello" +"JAVA ";
Each time you creates a String pool(literal) , the jvm checks the string pool first . if the already exists in the pool . a reference to the pool instance is returned .
If String does not existing in the pool , new string object is created ,and placed in the pool . String objects are stored in special memory area known as String constantPool(string literal pool) in side the heap memory .
String str = "Hello" ;
And we created as the same object with same string ,then reference of the string literal already present in string pool is returned .
String str = str ;
str2 = str2.concat("world");
Concatenating String :
1 ) Using concat() method
2 ) Use + operator
1) Using concat() method:
String s = "Hello";
String str = "JAVA";
String str2 = s.concat(str);
String str1 = "Hello".concat("JAVA"); //works with string literals too.
2) Using + operator :
String str1 = "RAHUL";
String str2 = "DRAVID";
String s3 = str1+str2;
String st ="RAHUL"+"DRAVID";
String Comparison:
1) Using equals() method
2) Using == operator
3) By CompareTo() method
Using equals() method:
equal method compares two strings for equality .
Syntax : boolean equals(Object str)
it compares the content of 2 strings . it will return true if string matches , else return false .
String s = "Hello ";
String s1 = "Hello";
String s2 ="Hello";
s1.equals(s2);//true
s.equals(s1); // false
Using == operator:
String s1 ="Kavya";
String s2 ="Kavya";
String s3 = "Kavya";
test(s1==s2); //true
test(s1==s3) ; //false
By CompareTo() method:
Syntax : int compareTo(String str)
To use this function you have to implement from Comparable interface . compareTo() the only function in comparable interface .
String s1 = "Sachin";
String s2 = "Tendulkar";
String s3 = "Sachin";
s1.compareTo(s2); // returns -1 becoz s1<s2
s1.compareTo(s3); // returns 0 becoz s1==s3
s2.compareTo(s1) ; //returns 1 becoz s2>s1
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Product prototyping during this lockdown period everyone can use it wisely.
icp-cat training