String in Java
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
The following is the declaration for java.lang.String class:
public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence
Important points for String class
Constructor Summary for java.lang.String class
There are a total of 15 for java.lang.String class, some will be deprecated soon.
String()
Initializes a newly created String object so that it represents an empty character sequence.
String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform's default charset.
String(byte[] ascii, int hibyte)
Deprecated. This method does not properly convert bytes into characters.
String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.
String(byte[] ascii, int hibyte, int offset, int count)
Deprecated. This method does not properly convert bytes into characters.
String(byte[] bytes, int offset, int length, String charsetName)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, int offset, int length, Charset charset)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, String charsetName)
Constructs a new String by decoding the specified array of bytes using the specified charset.
String(byte[] bytes, Charset charset)
Constructs a new String by decoding the specified array of bytes using the specified charset.
String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument.
String(int[] codePoints, int offset, int count)
Allocates a new String that contains characters from a subarray of the Unicode code point array argument.
String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument
String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument.
Importance of cases for java.lang.String class :
Case 1
String str=new String("Sara");
str.concat("HelloWorld");
System.out.println(str);//Sara
Once we create a String object we can't perform any changes in the existing object. If we are trying to perform any changes with those changes a new object will be created. This behavior is called the immutability of the String object.
StringBuffer sb=new StringBuffer("Sara");
sb.append("HelloWorld");
System.out.println(sb); //SaraHelloWorld
Once we created a StringBuffer object we can perform any changes in the existing object. This behavior is called the mutability of the StringBuffer object.
Case 2
String str1=new String("Sara");
String str2=new String("Sara");
System.out.println(str1==str2);//false
System.out.println(str1.equals(str2));//true
In String class .equals() method is overridden for content comparison hence if the content is same .equals() method returns true even though objects are different.
StringBuffer str1 = new StringBuffer("Sara");
StringBuffer str2 = new StringBuffer("Sara");
System.out.println(str1==str2);//false
System.out.println(str1.equals(str2));//false
In StringBuffer class .equals() method is not overridden for content comparison hence Object class .equals() method got executed which is always meant for reference comparison. Hence if objects are different .equals() method returns false even though content is same
Case 3
String str = new String("Sara");
In this case, two objects will be created one is on the heap the other one is SCP(String constant pool) and str is always pointing to the heap object.
String str = "Sara";
In this case, only one object will be created in SCP and s is always referring to that object
Importance of String constant pool (SCP) :
1). String constant pool is a specially designed memory area for the String literals/objects.
2). Instead of creating a separate object for every requirement, we can create only one object and we can reuse the same object for every requirement. This approach improves performance and memory utilization.
3). As a String object is the most commonly used object sun people provided a specially designed memory area like SCP to improve memory utilization and performance.
4). Object creation in SCP is always optional 1st JVM will check if any object already created with required content or not.
5). If it is already available then it will reuse existing objects instead of creating new objects. If it is not already there then only a new object will be created. Hence there is no chance of existing 2 objects with the same content on SCP that is duplicate objects are not allowed in SCP.
6). Garbage collector can't access SCP area hence even though object doesn't have any reference still that object is not eligible for GC if it is present in SCP.
7). All SCP objects will be destroyed at the time of JVM shutdown automatically.
String str1 = new String("Sara"); String str2 = new String("Sara"); String str3 = "Sara"; String str4 = "Sara";
8). Whenever we are using a new operator compulsory a new object will be created on the Heap. There may be a chance of existing two objects with the same content on the heap but there is no chance of existing two objects with the same content on SCP. i.e., duplicate objects possible in the heap but not in SCP.
Final vs immutability :
final modifier applicable for variables where as immutability concept applicable for objects.
If reference variable declared as final then we can't perform reassignment for the reference variable it doesn't mean we can't perform any change in that object.
That is by declaring a reference variable as final we won't get any immutability nature.
final and immutability both are different concepts.
Post/Questions related to String in Java
Java adding New Line in a StringJava String split() method Examples
String lastIndexOf() method examples in Java
String indexOf() method example in Java
String array in Java
How to check if an enum contains String
In this article, we have seen the String in Java.
0 Comments
Post a Comment