Variable uninitialized


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Variable uninitialized

  1. #1
    Join Date
    Oct 2004
    Posts
    21

    Variable uninitialized

    It says the variable gives might be uninitialized at the end where the statement start=gives+2;, anyone know why this is?
    PHP Code:
    int gives;
    int it 0;
                
                for(
    int i=0i<li++)
                {
                    
                    if(
    names[i].equals(v.get(start+l+1)))
                    {    
                        
    String num v.get(start+l+2);
                        
    String[] data num.split(" ");
                        
    given[i] = Integer.parseInt(data[0]);
                        
    gives Integer.parseInt(data[1]);
                        
    System.out.print(gives " ");
                        if((
    rem given[i]%gives) != 0)
                        {
                            
    received[i] = rem;
                        }
                        
                        
                        for(
    int k=0k<givesk++)
                        {
                            
    System.out.print(" ");
                            for(
    int m=0m<lm++)
                            {
                                if(
    names[m].equals(v.get(start+k+l+3)))
                                {
                                    
    received[m] += given[i]/gives;
                                    
    System.out.print(received[m] + " ");
                                }
                            }
                            
                        }
                        
                        
                    }
                    
    System.out.print(received[i] + " ");
                    
    start gives+2;
                } 

  2. #2
    Join Date
    Oct 2004
    Posts
    11
    If this line:

    Code:
    if(names[i].equals(v.get(start+l+1)))
    evaluates to false, then your variable gives is never assigned a value before it is referenced on the last line. When you declare the variable, you should initialize it to some value.

    I.e.

    Code:
    int gives = 0;

  3. #3
    Join Date
    Oct 2004
    Posts
    21
    Ok i didn't think id make a new thread but is there a way to see if every value in an array is set? Like if i initialize an array like
    int[] blah = new int[5];
    and then like
    if(othervar == othervar)
    {
    set some values of blah
    }
    and then can i see if all the values of blah are set?

    thanks

  4. #4
    Join Date
    Oct 2004
    Posts
    11
    When you create the array using the new keyword, each array element is automatically initialised to the default value for the primitive. (Primitive types include bytes, chars, ints, longs, floats, etc, etc.) The defaut value for the integer type is 0, so all elements will automatically be initialised to 0. Thus, all the elements are already 'set'.

    To summarise and avoid any further confusion...

    If you declare a primitive variable (e.g. byte, char, int, float, etc.) as a class/instance variable (i.e. at the class level, not within a method), then the variable is automatically assigned its default value, assuming you don't assign a value yourself. For numeric primitives, this is always 0. For booleans, the default is false.

    Array elements behave the same way, wherever/whenever (sorry for the Shakira link there) they are created using the new keyword.

    Objects declared as class/instance members are automatically initialized to null.

    If you declare a primitive variable within a method, the variable is local to whatever block the variable is declared in, and the variable is NOT initialised. This is why you have to initialize local variables before you use them. Similarly, objects declared with local scope are not initialized. To avoid compiler errors, it's often a good idea to initialize them to null at declaration time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center