SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    India
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy What is this array object and its default value

    This is a paragraph of the book I'm studyong. I could not understand the highlited statement. Please make me understand.

    Just like any other object reference, array references declared within a method must be assigned a value before use. That just means you must declare and construct the array. You do not, however, need to explicitly initialize the elements of an array.Array elements are given
    their default values (0, false, null, '\u0000', etc.) regardless of whether the array is declared as an instance or local variable. The array object itself, however, will not be initialized if it's declared locally
    . In other words, you must explicitly initialize an array reference if it's declared and used within a method, but at the moment you construct an array object, all of its elements are assigned their default values.

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not entirely sure what this book is talking about, but as for default values, if you declare an array like so:

    Code:
     int [] myArray = new int[5];
    The ints in the array will all be initialized to 0

    if it's

    Code:
     Object [] myArray = new Object[5];
    The objects will all be set to null

    Generally if it's declared locally it will always be followed by an assignment being the constructor (i.e. new int[5])

    If you make an array an instance variable, you'll probably do something like this:

    Code:
     
     private int [] someIntArray;
    You'll need to initialize this somewhere (probably the contructor) with something like:

    Code:
     
     someIntArray = new int[5];

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Mud people of Worksop
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What its saying is basically what rozner has said, except with the addition that if the array is defined locally, int[] myints; then it will be null. Obviously this is totally self explanatory so I don't know why the book bothered to mention it.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just like any other object reference, array references declared within a method must be assigned a value before use.
    That statement is true. You can't do this:
    Code:
    int[] nums;
    int[] temp = nums;
    Since nums doesn't refer to anything(not even null), it doesn't make sense to assign nums to something else.

    Just like any other object reference, array references declared within a method must be assigned a value before use. That just means you must declare and construct the array.
    That statement is false because you can do this:
    Code:
    int[] nums = null;
    int[] temp = nums;
    
    if(temp == null)
        System.out.println(temp);
    The code does not construct the array, yet the array reference nums is used in the code to do something.

    Array elements are given their default values (0, false, null, '\u0000', etc.) regardless of whether the array is declared as an instance or local variable.
    That statement is nonsensical. Array objects are not declared, they are created using new, e.g. new int[10]. If you create an array, and you don't initialize it yourself, then it will be created with default values--wherever its created in your code.

    The array object itself, however, will not be initialized if it's declared locally
    That statement is nonsensical. Array objects are not declared--you declare variables that will refer to array objects. Array objects are created using new, e.g. new int[10].

    Is that statement trying to say that if an array object is created locally that it will not be initialized? That would be false, as a simple test can prove:
    Code:
    class Apple
    {
        public void test()
        {
            int[] nums = new int[10];
            System.out.println(nums[0]);  //outputs 0
        }
            
    }
    public class AATest1
    {
         public static void main(String[] args)
        {
             Apple a = new Apple();
             a.test();
        }
    }
    In other words, you must explicitly initialize an array reference if it's declared and used within a method
    That statement is false. You do not have to initialize an array reference. To "initialize" an array reference, you assign something to the array reference when you declare it, like this:
    Code:
    int[] nums = new int[10];
    or
    int[] nums = {3, 2, 4};
    However, you do not have to "initialize" an array reference. You can do this:
    Code:
    int[] nums;  //not initialized
    ...
    ...
    int[] temp = {1, 2, 3};
    nums = temp;  //not initialized, rather "assigned a value"
    but at the moment you construct an array object, all of its elements are assigned their default values.
    The author already said that, and it's the only thing that he/she got right in the whole paragraph. The only thing worthwhile that you need to take away from that passage is that when you create an array with new, e.g. new int[10], the elements of the array are initialized with 0.

    Another thing you might want to know that wasn't discussed is that a variable that is a data member of a class is automatically initialized with 0. For instance, if you have this class:
    Code:
    class Apple
    {
        public int num1;  //data member of class
        
        public void test()
        {
           int num2;
        }
    }
    num1 will be initialized automatically with 0, but num2 will have no value and you must assign a value to it before you can use it in your program. As an exercise, change the type of num1 from int to int[], and then determine if the elements of the num1 array have been intialized with 0.

    Oh yeah, and even though you don't have to, you should always initialize the variables that you declare. If you don't know what to set them to when you declare them, then set them equal to 0.
    Last edited by 7stud; Nov 12, 2006 at 01:38.

  5. #5
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by The Book
    The array object itself, however, will not be initialized if it's declared locally
    Quote Originally Posted by 7stud
    Is that statement trying to say that if an array object is created locally that it will not be initialized? That would be false, as a simple test can prove:
    Code:

    class Apple { public void test() { int[] nums = new int[10];
    ...
    No, it's trying to say (quite accurately) that a locally declared array (or any other Object) reference will not be initialized, not even to null.

    Code:
    public class ABunchOfReferencesButNoObjects {
      private Object instanceReference; // == null
      private Object[] instanceArrayReference; // == null
      public void someMethod() {  
        Object localReference; // has no state
        Object[] localArrayReference; // has no state
      }
    }
    The author seems to be confused between what is an Object and what is a reference to an Object. If they have messed up this simple concept, the rest of the book must be similarly messed up - throw it away and get a decent book. (Such as Head First Java)

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it's trying to say (quite accurately)
    Qualifying what the book is trying to say with "quite accurately" is a travesty. Your sentence structure needs a little work.

  7. #7
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    If my worst offense is a minor grammar infraction, I can live with that


Bookmarks

Posting Permissions

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