Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anyone explain please

 
Soum Sark
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
Just on the question below. Why is line 5 not compiling. It must be something very simple I am missing here...any help would be appreciated
1. class ArrayTest {
2.
3. static int [ ] intArray = new int [5] ;
4. static int [ ] intArray1 = new int [1] ;
5. intArray1 [0] = 5 ;
6. static char [ ] charArray = new char [5] ;
7.
8. public static void main (String [ ] args) {
9.System.out.println (charArray [1] ) ;
10.intArray1 = intArray ;
11.intArray = charArray ;
12. }
13. }

a. The compiler objects to line 5.
b. The compiler objects to line 10.
c. The compiler objects to line 11.
d. Omitting lines 5,10 and 11 would make the program print 0.
e. There is nothing wrong with the code and the program prints 0
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 5 is an assignment statement so it must appear in a code block such as a method or constructor body or an initializer block.
 
Sachin Choudhari
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Soum,
You can declare and initialize an array at the same time. But you can not initialize it seperately out side the method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic