|
| 1 | +String in java |
| 2 | + |
| 3 | +1. There are 3 classes that can be used to create string in java |
| 4 | + - String - StringBuffer - StringBuilder |
| 5 | +2. String in java is an Object of java.lang.String |
| 6 | + |
| 7 | +String Class (java.lang.String) |
| 8 | +1. String is immutable hence cannot be changed changing a String value creates a new object because if not so then changing a string |
| 9 | + would change it for all the literals referring to the string. |
| 10 | + Concatenating string to a string does not change the value of the string. |
| 11 | + We can assign the string to the concatenated string. |
| 12 | +2. String acts like a pool to which String type object refer to. |
| 13 | + Example - String a ="Apple"; |
| 14 | + String b ="Apple"; |
| 15 | + Does not create two String object since "Apple" was already in memory b simply refers to "Apple" without creating new object. |
| 16 | +3. String created through new keyword creates a new String in the memory. |
| 17 | + |
| 18 | +Comparing String |
| 19 | + |
| 20 | +1. equals() or equalsIgnoreCase compares the value of the string. |
| 21 | +2. == operator compares the reference of the string. |
| 22 | +3. compareTo() return 0,1,-1 if the string are equal,greater or smaller lexicographically. |
| 23 | + |
| 24 | +Substring of a String |
| 25 | + |
| 26 | +public String substring(int startIndex) |
| 27 | +public String substring(int startIndex, int endIndex) |
| 28 | + |
| 29 | +startIndex is inclusive |
| 30 | +endIndex is exclusive |
| 31 | + |
| 32 | +String methods() |
| 33 | +1. toUpperCase() |
| 34 | +2. length() |
| 35 | +3. trim() |
| 36 | +4. valueOf() |
| 37 | +5. replace() |
| 38 | +6. intern() |
| 39 | + |
| 40 | + |
| 41 | +StringBuffer class (mutable,thread-safe) |
| 42 | +1. insert(int,String) |
| 43 | +2. append(String) |
| 44 | +3. replace(int,int,String) |
| 45 | +4. delete() |
| 46 | +5. reverse() |
| 47 | +6. capacity() |
| 48 | +7. charAt() |
| 49 | +8. length() |
| 50 | +9. substring() |
| 51 | + |
| 52 | + |
| 53 | +StringBuilder class (mutable,non-synchronized) |
| 54 | +methods same as StringBuffer |
0 commit comments