File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local variables" Watch "Local variables" New topic
Author

Local variables

Ripul Chhabra
Greenhorn

Joined: Mar 19, 2006
Posts: 12
Hi,

If anybody can help me regarding following query.
Why Local variable are not assigned a default value?

Thanks in advance
Ripul


With Regards,<br />Ripul Chhabra
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,

It's meaningful to have Object state get initialized(before the constructor call).

Local variables wont come under Object state. and also no meaning and purpose in initializing them.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Ripul Chhabra
Greenhorn

Joined: Mar 19, 2006
Posts: 12
Hi Mr Srinivasan,

Local variable don't come under object state but they can be assigned default value as instance or static variables are.

Can you please elaborate on you reply.

Thanks for replying.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,

Think before you type...

stay calm and think...

you got one .class file say Sample.class passed to JVM (java Sample).

1)Then it will load the Sample class(byte code) and creates instance when you say new Sample() in main method. before constructor it can initialize instance variables, class variables. Now constructor completes Object ready.

2)it can't initialize Non-existing local variables of methods. because it they were not there.

3)The methods you write will be loaded along with (.class) into "CODE AREA".

4)Now you are going to execute a method on the Object.
control jumps to code area, and it will start copying local variables into stack. updates them. and removes after execution.

(If you want the local variable to get inited, Who will do that).
Think...
-----------------------------------------------
What compiler do it for you is, it will assure that local vars are inited properly before use.
It has so many checks for this...you know.Refer to Java Language spec for all these checks done.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Originally posted by Ripul Chhabra:
Local variable don't come under object state but they can be assigned default value as instance or static variables are.

Can you please elaborate on you reply.


Simple answer, the designers of Java chose not to. Yes, they could have added extra instructions in the method to initialize the memory to zero, in addition to allocating the space on the stack.

Instead, Java was designed to do a compile time check to make sure that local variables are guaranteed to be initialized before they are used -- instead of actually pre-initializing them to zero or null at runtime.

For the more complex answer of why, I'm afraid you'll have to dig through the history of Java. Or maybe interview one of the designers.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Henry Wong,

How i understood the concept is:

It would be a extra operation,initializing the local variable of a method in a class. and it has to execute NOT ONCE PER OBJECT, but for each invocation of method adding extra step to init all the local variables.

Imagine a method has 100 local variables.

and Object is going to call that method 100 times in its lifespan.

then 100 * 100 = 10,000 lines of init instructions it has to add.

I think they might have considered it a overhead to initialize.
So they have complex logic at complile time to check proper inits of locals, WHICH WILL BE DONE ONCE.

What to you say, HENRY
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Imagine a method has 100 local variables.

and Object is going to call that method 100 times in its lifespan.

then 100 * 100 = 10,000 lines of init instructions it has to add.

I think they might have considered it a overhead to initialize.
So they have complex logic at complile time to check proper inits of locals, WHICH WILL BE DONE ONCE.


Well, first, it is not 10,000 lines of init instructions. It is only 100, one for each variable in the method. Methods don't increase in size, as you use it.

I agree it is 10,000 more calls to zero out a memory location. But then... it is also 10,000 calls to allocate the space on the stack, 10,000 calls to pop the stack after the variable goes out of scope, and this is before the variable is even used.

Furthermore, considering it takes one cycle to zero out memory, 10,000 cycles is only about one microsecond. I hardly think it will be noticable for 100 method calls.


Not saying that I agree or disagree with the Java designers on which is better. My opinion is that I don't see any major advantage or disadvantage with it.

Henry
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
Posts: 49
I think the choice of Java designers also might have to do with memory management. If local variables are not copied to the stack until method execution time, it means JVM never has to touch (move to stack) local variables for methods which never get executed.


EXCEL IN ALL YOU DO
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Henry,

Thanks a lot for your interpretation.
Thanks to Ripul for raising this topic.

Ripul Chhabra
Greenhorn

Joined: Mar 19, 2006
Posts: 12
Thanks everybody for taking their important time for giving their opinion.
I have few more queries in which I�m facing little problem should I post them?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variables