aspose file tools*
The moose likes Beginning Java and the fly likes Variable initialisation and default values. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable initialisation and default values." Watch "Variable initialisation and default values." New topic
Author

Variable initialisation and default values.

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 89
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
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

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

Joined: Sep 05, 2012
Posts: 89
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Jul 03, 2011
Posts: 256
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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

Joined: Oct 13, 2005
Posts: 39791
    
  28
There are some nulls which are unavoidable, so they have to be handled somehow.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

And null initialization does not makes itself a best practice. They are just unavoidable sometimes!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable initialisation and default values.