Results 1 to 3 of 3

Thread: Will the compiler creates/assigns the memory for String

  1. #1
    Geek_Guest
    Guest

    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


  2. #2
    Junior Member
    Join Date
    Jun 2007
    Answers
    20

    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"


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    4

    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
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact