Results 1 to 12 of 12
- 04-09-2012, 08:13 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
To initialize or not to initialize - when initialized value is not used
I took a java beginner class a while back, and the instructor told us to always initialize every variable, regardless of whether or not we will use the initialized value.
useless example code:
NetBeans complains "The assigned value is never used". I considered turning off the "Unused Assignment" hint, but then got to wondering what the best practice should be in real code.Java Code:Scanner sc = new Scanner(System.in); int x = 0; int y = 0; x = sc.nextInt(); y = x * 3.14; System.out.println("x = " + x); System.out.println("x * PI = " + y);
On one hand, if I don't initialize the variables, and some other coder comes after me and changes something, then he might break the code that initializes it later. initializing the variables would keep the app running, though possibly with bad or default values.
On the other hand, if i don't initialize them, maybe I *want* the code to break if he changes the code that initalizes it later, so it's obvious to him that he screwed something up.
Should I initialize everything or not?
- 04-09-2012, 09:54 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: To initialize or not to initialize - when initialized value is not used
In some cases, the compiler forces you to assign a value (see Definite assignment analysis - Wikipedia, the free encyclopedia ). In this case however, why initialize to 0 - just initialize the variable to the first assignment. eg
Java Code:int x = sc.nextInt(); int y = x*3.14;
- 04-09-2012, 11:59 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: To initialize or not to initialize - when initialized value is not used
U can run into problems declaring variables within loops.
- 04-10-2012, 12:01 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: To initialize or not to initialize - when initialized value is not used
You dont need to initialize it right away, but YOU MUST, initialize it before it is used.
int a;
for(int i = 0; i < 5; i ++)
{
a = getRandomInt();
System.out.println(a);
}
- 04-10-2012, 12:38 AM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: To initialize or not to initialize - when initialized value is not used
I recommend in the future, elaborate on definitions such as "MUST" and "problems declaring variables" - especially if your intent is not to confuse. And furthermore, post code snippets that actually demonstrate the 'problems' - the one you posted does not (of course I don't know how you define problems, so for all I know it does).
There are clearly defined affects when you do not initialize a variable in certain context - namely a compile time error. Again - in certain contexts, not limited solely to loops, and not directly applicable to the code you posted.
- 04-10-2012, 12:49 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: To initialize or not to initialize - when initialized value is not used
Is
For (int i = 0 ; i < 5; i ++)
{
int a = 5;
System.out.println(a);
}
Have the same workload as
int a;
For (int i = 0 ; i < 5; i ++)
{
a = 5;
System.out.println(a);
}
- 04-10-2012, 02:32 AM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: To initialize or not to initialize - when initialized value is not used
Again, definitions seem to be lacking, and I cannot tell if your post was a question or a statement. By workload, I presume you mean that one of those is faster than another? Why not write an SSCCE that demonstrates or refutes that one has more 'workload'? But I do wonder what that has to do with initialization and the original posters question...
- 04-10-2012, 02:35 AM #8
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: To initialize or not to initialize - when initialized value is not used
If you look at line 2 of Vizoere's code, he initialized the variable to 0. His professor tells him that he needs to always have a defined variable. What I am saying is the same thing, except if you define the variable directly before it is used, even if not at first, it will still be correct and good coding.
- 04-10-2012, 02:38 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: To initialize or not to initialize - when initialized value is not used
I can't write a SSCCE because I sir, do not know what that is, but he is doing an unnecessary step if you define a variable like that within a for loop : reallocating memory when you need not. But that dosen't pertain to this question, I'm doing too many things at once atm.
- 04-10-2012, 03:21 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: To initialize or not to initialize - when initialized value is not used
I agree with this sentiment. If your initialisation code gets deleted you want the compiler to complain ("variable not initialized"), rather than have the program run just fine ... until the bogus value causes your database to be deleted.On the other hand, if i don't initialize them, maybe I *want* the code to break if he changes the code that initalizes it later, so it's obvious to him that he screwed something up.
In general try and declare the variables close to where they are first used and don't give them excessive scope. By the latter I mean if a variable is not meant for use outside of a given block, don't declare it outside that block. The difference between the alternatives in Ameer's examples (#6) lies not in the "load" the declaration imposes (before you measure it determine where this "load" is actually defined!) but rather whether you intend using a after the loop has finished.
If you do wish to use a outside the for loop it must be declared before the loop. And initialised to a bogus value before the loop because the compiler is not clever enough to figure out that the loop will give it a value.
- 04-10-2012, 04:38 PM #11
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: To initialize or not to initialize - when initialized value is not used
To quote java code conventions
And not included in the code conventions is the often used phrase and IMO good rule of thumb (which is off topic but only brought up above): declare variables in the smallest scope possible. The argument above that one of those code snippets has more 'workload' than another is erroneous: take a look at the bytecode for either of those methods and you will see the bytecode is virtually identical.Try to initialize local variables where they’re declared. The only reason not to initialize a variable where it’s declared is if the initial value depends on some computation occurring first.
- 04-11-2012, 05:57 PM #12
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Initialize a Variable
By lala in forum New To JavaReplies: 13Last Post: 11-16-2010, 06:51 PM -
Trying to initialize variables
By random0munky in forum New To JavaReplies: 2Last Post: 10-14-2009, 10:30 PM -
ObjectInputStream does not initialize
By Singing Boyo in forum New To JavaReplies: 1Last Post: 06-03-2009, 08:11 AM -
Int does not initialize, will this work?
By starchildren3317 in forum New To JavaReplies: 2Last Post: 07-09-2008, 10:42 PM -
Initialize variables before use
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks