This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local variable initialization - source: http://www.jchq.net/mockexams/exam1.htm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local variable initialization - source: http://www.jchq.net/mockexams/exam1.htm" Watch "Local variable initialization - source: http://www.jchq.net/mockexams/exam1.htm" New topic
Author

Local variable initialization - source: http://www.jchq.net/mockexams/exam1.htm

Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
I was working on Q10 and I got a doubt:

public class Q {
public static void main(String argv[]){
int anar[]=new int[5]; //line 1
System.out.println(anar[0]); //line 2
}
}

It prints "0" without initialization of the array.
However, If I replace the line 1 and line 2 with the below code, its gives the compilation error.

int anInt; //line 1
System.out.println(anInt); //line 2

I understood the error message: "The local variable anInt may not have been initialized".

Can somebody explain the reason behind allowing to access uninitialized array but not uninitialized int?

Thanks for your time.


Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
wei luo
Greenhorn

Joined: Jul 17, 2008
Posts: 14
the Array Object will be initialized no matter where it is declared ( as method local or instance or class variable), and the default value is the default of the type
yen hoang
Ranch Hand

Joined: Apr 05, 2009
Posts: 58
There is a rule for array elements you should remember:
Array elements are always, always, always given default values, regardless of where the array itself is declared or instantiated
Have fun and good luck


I love Mozart (^o^)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variable initialization - source: http://www.jchq.net/mockexams/exam1.htm