Results 1 to 12 of 12
  1. #1
    Vizoere is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default 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:
    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);
    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.

    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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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;

  3. #3
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: To initialize or not to initialize - when initialized value is not used

    U can run into problems declaring variables within loops.

  4. #4
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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);
    }

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: To initialize or not to initialize - when initialized value is not used

    Quote Originally Posted by Ameer3881 View Post
    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);
    }
    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.

  6. #6
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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);
    }

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: To initialize or not to initialize - when initialized value is not used

    Quote Originally Posted by Ameer3881 View Post
    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);
    }
    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...

  8. #8
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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.

  9. #9
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: To initialize or not to initialize - when initialized value is not used

    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.
    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.

    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.

  11. #11
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: To initialize or not to initialize - when initialized value is not used

    To quote java code conventions
    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.
    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.

  12. #12
    Vizoere is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: To initialize or not to initialize - when initialized value is not used

    A lot of good points made. Thanks all for your thoughts.

Similar Threads

  1. Initialize a Variable
    By lala in forum New To Java
    Replies: 13
    Last Post: 11-16-2010, 06:51 PM
  2. Trying to initialize variables
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-14-2009, 10:30 PM
  3. ObjectInputStream does not initialize
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 06-03-2009, 08:11 AM
  4. Int does not initialize, will this work?
    By starchildren3317 in forum New To Java
    Replies: 2
    Last Post: 07-09-2008, 10:42 PM
  5. Initialize variables before use
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 11:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •