Ok now lets talk about strings in java. Rather i should use Strings. Strings are itself not provided as a primitive data type by java like int. It is implemented with the help of String class in java.
eg String str="amit";
Most c users consider char str[] ="amit" as the implementation of the above declaration. But it is not true. The declaration resembles char *str="amit". The diffrence between the two is that the former is mutable (can change individual character values eg str[2]='a' ) but the latter is immutable.
Strings in java lie in between the implementation of primitive types and objects. Consider the following declaration :
String str="amit";
String str = new String("amit");
The former declaration uses the concept of primitive data types ie it data is not kept in user/program heap. All the variables declared in this style with the same value share the same memory space. Consider the following c illustration :
char *temp ="str";
char *b = temp;
Both point to same memory location. Similarly String a="emp" and String b="emp" both share memory and point to the same location in java.( Try using if(a==b) though strictly not recommended. Since both point to same memory the output is true).
The latter places the string in the program heap so there is no concept of sharing.(Try comparing two strings with same value using == operator, the output comes out to be false.So use String.equals function for string comparison)
In java only + operator is overloaded for concatenation of two strings.
String class being immutable leads to slower string operations. For this StringBuffer( multiple thread) and StringBuilder ( single thread) class are provided.
They are mutable classes and does not overload the "+" operator as in String class.
PS: USe strings when data need not be modified frequently else use stringbuffer or StringBuilder.
For more details visist the following link : http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html
what shoud be use in function of a class string or string buffer, where variables used again & again.
ReplyDeletewhat does used means ??
ReplyDeleteif use means change in the variable than you should use string buffer or string builder (depends , if variable is accessed by multiple threads (use string builder)or single thread (use string buffer))
if use means just accessing the variable, you should use string class.
Adding to what Anjani said that String class itself will not boost the performance unless you carefully take the advantage of memory sharing(the comman pool for strings). So if the requirement is just to use the string ie read it then go for String str = "str".
ReplyDeletethanks Amit and Anjani, I have read the article. Now the concept is clear
ReplyDeleteA question which is still not clear is why string class is not mutable? Why did java developers implementes String as a class,why not as a primitive type. The probable reason could be performance (as discussed above the concept of memory pooling). Another reason could be since strings are heavily used in java,multiple threads would require synchronisation if strings were mutable.So this saves the overhead.
ReplyDeleteWell there could be 1 more reason. Consider a case that there are 2 users-A and B, both require to authenticate themselves to read their respective files - fileA and fileB. In java File name is represented as string(what about c/c++ then? ). So now suppose A logs in and authenticates himself. Now before the read call reaches OS,A changes the file name to fileB. So now he can access file for which he was not authenticated. This leads to major security issue. Hence strings are immutable in java.
How A can change file name without being authenticated?
ReplyDelete@Anjani..A after authentication can read the file. Suppose that their is a request that first authenticates and then opens the file(Some what similar to our mails,we first get authenticated and then we see our mailbox). What i am trying to say is u get authenticated but before call reaches to OS to open a file you change the String value. So now it will open you some other file.
ReplyDelete