String : Immutable, String Pool, String Buffer, String Builder, String Methods
String: it is basically an Object which represents a sequence of characters.
The Java platform provides the String class to create and manipulate strings. An array of characters works same as java string.
For ex:
Unlike C and C++ , String does not terminate with null character.
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
String is immutable i.e. it can not be changed once created, on changing new instance is created.
To overcome this two more classes are there in java : StringBuffer and StringBuilder. All these are in the same package: java.lang
Creating String : We can create string in two ways: 1) String literal 2) Using new keyword
String literal :
Java String literal is created by using double quotes. For Example: String str = "Hello";
Every time a string literal is created, first of all JVM checks the string constant pool . If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:
Using new keyword: it is like creating any object in java. For example : String str=new String("Hello");
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 str will refer to the object in heap(non pool).
Some useful String methods with description :
What is String POOL:
String Pool is a pool of Strings stored in Java Heap Memory.
Because String objects are immutable, it's safe for multiple references to "share" the same String object . Whenever a new instance of an already known Strings is needed, then already existing object reference is returned.
This way it helps to manage the memory efficiently.
Example :
* String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.
You can add a string to the pool by calling
These two classes are almost the same (they have the same API). The difference between them is that StringBuilder is NOT thread safe and it is faster.
Both classes are defined because they implement in a more efficient manner (memory) processing of string values. One reason is that their values are not stored in String Constant Pool and they behave like normal object – when the object is not references is destroyed by GC and it will not remain in String Constant Pool.
The idea behind StringBuilder and StringBuffer is to provide the means for efficient I/O operations with large streams.
StringBuilder and StringBuffer instances are created using only new operator and NOT with = like Strings:
Example :
If you want to modify the value of a StringBuilder or StringBuffer you can use the methods from this classes. Most used methods are:
Another fact about StringBuilder or StringBuffer is that you can call multiple methods – chained methods on the same object. Chained methods affect the calling object and they are executed from left to right.
String: it is basically an Object which represents a sequence of characters.
The Java platform provides the String class to create and manipulate strings. An array of characters works same as java string.
For ex:
char [] c = {'j','a','v','a'};
String s = new String(s);
or simply :
String s = "java";
Unlike C and C++ , String does not terminate with null character.
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
String is immutable i.e. it can not be changed once created, on changing new instance is created.
To overcome this two more classes are there in java : StringBuffer and StringBuilder. All these are in the same package: java.lang
Creating String : We can create string in two ways: 1) String literal 2) Using new keyword
String literal :
Java String literal is created by using double quotes. For Example: String str = "Hello";
Every time a string literal is created, first of all JVM checks the string constant pool . If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:
String s1="Hello"; String s2="Hello"; // previous object reference will be returned. New object will not be created.
Using new keyword: it is like creating any object in java. For example : String str=new String("Hello");
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 str will refer to the object in heap(non pool).
Some useful String methods with description :
| No. | Method | Description |
|---|---|---|
| 1 | char charAt(int index) | returns char value for the particular index |
| 2 | int length() | returns string length |
| 3 | static String format(String format, Object... args) | returns formatted string |
| 4 | static String format(Locale l, String format, Object... args) | returns formatted string with given locale |
| 5 | String substring(int beginIndex) | returns substring for given begin index |
| 6 | String substring(int beginIndex, int endIndex) | returns substring for given begin index and end index |
| 7 | boolean contains(CharSequence s) | returns true or false after matching the sequence of char value |
| 8 | static String join(CharSequence delimiter, CharSequence... elements) | returns a joined string |
| 9 | static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) | returns a joined string |
| 10 | String concat(String str) | checks the equality of string with object |
| 11 | boolean isEmpty() | checks if string is empty |
| 12 | boolean equals(Object another) | concatinates specified string |
| 13 | String replace(char old, char new) | replaces all occurrences of specified char value |
| 14 | String replace(CharSequence old, CharSequence new) | replaces all occurrences of specified CharSequence |
| 15 | String trim() | returns trimmed string omitting leading and trailing spaces |
| 16 | String split(String regex) | returns splitted string matching regex |
| 17 | String split(String regex, int limit) | returns splitted string matching regex and limit |
| 18 | String intern() | returns interned string |
| 19 | int indexOf(int ch) | returns specified char value index |
| 20 | int indexOf(int ch, int fromIndex) | returns specified char value index starting with given index |
| 21 | int indexOf(String substring) | returns specified substring index |
| 22 | int indexOf(String substring, int fromIndex) | returns specified substring index starting with given index |
| 23 | String toLowerCase() | returns string in lowercase. |
| 24 | String toLowerCase(Locale l) | returns string in lowercase using specified locale. |
| 25 | String toUpperCase() | returns string in uppercase. |
| 26 | String toUpperCase(Locale l) | returns string in uppercase using specified locale. |
What is String POOL:
String Pool is a pool of Strings stored in Java Heap Memory.
Because String objects are immutable, it's safe for multiple references to "share" the same String object . Whenever a new instance of an already known Strings is needed, then already existing object reference is returned.
This way it helps to manage the memory efficiently.
Example :
String s1 = "test";
String s2 = new String("test"); // "new String" guarantees a different object
String s3 = "test";
System.out.println(s1 == s2); // prints false as both object does not points to same refrence
System.out.println(s1 == s3); // prints true , because string pool return the same reference
* String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.
You can add a string to the pool by calling
String.intern(), which will give you back the pooled version of the string . So , you can also put a string object created using new keyword into String pool by using intern as shown belows2 = s2.intern(); System.out.println(s1 == s2); // should print true
What are StringBuilder and StringBuffer
These two classes are almost the same (they have the same API). The difference between them is that StringBuilder is NOT thread safe and it is faster.
Both classes are defined because they implement in a more efficient manner (memory) processing of string values. One reason is that their values are not stored in String Constant Pool and they behave like normal object – when the object is not references is destroyed by GC and it will not remain in String Constant Pool.
The idea behind StringBuilder and StringBuffer is to provide the means for efficient I/O operations with large streams.
StringBuilder and StringBuffer instances are created using only new operator and NOT with = like Strings:
Example :
StringBuilder sbuild = new StringBuilder("Hello");
StringBuffer sbuf = new StringBuffer("Hello Java !");
If you want to modify the value of a StringBuilder or StringBuffer you can use the methods from this classes. Most used methods are:
| Method | Description |
| append() | adds the argument to the end of the current object |
| delete() | deletes chars between a start and end index |
| insert() | inserts a value at a given offset |
| reverse() | reverse the value of the current object |
| toString() | returns the value of the StringBuilder or StringBuffer object |
Another fact about StringBuilder or StringBuffer is that you can call multiple methods – chained methods on the same object. Chained methods affect the calling object and they are executed from left to right.
Comments
Post a Comment