• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Creation

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Here are my questions in comments
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//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.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This will print:
4 5 6
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Imad. Could you please let me know the difference between
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure whether I understand this or not. Let me modify the question like this.

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7136
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic