String is one of the most used class in java programing language , let see what makes it very useful class in java as well as its properties.
So lets Start with
1) What is String ?
So String is a Class in Java , which is basically the array of characters , In Java String is Class,not a primitive datatype as in C language.
Package Name - java.lang.String
2) How can you say that String is immutable in java ?
So we should first understand what is immutable means , So immutable means when any object is not permit itself to be modified or changed. so how string is immutable in java , lets see by exampleclass StringManupulation
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String str1= new String("Program");
str1.concat("In Java");
System.out.println(str1);
}
}
Output :- Program
Logic - As String is immutable it is not allowing the object to be modified as the object initial refer to Program and after concat it with another string , It is not allowing itself to modified
class StringManupulation
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String str1= new String("Program");
str1+=str1.concat("In Java");
System.out.println(str1);
}
}
Output : Program In Java
Logic:- As we are changing the reference of the Str1 to Str1+ "In java" Object so the String str1 is now pointing to different object hows valus is "Program In Java".
Difference btw string and string buffer ?
- String is immutable and StringBuffer is mutable
- immutable - means the string will not be modify after created
String object are cached in string pool and these strings are shared so changing values has risk.
security reasons-
- if string is mutable then user could have changed the path after logging.
- would have changed the db url
- Mutable string cause problem in reflection also.
Non Changing behaviour is called immutability
Example-
String s = new String("programinjava"); s.concat(".com"); System.out.println(s);
Output : programinjava
Second Example:-
String s1 = new String("programinjava"); String s2= new String ("programinjava"); System.out.println(s1==s2) System.out.println(s.equals(s2))
false true
In String class .equals Method meant for content check
Explanation:-
String s = new String("programinjava");
- 2 object created , One is in Heap area and other one is in String constant pool ,s is pointing to Heap Object
String s= "programinjava" ;
- 1 object created in string constant pool
- Garbage collector is not allowed to access String pool area.
Memory area in java
There are 5 memory area are there in java -
- Heap(garbage collected is applicable only for heap)
- Method Area (SCP is in Method area)
- Stack
- PC Register
- Native Method stacks
Whenever we use new operator with string , it will compulsory create object in heap and put it in Method Area
Example-
How many object will create ?
String s1 = new String("programinjava"); // 1 h 1 scp String s2 = new String("programinjava");// 1 h , pre scp String s3="programinjava";// previous scp String s4 ="programinjava";// previous scp There are total 3 object;// 2 in heap and 1 in scp
Runtime opertion on string will create object in heap
Examples-
String s = new String("programinjava"); s =s.concat(".com");Explanation:
Here ,
"programinjava" will be in heap and scp
s.concat will create one string constant in scp ".com"
and create object "programinjava.com" in heap area
NOTE1-For every string constant , one object will be placed in scp area.
NOTE2 - At Runtime if an object is required to placed only in heap area but not in scp area.
Mutable with examples
StringBuffer sb = new StringBuffer("programinjava"); sb.append(".com"); System.out.println(sb);Output: programinjava.com
StringBuffer sb1 = new StringBuffer("durga");
StringBuffer sb2 = new StringBuffer("durga"); System.out.println(sb1==sb2); //false System.out.println(sb1.equals(sb2) ); //false
In StringBuffer Class .equals method meant for reference check
If you have any problem in understanding this , Please let me know in Comment section.
Thanks For Reading
-Noeik