• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in declaration  RSS feed

 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have doubt.

for(Person p:personlist)
PersonDAO pp= p;

how below code is bettern than above one

PersonDAO pp=null;
for(Person p:personlist)
pp= p;

How will this affect memory management. according to me both are same right?

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't sure, that I'm correct or not. But, first will take some overhead. Because, each time you are re defining the variable, that's not the case in second!
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In first case the scope of variable PersonDAO pp is just within your for loop. The other way let's you use the variable within a bigger scope. I would say codes are better/worse depending on the rest of your program logic.

As for memory management, in first case, once for loop runs its course, the object that pp variable is pointing to is eligible for garbage collection. Given that no other variable is pointing to it.
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for the replies. I know about the scope. what i really mean is that. I heard that if you
declare the variable outside the loop it will save the memory space. if we do it inside it will
declare it every time there by increasing the memory useage. But what i feel is that even though
we have the declaration inside the looping it will overrite the previuos variable with same name.

There by cuasing the same effect in both the cases is that right?

Thanks
Subu
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. Just as Abimaran explained, in the first case you create a variable every iteration of the loop and assign it. In the second case you just assign it for every iteration.
 
Andrey Choe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

If you create many object's in your method (first code sample), than you take more heap memory. So after exiting method JVM can decide to run gc (all PersonDAO instances are useless). Running gc take some resources. That's why second code sample is better (for memory usage point of view).
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the first case you create a variable every iteration of the loop and assign it.


This is what is confusing me. how can we declare the variable with same name. if we do manually it will give compile time error. Then how it happens in loops.
I think instead of declaring it every time . It may be overwriting it right?

Thanks
Subu
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both bits of code are illegible without code tags, indentation and braces. In both cases you are assigning a value to the poorly-named pp reference, and doing nothing with it

I suspect the compiler will optimise away part of that code: put both snippets into a two-method class, compile it, and use javap -c on that .class file. See whether there is any difference in the bytecode at all.
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I wrote both the codes and decompiled using jad, to check how they will be different. but there was no difference in the decompiled code. whatever i wrote was the same.

Can anybody tell me what is the actual difference.
Thanks
Subu
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subu Mhathma wrote: . . . decompiled using jad, . . .
I suggested javap, not jad. Seeing what the bytecode means will allow you to see whether there is a difference at all after compiling.
Alternatively, put both versions into a one-method class and compile the two versions in different folders. Then open the .class file with a hexadecimal editor.
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I decompiled the code using javap -c Test in both the cases. I did see some difference but couldn't understand it.

Thanks
Subu
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this wont compile, because every local declaration is followed by a block implicitly, so the pp is not available in the block . hence error(<edit>to avoid this use braces to for loop</edit>)
Subu Mhathma wrote:

for(Person p:personlist)
PersonDAO pp= p;


and this will compile, but it missing the braces which is recommended.
Subu Mhathma wrote:
PersonDAO pp=null;
for(Person p:personlist)
pp= p;


<edit>
For more clarity : From JLS

Every local variable declaration statement is immediately contained by a block. Local variable declaration statements may be intermixed freely with other kinds of statements in the block.

A local variable declaration can also appear in the header of a for statement (§14.13). In this case it is executed in the same manner as if it were part of a local variable declaration statement.

</edit>

hth
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First does compile. Question here is not about the syntax or ... it is about how one is more memory efficient than the other.

Thanks
Subu
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subu Mhathma wrote:First does compile.

Hmmm, do you tried?
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiles and executes to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!