✓String is a inbuilt class in Java which is used to store string values.
✓A group of characters written within the double quoted is called as string value or string literal.
ex: String s="Vijay";
✓String class present in "java.lang.*" package.
There are two ways to create string object,
1. By string literal: It is created by using double quotes.
ex: String s= "Vijay";
2. By new keyword: String is created by using a Keyword "new".
ex: String s=new String("Vijay");
✓String objects are stored in special memory of Heap called "String pool". i.e. String objects are stored in string pool of heap area.
String pool as two parts,
1. Constant pool :
- Any string objects are created without using new operator will be stored in constant pool.
- within the constant pool no duplicates are allowed.
2. Non -constant pool :
- Any string object is created using new operator will be stored in non constant pool.
- within the non constant pool duplicates are allowed.
✓The string objects can also be created using concatenation operator.
ex: String s="Vijay";
String s1="blog";
s+ "tech" -> Stored in non-constant pool as Vijaytech
"Mr" + s-> Stored in non-constant pool as MrVijay
"Vijay" + "tech" -> Stored in constant-pool as Vijaytech
s+s1 -> Stored in Non-constant pool as Vijayblog
✓ example to show in which pool string objects will store.
String s= "Vijay";
String s1= new String("Vijay");
- if(s==s1) - false (because s will store in constant pool and s1 store in non-constant pool. so, both objects address will be different)
String s2= "Vijay";
- if(s==s2)- true( because both points to same object and shares same address)
String s3= new String("Vijay");
- if(s1==s3) - false(because two Vijay objects created in non-constant pool and have different addresses)
Note:
- If we use == operator, it compares address of two strings.
- If we use,equals(), it compares content of two strings.
✓ String is immutable class. Explain?
If we try to change the existing string object,then instead of changing the old object a new object will be created leaving the old object object as dereferenced.
✓ Below are the rules to make the classes as immutable.
1. Final class, the calss which is declared as final it cannot be extended.
2. All fields/members should be private so that direct Access to the fields will be blocked.
3. No set methods are allowed and only get methods are allowed.
ex:public final class Immutable {//final class
private final String s;///private member
Immutable(final String s){
this.s=s;
}
public final String getName() {//only get() and set()
return s;
}
public static void main(String[] args) {
Immutable im=new Immutable("Vijay");
System.out.println(im.getName());
}
}
String Class inbuilt methods:
1. charAt(index) : returns char value at specified index.
2. concat() : Appends string to the end of another string i.e. combines two strings.
3. contains(): Checks whether a string contains sequence of characters or not.
4. compareToIgnoreCase(): Compare two strings ignoring case differences.
5. equals : Compares two strings. Returns true if strings are equal and false if not.
6. equalsignorecase()
7. hashcode(): Returns the hashcode of string.
8. index of(): returns the position of first found occurence of specified char's in a string
9. length(): Returns the length of a string.
10. isEmpty(): Checks whether string is empty are not.
11. toCharArray(): Converts the string to new char array.
12. toString(): Returns the value of string object.
13.toUpperCase(): converts all char's to uppercase
14. to lowercase(): converts all char's to lowercase
StringBuffer and StringBuilder:
To create mutable objects in string, we will go for StringBuffer and StringBuilder classes.
✓In mutable objects, value can be changed after installation.
✓ we can change the object value such as fields and states after the object is created.
ex: java.util.Date
StringBuffer
StringBuilder
No comments:
Post a Comment