• 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

Shifting and Garbage Collection

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shifting:
I know that 128 >>> 1 equals 64. How about 128 >> 1? Is it 64 or -64? Why?
Garbage Collection:
When is the string object created in line2 first subjected to Garbage Collection?
1 public void aMethod(){
2 String s1 = "Hello";
3 String s2 = "pal";
4 s1 = s1 + s2;
5 System.out.println(s1);
6 }
The answer is "after line 4". Why not after line 6?
Thank you.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shifting:
I know that 128 >>> 1 equals 64. How about 128 >> 1? Is it 64 or -64? Why?
Garbage Collection:
When is the string object created in line2 first subjected to Garbage Collection?
1 public void aMethod(){
2 String s1 = "Hello";
3 String s2 = "pal";
4 s1 = s1 + s2;
5 System.out.println(s1);
6 }
First Question Explanation:
The >> & >>> will behave in the same way as long as the left hand side operand is +ve.
Second Question Explanation:
2 String s1 = "Hello"; ----> the object "Hello" is created in
the pool.
3 String s2 = "pal"; -----> The Object "Pal" is created in
the pool.
4 s1 = s1 + s2; ----> Now a third object is created
which is "HelloPal" & the s1 is
referring to this object instead
of "Hello" .Since there is no
reference to "Hello" it is set
for garbage collection.
5 System.out.println(s1);

 
Susie Chow
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Shrenik. I am clear about garbage collection. For shifting, if 128 is a byte, which becomes 1000 0000 in binary, right? Since the leading bit is 1. It has becomed a negative number according to two's complement theory? I know that I am missing something. Can you tell me how exactly it works? Thanks a lot!
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Susie,
Check out Cat and Mouse Games with bits. It explains the basics of shifting and has a application that lets you play with various values and operators.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
My cellmate was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic