File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array initialized

 
Jason Li
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Bill Brogdon's book, Exam Cram 2nd Edition, P50:
<code>
1. int counts[];
2. int[] counts;
3. boolean flags [ ];
4. String names[];
5. MyClass[][] things;
6. System.out.println("counts = " + counts);
If lines 1 through 6 were in a method and the method was exected, line 6 would print "counts = null" bexause the array object has not yet been constructed.
</code>
In my mind, any declaration in a method will not be initialized except array elements, but this array object is not still initialized. So I tried to compile it using JDK 1.3, it complained that counts not initialized. Does JDK 1.2 not complain?
Jason
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason,
Are you sure you copied the example exactly? You'd get an error if you put lines 1 thru 6 into a method; 'counts' is being declared twice. If you remove line 1, then you're right, you get the 'uninitialized' error. Array reference variables are not automatically initialized to 'null' in methods.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Jason Li
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for the comment after some lines of codes which i didn't copy and typo like: "bexause", others are from the book. b'cos i only have jdk1.3, i am not sure if it can run by jdk1.2. Right now i got it. Thank you, Jane.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic