• 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

Garbage collection ..

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following program :::
public class Manal {
static int m;
public int met() {
return this.m;
}
public Manal() {Manal.m ++ ;}
public static void main(String s []) {
for(int lp = 0; lp< 100;lp++)
System.out.println(new Manal().met());
System.out.println("the no. of objects created r " + m);
}
}
can any1 pls tell me hw many objects r eligible for garbage collection when this program exit ...
i think 203 bcos ::
100 objects of Manal class
100 String objects from System.out.println(new Manal().met());
2 String objects ("the no. of objects created r " and other is toString(m) )
& 1 String frm System.out.println("the no. of objects created r " + m)
what do u say ....
manal
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I say there are just 201 objects created.
coz : "something" + m - does not create 3 objects. It just creates one objects.
Heres the explanation :
String buffers are used by the compiler to implement the binary string concatenation operator . For example, the code:

x = "a" + 4 + "c"

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c")
.toString()

which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.
 
manal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Deepak ....
manal
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic