• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Local Array

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
1.)
I am little confuse after doing test in exam cram. it says ' Primitive number arrays that are member variables are automatically initialized to zero when constructed. but that is not true of variables declare inside methods."
I am totally agree with first part of the statement. but i don't understand what exactly second part wants to explain? because as per my reasoning local array also gets initialized to zero when constructed even they are define inside the method.
Please explain to me and feel free to correct me.
2.)
It is a general question. is it worth of doing same mock exam over again and again and again ? does it help u to understand the concept more effectively ? does u help to remember key points ?
regards
vivek
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

variables declare inside methods.
1. You are correct about arrays. However, IMO, what
the author refers is to method variables
(other than arrays).

Output:
Member Array element = 0
Class Array element = 0
Please note that I have declared a boolean, without
initialization. This is okay since I have never used it.
Once I use it in my program, then it looks for
initialization.

2.
Yes, IMO, it helps. In a way that you are confident that
you don't repeat the same mistakes again. However this is
a very open qstn and many people have varied opinions.
Regds.
- satya
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
Here's the ranch dressing for Satya's salad..
Class variables( whether arrays or non-arrays ) are always initialized to their default values.
Arrays are ALWAYS initialized. Whether local or class level. They are initialized to the default value of their type. ie., 0 for integral types, 0.0 for double, false for boolean and null for object arrays.
Non-array local variables are not initalized. You will get a compiler error ONLY when you try to access an uninitialized (non-array) local variable.
Again, these are important concepts for the exam. So make sure you understand them thoroughly. If need be, write some code.
Good luck,
Ajith
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I have to agree with you because I coded, compiled and executed the following two classes with the result J=0 as I expected when i[] is an instance variable but not when it was a local variable of method x(). Now I think that arrays of primitives are the exception to the rule that local variables don't get initialized:
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The less obvious point to be made obvious in our discussion here is this.
All Array elements of an array irrespective of whether the array has instance/class/local scope, always get initialized to their default values, provided the array itself was constructed in the first place.
In other words, still the concept of all member var gets initialized automatically and local vars NOT get initialized automatically holds good for vars of array type declaration also. for example take a class like this.
<pre>

class Test {
static int[] memArray;
static int[] memArray1 = new int[10];
public static void main(String[] args) {
int[] localArray;
int[] localArray1 = new int[10];
System.out.println(memArray); // prints null
//System.out.println(localArray); //COMPILE error
System.out.println(memArray1[0]); //prints 0
System.out.println(localArray1[0]); //prints 0
}
}

</pre>
If you look closely , in the above code the uninitialized array var memArray gets initialized to null wheras
the local array localArray
is NOT auto inited to null and compiler error occurs when you try to access this local array var.
The extra point to note in case of array vars is ,since they have an added attribute called array elements, what happens to them ? It is just the fact that if and only if the array gets constructed in memory with new ***[size], the added attributes called 'array elements' get initialized to their defaults irrespective of whether the array has local/instance/class level scope.
Do you all agree with the point what I am trying to say ?
Vivek,
Regarding your 2nd qstn, to a certain degree , yes taking mocks again and again reinforces the key concepts. In my opinion, you save them for the last 1 month. Before that you participate actively here. Then start taking the mocks from the easier to harder ones.
regds
maha anna


[This message has been edited by maha anna (edited June 04, 2000).]
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you.
Yes maha anna your point is really good. i have noted down it.
regards
vivek
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha - Yes, I agree with your point.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha, i agree with you. I would also like to add the following comment:
int i[] = new int[10];
In my opinion there are two variable types
1) reference to array -- i
2) array elements -- i[0].. i[9]
array elements when created will always be initialised, doesnot matter whether it is local or instance.
reference to array (placeholder of the address of array object) will not be initialised if it is declared locally.
(i.e) void method(x) {
int i[]; // will not be initialised
/* both array reference and array element get initialised */
int j[] = new int[10];
}
let me know if i am wrong.
cheers
sankar
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Sankar. This is true with any reference for that matter. If you create a reference to an Object, it will not be initialized. Trying to accesss members/variables will raise NullPointerException.
References are just handles to Objects. They have an associated type and point to some memory location where the actual object is stored.
Ajith
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic