wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Creation" Watch "Array Creation" New topic
Author

Array Creation

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, Here are my questions in comments
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

//Why compile error .. Syntax error, insert "AssignmentOperator Expression" to complete Expression


Assign the array to a variable to fix the error.

int[] it = new int [][]{{1}}[0];//what is the meaning of this statement?


An anonymous array with the contents 0,0 = 1 was created. You take this array and refer to the first (0) row which is assigned to it. That means it[0] is now 1.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Deepak Bala wrote:Assign the array to a variable to fix the error.


Hi Bala, I am looking for reason why line 4 is giving error.

An anonymous array with the contents 0,0 = 1 was created. You take this array and refer to the first (0) row which is assigned to it. That means it[0] is now 1.

This explanation is confusing..Can you please write in a simple way
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87


This will print:
4 5 6


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Thanks Imad. Could you please let me know the difference between
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Dear Saibabaa ... this code (new int[2];//Compile Error..Why ?? ) is an (Array Initializer) and as the JLS stated :
(An array initializer may be specified in a declaration, or as part of an array creation expression (ยง15.10), creating an array and providing some initial values)
so the code above is not a legal statment and will not create an array object by itself unless its used in a declaration or as part of an array creation expression" in other words there must be a left value contains either an array declaration or a refrence to array".
I thought the other way, means by creating an anonymus array, will work like the code below:
new int[]{1,2};
which will create an anonymous array of two int and returned a refrence to this int[], but using this my compiler throws [Not Statment]. I started searching the internet for a whole 24 hours and reading the JLS until I finally got THIS
I'm not fully confienced by this.. and i hope some one will elaborate on the subject.

regards
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
I am not sure whether I understand this or not. Let me modify the question like this.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

line 4 (of your code) gives error because its useless. Line 5 (of your code) on the other hand makes a constructor call. A constructor is a method so the call doesn't need to be assigned to a reference variable as it might have some side effects. Its a similar situation to this


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Dear Ankit, My question is :
line 4 will make a constructor call and create a new Object, and this is permisable. Then why its not permitted to construct anonymous array by the call at 5?. why the side effect in 4 might be needed and the side effect in 5 is not ?? !!

regards
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
I can understand that writing only literals without any Left Hand Value is meaning less, but creating objects has sure other side effects , then for Consistency Sake, I thought Java will allow creation of Array objects as it allow creation of any other object.

regards
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3019
    
  35

Imad Aydarooos wrote:I thought Java will allow creation of Array objects as it allow creation of any other object.

Creation of other objects may probably do something helpful. Suppose that there are some statements in the constructor. Probably, these statements could do something as showing a GUI window, or something else which is useful. So you can make these statements run just by instantiating that class. The creation of an array, on the other hand, does nothing other than just creating a set of empty elements. Generally, there is no way to place a set of statements to get fired upon an array creation. Therefore, if you create an array without making any reference to it, that array will be definitely useless, and thus it's not allowed.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Ankit,
1. Line 4 doesn't give error..
2. How Line 5 makes constructor call.
3. I think I am lost with Line 5 for your/others explanations. Could you please tell me why lines 5 (new int[] {1, 2,3};) is giving error.
Ankit Garg wrote:line 4 gives error because its useless. Line 5 on the other hand makes a constructor call. A constructor is a method so the call doesn't need to be assigned to a reference variable as it might have some side effects. Its a similar situation to this
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Saibabaa Pragada wrote:Hi Ankit,
1. Line 4 doesn't give error..
2. How Line 5 makes constructor call.
3. I think I am lost with Line 5 for your/others explanations. Could you please tell me why lines 5 (new int[] {1, 2,3};) is giving error.

1. Line 4 of my code WILL give an error, I don't know how you tried it. (edit: I edited my previous post to clarify that I was talking about line 4 of your code not mine)
2. Line 5 in your code is not the initialization of int[], its the call to new Object(). I suppose you know that will call the constructor of Object class.
3. Again, the statement new int[]{1,2,3} is useless. The int array that you are creating is wasted, its of no use, its not accessible by any means. But in case of a constructor call, the call to the method makes the statement valid even without assigning it to a reference variable...
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
I have 2 different code snippets in this thread. That lead to confusion. I am writing the answer for my question.

new Object();//Works fine because Object class has no constructors and we are calling default constructor.
new int[] {1, 2,3}; // Does not work because there is no matching class and obviously no constructor. In case of Arrays, assigning is the only option.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Saibabaa Pragada wrote:new Object();//Works fine because Object class has no constructors and we are calling default constructor.

This has nothing to do if we call the default constructor or parameterized one. We can do this too
The point again is creating an array and not assigning it to anything would waste that array so its not allowed. Calling a method or constructor without an assignment on the other hand is allowed as it will not be useless in all cases. Like the example Devaka gave, we can show a GUI window in a constructor, or there could be many such things that can be done in a constructor...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Creation
 
Similar Threads
Static Methods
illegal start of expression -- arrays
Question on Dan's Mock
Access modifiers within methods
array declaration and creation