• 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

How this code works ?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class sample
{
public static void main(String a[])
{
sample s = new sample();
s.countDown();
}
public void countDown()
{
for (int i=10 ; i>=0 ; i--)
{
String tmp = new String(" ");
//Integer
int x = 10;
tmp = Integer.toString(i);
System.out.println("Hash code is " + tmp.hashCode());
System.out.println(tmp);
}
}
}
Here i have created variables in the type of both primitive data type and as well as a class. It is given inside the for loop. Wont the compiler complain that iam trying to duplicate the already exixting variable..?
Or will the variable tmp and i be destroyed at the end of each iteration and recreated afresh at the begining of the loop.
Please explain
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i get no errors in executing your code.
 
krish chandru
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah,
thats what am asking ....should not a compiler throw an error...
How this accepts the variable declared in the for the for loop...
is that not *redeclaration* of the variable ?
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Krish,
Part of the answer to your question is understanding scope. By defining "String tmp..." inside the for loop, you are limiting the scope of the variable to that loop (everything inside the curly braces), so the compiler allows the definition inside the braces.
From a PURELY LINEAR standpoint it may seem like you would be trying to recreate the variable over and over, but in this case you are reinstantiating the tmp String object, creating a new one and making the old one available for garbage collection.
But this doesn't work just with objects! If you had a primitive defined inside the braces it would work just as well because the compiler only looks for one definition statement. The run-time execution doesn't care if you hit that statement a zillion times because the compiler already took care of the definition.
So don't confuse the operation inside the scope with the compile time definitions. Hope this helps!
Someone correct me if I'm wrong.
------------------
I'm a soldier in the NetScape Wars...
Joel
 
krish chandru
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
joel,
Clear me this thing.....Will the primitive x and the object tmp in the following snippet be available for garbage collection (ie ..) its scope ends when the curly brace ends...?
for (int i=10 ; i>=0 ; i--)
{
String tmp = new String(" ");
//Integer
int x = 10;
tmp = Integer.toString(i);
System.out.println("Hash code is " + tmp.hashCode());
System.out.println(tmp);
}
I think it should be..otherwise the folloing should also be valid
for (int i=10 ; i>=0 ; i--)
{
String tmp = new String(" ");
//Integer
int x = 10;
int x = 12;
tmp = Integer.toString(i);
System.out.println("Hash code is " + tmp.hashCode());
System.out.println(tmp);
}
Please clear me with this !!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well now. First of all x is not a primitive. 10 is a primitive. x is a variable of "type" int. And tmp is a variable of type string, it is not an object itself. tmp holds a reference to a String that is the object. Variables are created on the stack, not on the heap and are therefore not cleaned up by the garbage collector. When the stack that the variable is held in is discarded by the JVM then the variable is gone.
The primitive 10 is a literal, and is held in the constant pool, which is a "per class" storage area. This is unloaded when the class is unloaded.
However, since you used the new keyword to create your String object, it WILL be created on the heap, not in the constant pool.
String tmp = new String(" ");
| |.............| |
Variable / 1st object holding a space String

As soon as you replaced the contents of tmp with the Integer.toString(i); the first String that you created (holding a space) is ready for garbage collection.
tmp = Integer.toString(i);
| | ................| |
same variable / 2nd String object
The second object that was created with the toString command is available for the garbage collector after the iteration of the loop is over.

[This message has been edited by Cindy Glass (edited May 08, 2001).]
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ummmm...yeah....that's what I said
Cindy, 'x' is still not available outside the curly braces, right? So once you leave the scope that contains the variable, it is in essence ready for GC since you can't use it anymore. What I was trying to say was that these variable exist only inside the scope in which they were created so it doesn't matter if they are in a loop or not they only get created once. And the only reason I referred to 'tmp' as an object was because Krish used 'new'. Am I wrong here somewhere? (just curious)
Krish,
Forgive me, but the only thing I see different between your two examples is the addition of 'x = 12;' in the second one, which would simply mean that x now refers to a value of '12' and no longer '10'. None of this matters since 'x' isn't used anywhere...

------------------
I'm a soldier in the NetScape Wars...
Joel
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joel
x and tmp are not objects (they are variables) and will NEVER be ready for the gc(). They will die with the stack.
The x does not reference an object, it references a constant literal primitive that happens to be an int. That literal will NEVER be ready for the gc(). It will be unloaded when the class is unloaded.
Only objects on the heap get garbage collected. The references that tmp holds during each iteration are to 2 objects on the stack. They will get garbage collected.
I can see that you have the concept of scope down pat, so I won't go into that.

 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
Joel
x and tmp are not objects (they are variables) and will [b]NEVER
be ready for the gc(). They will die with the stack.
The x does not reference an object, it references a constant literal primitive that happens to be an int. That literal will NEVER be ready for the gc(). It will be unloaded when the class is unloaded.
[/B]


I understand that '10' is a literal primitive and will never actually go away, what I don't get is what happens to the variable 'x' once outside its scope? The literal may be unloaded, but what happens to the variable? You said something about dying with the stack, I consider that GC so maybe I'm just too general with the term.

Originally posted by Cindy Glass:

Only [b]objects
on the heap get garbage collected. The references that tmp holds during each iteration are to 2 objects on the stack. They will get garbage collected.
[/B]


'tmp' is the reference to a String object, right? So aren't we splitting hairs by saying that 'tmp' doesn't get GC'd? If not, then I need to learn to be more specific in my wording, because when I mention 'tmp' I'm talking about the object it is referencing.
Thanks for your patience Cindy, some of us need it more than others.
------------------
I'm a soldier in the NetScape Wars...
Joel
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to be more specific in your wording because tmp holds different objects at different times, and they are eligible for the gc at different times. The first object that tmp holds is eligible in the middle of the method. The second becomes eligible when the method goes out of scope.
In fact when the method ends, the stack for the method is immediately destroyed by the OS/JVM (not the gc) and is probably gone BEFORE the gc gets around to cleaning the heap.
This is probably over-kill but in case you are interested:
From the JVM Specification


3.5.2 Java Virtual Machine Stacks
Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread. A Java virtual machine stack stores frames (�3.6).



3.6 Frames

A frame is used to store data and partial results, as well as to perform dynamic linking , return values for methods, and dispatch exceptions.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are allocated from the Java virtual machine stack (�3.5.2) of the thread creating the frame. Each frame has its own array of local variables (�3.6.1), its own operand stack (�3.6.2), and a reference to the runtime constant pool (�3.5.5) of the class of the current method.

 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy, I am definitely interested. Looks like I need to spend some time reading the specification, and I will try to be more specific in my wordings - with good reason
Thanks,
Joel
 
reply
    Bookmark Topic Watch Topic
  • New Topic