This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
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
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Tony Alicea
Desperado
Sheriff
Joined: Jan 30, 2000
Posts: 3219
posted
0
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:
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
maha anna
Ranch Hand
Joined: Jan 31, 2000
Posts: 1467
posted
0
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
Joined: Jun 03, 2000
Posts: 277
posted
0
Thanks to all of you. Yes maha anna your point is really good. i have noted down it. regards vivek
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
Maha - Yes, I agree with your point. Ajith
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Joined: Mar 17, 2000
Posts: 5782
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.