• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why compiler does not provides default values to local variables?  RSS feed

 
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler does not provides default values to local variables ,why?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:Compiler does not provides default values to local variables ,why?


It's not the compiler that provides the default values for member variables, it's the JVM.

As to why it doesn't do it for locals, the reason is because that's the decision the designers made. To learn whey they made that decision, you'd have to ask them.

We can speculate why. For instance, methods are called more often than objects are created, so it may be too big a performance hit to initialize every local variable every time. Or maybe it's that it makes sense for an object to start with a predictable state but not so much for a method, which is more short-lived.

In the end though, it's just speculation.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the question should be the other way round. Why can you have default values for fields. Why does the compiler not insist every field be initialised in the constructor or an initialiser? It can do it for final fields. It would reduce problems with NullPointerExceptions because reference-type fields haven’t been initialised.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i am unable to figure it out that why jvm provides default values only to instance variables, why not to local variables ? Is there any Concept behind this ,which i have skipped ??
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:Actually i am unable to figure it out that why jvm provides default values only to instance variables, why not to local variables ? Is there any Concept behind this ,which i have skipped ??

I think Jeff covered most of the possibilities; and my guess is that performance is pretty high among them.

Also: Why would you not provide a value for a local variable?
Answer: because you're not using it; in which case initialization is moot.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:Actually i am unable to figure it out that why jvm provides default values only to instance variables, why not to local variables ? Is there any Concept behind this ,which i have skipped ??


I already told you a couple of potential reasons. And I already stated that to know the actual reasons those decisions were made, you'd have to ask the creators of the language or dig up some whitepapers from back when Java was first created.

To slightly rephrase what I said previously:

An object represents state, and is globally visible--or can be. It makes sense for something that represents state to come into existence with known, "blank" values, and to not require us to explicitly say, "make this value blank."

A method, on the other hand, is generally more short-lived, and its variables only exist during that invocation of that method and are only visible by that invocation of that method. Since their scope, visibility, and lifetime are typically smaller than those of an object, and since methods pop in and out of existence more frequently than objects, a) it is less valuable to give their variables default initial values, and b) it is more expensive to give their variables default initial values.

As I already said, though, these are just my opinions. But I've been programming Java for a rather long time now, and my programming has not suffered one tiny bit from not knowing why these decisions were made. It's not something I'd spend much time worrying about if I were you.
 
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JIT = Just In Time

Only spend CPU intializing something (not always true though) when you need it.

WP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:JIT = Just In Time

Only spend CPU intializing something (not always true though) when you need it.

WP


Okay, so then why did the Java language rules decide that always need every single member variable?
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Nikhil Sagar wrote:Actually i am unable to figure it out that why jvm provides default values only to instance variables, why not to local variables ? Is there any Concept behind this ,which i have skipped ??


I already told you a couple of potential reasons. And I already stated that to know the actual reasons those decisions were made, you'd have to ask the creators of the language or dig up some whitepapers from back when Java was first created.

To slightly rephrase what I said previously:

An object represents state, and is globally visible--or can be. It makes sense for something that represents state to come into existence with known, "blank" values, and to not require us to explicitly say, "make this value blank."

A method, on the other hand, is generally more short-lived, and its variables only exist during that invocation of that method and are only visible by that invocation of that method. Since their scope, visibility, and lifetime are typically smaller than those of an object, and since methods pop in and out of existence more frequently than objects, a) it is less valuable to give their variables default initial values, and b) it is more expensive to give their variables default initial values.

As I already said, though, these are just my opinions. But I've been programming Java for a rather long time now, and my programming has not suffered one tiny bit from not knowing why these decisions were made. It's not something I'd spend much time worrying about if I were you.

got it, Thanks jeff..
This is your friendly forum for beginning Java. No question is too simple or too small !
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
This is your friendly forum for beginning Java. No question is too simple or too small !


Yes. I'm sure we're all aware of that even without you shouting it in our faces. It's worth noting, however, that this statement does not imply that every question is worth spending large amounts of time or effort on.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Nikhil Sagar wrote:
This is your friendly forum for beginning Java. No question is too simple or too small !


Yes. I'm sure we're all aware of that even without you shouting it in our faces. It's worth noting, however, that this statement does not imply that every question is worth spending large amounts of time or effort on.


Respected Sir,
Jeff ,Two person can never have same thinking in this whole world. I am here to learn java.I am here because i want to be a person ,who should have every answer about java.
In short, i Want to be a person like you Sir. I had a doubt related to java so i just asked and i am here to learn java. Genius People like you are here to help people like me who want to be a Genius like you too.I can understand It's not something you'd spend much time worrying about if you were I. But understand it Sir i am Nikhil and You are Jeff ,i am learning and you have learned. Sorry if i am again making a mistake by posting this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:I am here because i want to be a person ,who should have every answer about java.


That is not a realistic goal. The amount of stuff you can hold in your brain is finite, as is the amount of time you have on this earth in which to learn things. So it's important to prioritize things. Even the people who created Java don't know everything about it.

In short, i Want to be a person like you Sir.


Java has been a large part of my daily live for over a decade, and what I DON'T know about Java is still MUCH, MUCH bigger than what I do know about it.

I had a doubt related to java so i just asked and i am here to learn java.


I'm not saying don't ask. Any question is worth asking. I'm saying that the practical value of knowing the exact answer to this question is very low, and your finite time and finite brain power will be better spent accepting the answers you have so far and moving on to the next question.

Genius People like you are here to help people like me who want to be a Genius like you too.


Thank you for the compliment, but I'm far from being a genius, believe me.

Sorry if i am again making a mistake by posting this.


Nope, no mistake at all. Nothing wrong with politely stating one's view.

I hope you understand now that I'm not trying to discourage you from asking questions, but rather, am only trying to get you to recognize that the answers to some questions are more useful to know than others.

Good luck, and don't stop wondering!
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Again Sir, For The Guidance .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!