-
Will the compiler creates/assigns the memory for String
We are able to create String object in 3 methods
Method 1.By directly Declering String Variable
Ex: String s1="Hai";
Method 2.By Using new operator
Ex: String s1=new String("Hai");
Method 3.By converting the character Array in to String
In 1 interview they asked me what is the difference between Method 1 and method 2
then i said tht compiler will create the String object in Method1
String object will be create at run time by using method2
Now the question is as follows
Will the compiler creates/assigns the memory for String
Question asked by visitor Veerraju
-
Junior Member
Re: Will the compiler creates/assigns the memory for String
Hi
The difference between the two is that the first one creates the reference of string class & second creates the object of the string class that acquires some memory & will have the value "Hai"
-
Junior Member
Re: Will the compiler creates/assigns the memory for String
See, basically, Strings in java are immutable objects, once they are created there value will never change throughout the program.so, when one string is created like String s = new String ("abcd"),
here, s= abcd, as you know,
now , this is stored in the heap.
now , you concat for example with "hij",
String.concat(s,"hij")
then,
in the heap,
there is another string created for hij,
while the "abcd" remains as it is, since, it is immutable.
now, in the memory at run-time, both are concated.
so,
your qestions asked gets satisfied.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules