Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable initialisation and default values.

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two very similar codes achieving the same results. But the 1st one has no variable initialisation error when I try to compile it, while the 2nd one has. I can't figure out why. The only difference is is the bracketing.

Local variables are not given a default value, but why does the 1st code work ?

Code with the variable incre not initialised, and it still compiles.


Code with the variable incre not initialised, and it doesn't compile.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Int the first piece of code, incre will be initialized by the if condition block. In the second piece of code, incre will not be initialized when yr1=10.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Int the first piece of code, incre will be initialized by the if condition block. In the second piece of code, incre will not be initialized when yr1=10.


Oh ! Nice catch, I didn't think about that one.. Thanks a lot.

Btw, is it best practice to initialise local variables to 0 ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:Btw, is it best practice to initialise local variables to 0 ?

No. Initialize variables to whatever value is appropriate. Don't initialize a variable to whatever value for no good reason.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49772
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote: . . . Btw, is it best practice to initialise local variables to 0 ?
It is probably not a good idea to give local variables a default value. You may have to in some cases (eg an index might have to start at 0), but I would suggest, as a general rule, no.
Of course, if your variables are reference types, you cannot initialise them to 0. You would have to use null, and as you doubtless know, nulls are dangerous. You will find discussion of null problems and other mistakes, in this thread.
The same applies to fields, though they do have default values.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Of course, if your variables are reference types, you cannot initialise them to 0. You would have to use null, and as you doubtless know, nulls are dangerous. You will find discussion of null problems and other mistakes, in this thread.
The same applies to fields, though they do have default values.


A little confused about this part. Consider this small piece of code


Am I wrong in writing something like this? Lets says I had to use conn reference across the entire function and not just under the scope of 1 try-catch block.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say it probably depends on what getConnection() does. Will it throw an exception? Will it return a null, or is it guaranteed to return something.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. But, irrespective of what getConnection returns, from your functions perspective, you know that the reference can only have 2 states, either null or not null. Wouldn't that be much easier?

The point am trying to make here is purely on the lines of best practice, nothing technical. Am just thinking it would be better to initialize the variables to a known quantity apriori, so that its much easier to program the rest of the stuff around that value.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49772
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some nulls which are unavoidable, so they have to be handled somehow.
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And null initialization does not makes itself a best practice. They are just unavoidable sometimes!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic