• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I get irritated with String questions such as these.

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from javaprepare site exam1
* At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
1. Before statement labeled 1
2. Before statement labeled 2
3. Before statement labeled 3
4. Before statement labeled 4
5. Never.
Ans
Now i get very confused whether lines such as 3 create new strings or refer to the same old String.There are some other questions i have seen which i am not still sure of.
Help
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leena,
Just remember, Strings are immutable. That means that once they are created they can't be changed. This refers to the actual String object in memory and not the reference variable that points to it.
Also remember, any time the new operator is used a new memory allocation happens. Therefore if we have the following:
String x = new String("X");
String y = new String("X");
we are creating two strings in memory (i.e., 2 new operators where used). Not to get to confusing here but if we had the following:
String x = "X";
String y = "X";
the compiler would be smart enough to know that both references could point to a single copy of the string "X". Because strings are immutable. Therefore, niether variable reference could change the string from "X", ever!
Knowing this we can say 100% of the time that any assignment function using strings creates a new string in memory. In your example:
r = r + 1;
will definitely create a new string in memory. Also we can say that:
s = s + r;
will also create a new memory object. As a matter of fact the null keyword is interpretted as "null". Therefore the actual value of s after the above assignment will be:
s = "abcnull";
Regards,
Manfred
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi leena rane:
This is from javaprepare site exam1
* At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
1. Before statement labeled 1
2. Before statement labeled 2
3. Before statement labeled 3
4. Before statement labeled 4
5. Never.
Ans
Now i get very confused whether lines such as 3 create new strings or refer to the same old String.There are some [b]other questions
i have seen which i am not still sure of.
The correct answer is : before statement labeled 4
Because first you created two String objects pointed by r, s;
Then at line s = s + r; you created one more String object(new String object is created if you use "new keywords" and
"+ operator" with String ref)
The first object is pointed by s becomes available for garbage
collector
Hope this help

[This message has been edited by lu v thuan (edited October 26, 2001).]

 
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Manfred ,
Thanx lu too
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case how many objects eligible for gc. My ans is one.somebody correct me if I am wrong.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just remember, Strings are immutable. That means that once they are created they can't be changed. This refers to the actual String object in memory and not the reference variable that points to it.


I have seen this statement in a number of places. Why is it important?
In the following code, you are saying that += is creating a new String object and temp1 was a reference to a String that contained "dog" and is now a reference to a new String that contains "dogcat". Why is that important? The code statement indicates that I want a string object that contains "dogcat". Is it relevant to me if the JVM used the old object or created a new one?
Any insight?

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
It's important because the exam will test your understanding of String objects.
While programming it's important to know that String objects that were interned can be compared with "==". Any string comparision involving a non interned string must be done with equals.
hope helps
 
lu v thuan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by suma krishnan:
In this case how many objects eligible for gc. My ans is one.somebody correct me if I am wrong.


Hi krishnan
Three objects are eligible for gc
Two pointed by r : object new String("abc") and object created by r+1;
One pointed by s " new String("abc");
Hope this help
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never because the reference is stil in active part of the program so the object is never garbage collected
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with lu and Manfred there are three String objects available for g.c. after the execution of statement three
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leena,
What was your score in the exam, i came after 1 week or so.
Nisheeth.
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic