Any thing in double quotation " is called as string object in java.
We can create strings in 2 ways
String str2=new string("hi");
-> Strings are immutable(unchangeable) objects.
-> Sometimes strings will be stored in string constant pool and sometimes in the heap depending on the logic.
->If we don't use "new" operator when creating string, it will be stored in string constant pool.
->But before storing it will check if that object is already available or not in that string constant pool.
-> If the string object is already available in the string constant pool, then it will not create a new object.
-> If you create strings with "new" operator it will be stored in the heap. But before creating the string it will not check if that string is already available in heap or not.
String str1= "hi";
String str2= new String("hi");
String str4= new String("hi");
In the above example we are trying to copy a string int str but strings are "immutable". So rather than modifying in same location it will create one more object in the string constant pool.
We can append one more string to existing string using concat().
Check if two functions/strings are equal or not.
String str= "tech palle";
String str="tech palle";
String res = str.substring(2);
System.out.println(res); // ouput: from 2 it will start printing.
-> I want to create a string and share it will multiple threads where those threads must not be modified my string.
-> Advantage of string is we can share it with multiple threads.