File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about initialization? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about initialization?" Watch "about initialization?" New topic
Author

about initialization?

Albert Lee
Greenhorn

Joined: Aug 04, 2002
Posts: 18
1. public class ArrayTest {
2. public static void main (String[]args) {
3. float f1[], f2[];
4. f1 = new float [10];
5. f2 = f1;
6. System.out.printIn (�f2[0]=� + f2[0]);
7. }
8. }
What is the result?
A. It prints f2[0] = 0.0.
B. It prints f2[0] = NaN.
C. An error at line 5 causes compile to fail.
D. An error at line 6 causes compile to fail.
E. An error at line 6 causes an exception at runtime.
The answer is A.but i think is D.because both f2 and f1 are not initialized,so it will not compile at line 6.pls help?Thanks in advance


Everyday create your history.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Both f1 and f2 have been initialized. They both refer to an array of 10 floats. Each element of the array has automatically been initialized to the default value of the float type.
Array elements are automatically initialized to default values.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Albert Lee
Greenhorn

Joined: Aug 04, 2002
Posts: 18
if the variable is class-member variable then it will be initialize automaticly.but it is in a method,so it can't be initialized automaticly.
[ November 25, 2002: Message edited by: Albert Lee ]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Array elements are always initialized to default values even if the array is declared inside of a method. To prove this fact to yourself, go ahead and compile and run the code example that you have provided.
Albert Lee
Greenhorn

Joined: Aug 04, 2002
Posts: 18
I have tried these codes.Now I know the answer.
First it dosen't mean Array elements are always initialized to default values even if the array is declared inside of a method.You should also initialize array explicitly.
eg. int[] i;
System.out.println(i[0]);
it will not compile.
Second array is an object ,so if you use keyword
new ,it will be initialized.so that question has been explicitly initialized.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Albert Lee:

int[] i;
System.out.println(i[0]);
it will not compile.

Yes, the above will not compile. The declaration, int[] i, declares only a reference to an array of type int; but it does not actually create an instance of an array.
The keyword "new" followed by a type name and dimension expression, new int[10], forms an array creation expression that results in the creation of an array and the initialization of the elements.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about initialization?