Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

What is the point of ++ at the end of this array statement?

 
Ranch Hand
Posts: 113
7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

The following code and question come from one of Enthuware's mock exams.

The following class will print 'index = 2' when compiled and run.


The answer to the question being true. This is not what I want to focus on though. I am wondering what the ++ at the array statement is doing. I have rewritten the code to illustrate what I mean.


I do notice that if I omit the ++, I get the following error:

Test.java:5: error: not a statement
     getArray()[index=2];
               ^
1 error



So it must be doing something. If I replace the getArray() method with a regular array, it compiles without problems. What is ++ doing there? What does it increment?

Thanks in advance!

Regards,
Shane
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
If I replace the getArray() method with a regular array, it compiles without problems. What is ++ doing there? What does it increment?



The method call is supposed to return an int array, the brackets which follows is supposed to dereference the array to an element, and ... with the int element, the ++ is a post increment operator, which of course, should increment the element.

Henry
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Shane Jensen wrote:
If I replace the getArray() method with a regular array, it compiles without problems. What is ++ doing there? What does it increment?



The method call is supposed to return an int array, the brackets which follows is supposed to dereference the array to an element, and ... with the int element, the ++ is a post increment operator, which of course, should increment the element.

Henry


Right, of course. How did I not see that? Thanks!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:If I replace the getArray() method with a regular array, it compiles without problems.


Just to be crystal-clear: if the ++ operator is omitted, the code will still not compile. The statement on line5 (without the ++ operator) is still an invalid statement (and using a regular array will not have any effect).
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:Right, of course. How did I not see that?


Now let's take this one step further What's the output of this code snippet?
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Shane Jensen wrote:If I replace the getArray() method with a regular array, it compiles without problems.


Just to be crystal-clear: if the ++ operator is omitted, the code will still not compile. The statement on line5 (without the ++ operator) is still an invalid statement (and using a regular array will not have any effect).


Good point! Thanks for adding that.

Roel De Nijs wrote:

Shane Jensen wrote:Right, of course. How did I not see that?


Now let's take this one step further What's the output of this code snippet?


Interesting addition. I tried this earlier as well. You'd expect the value to be incremented once it's being printed, but of course getArray() return an entirely new array, so the output is "element = 3" rather than "element = 4".

For any future readers: the following will output "element = 4":

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:For any future readers: the following will output "element = 4":


Yeah, but that's way to easy. Give those future readers a challenge

What's the result of this code snippet?Or what about this one?And here's another oneThere are no limitations to your own imagination. The world is your oyster!
 
Ranch Hand
Posts: 371
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

Henry Wong wrote:which of course, should increment the element.

Henry




Is this really on the test?  If so, in light of the above, why is the "should" being applied?  In other words, what are the rule(s) with respect to the ++ at the end of these array statements (compared to other statements where it does increment the element)?  
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:If so, in light of the above, why is the "should" being applied?  In other words, what are the rule(s) with respect to the ++ at the end of these array statements (compared to other statements where it does increment the element)?  


Honestly I don't really understand what you are asking here. And I had a look at your code snippet, but really don't see anything wrong/strange/weird in the different outputs. So could you tell us about which statements you have doubts about and/or were expecting a different output?
 
Charles O'Leary
Ranch Hand
Posts: 371
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Charles O'Leary wrote:If so, in light of the above, why is the "should" being applied?  In other words, what are the rule(s) with respect to the ++ at the end of these array statements (compared to other statements where it does increment the element)?  


Honestly I don't really understand what you are asking here. And I had a look at your code snippet, but really don't see anything wrong/strange/weird in the different outputs. So could you tell us about which statements you have doubts about and/or were expecting a different output?



So far in this entire thread, I personally haven't been able to hone in on any specific JLS that informs me of why Output lines 1 & 2(post increment) have the same 2 (String) returned.  As well as 3(post increment) & 4 with the 3 same (String) return.  As well as 5(post increment) & 6 with the same 3 (String) return.  From a JLS perspective, why do these pairs match, when

Shane Jensen wrote:You'd expect the value to be incremented...  


in one of each of these pairs (that is: without the benefit of hindsight after actual compilation and actual execution).  

Essentially, "What is the point of ++ at the end of this array statement?"  It seems as if I missed the take away from these tweaks.  Please advise.    
 
Enthuware Software Support
Posts: 4466
42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:

Roel De Nijs wrote:

Charles O'Leary wrote:If so, in light of the above, why is the "should" being applied?  In other words, what are the rule(s) with respect to the ++ at the end of these array statements (compared to other statements where it does increment the element)?  


Honestly I don't really understand what you are asking here. And I had a look at your code snippet, but really don't see anything wrong/strange/weird in the different outputs. So could you tell us about which statements you have doubts about and/or were expecting a different output?



So far in this entire thread, I personally haven't been able to hone in on any specific JLS that informs me of why Output lines 1 & 2(post increment) have the same 2 (String) returned.  As well as 3(post increment) & 4 with the 3 same (String) return.  As well as 5(post increment) & 6 with the same 3 (String) return.  From a JLS perspective, why do these pairs match, when

Shane Jensen wrote:You'd expect the value to be incremented...  


in one of each of these pairs (that is: without the benefit of hindsight after actual compilation and actual execution).  

Essentially, "What is the point of ++ at the end of this array statement?"  It seems as if I missed the take away from these tweaks.  Please advise.    



You don't need JLS for this because nothing special is going on in these statements.
Can you do this? After each print statement, write the value of index and the values contained in the array at 3 indices?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:So far in this entire thread, I personally haven't been able to hone in on any specific JLS that informs me of why Output lines 1 & 2(post increment) have the same 2 (String) returned.  As well as 3(post increment) & 4 with the 3 same (String) return.  As well as 5(post increment) & 6 with the same 3 (String) return.


Although I still don't have a complete grasp on what you are uncertain about and which are your actual doubts, I'll have a go and try to provide some advice and answers. But I think your main doubt is that you would expect a different value being printed in the statement following a statement with the post-increment operator. Because there are a few little details involved in this code snippet, we'll take it step-by-step.

First of all, I assume you know the difference between the pre- and post-increment operator. So you should know that the following code snippets produce a different outputIf you don't, this topic is definitely a must-read for you. So if you would change the post-increment operator in your code snippet to the pre-increment operator (e.g. ++new Test3().getArray()[index=1] - that's very weird looking syntax, I know, but it compiles successfully ), the output would be completely different.

Secondly the array arr (in class Array) is an instance variable. So every time you create a new instance of Array, a new array will be created with the initial values (1, 2, 3). In methods getArray() and getArray1() you are creating a new Array instance on each method invocation. So if you would change array arr (in class Array) to a class variable, the output of would be completely different too (using the post-increment operator); because in each statement the same array is used.
And because you are creating a new array arr on each invocation of methods getArray() and getArray1(), you are unable to see the result of the post increment operator. So either store the array first (before applying the post-increment operator) or use a seperate method to post-increment the element. Here's an example of the latter approachOutput:
1)[index=1]; element = 2
2)[index=1]++;
 - before: [1, 2, 3]
 - element = 2
 - after: [1, 3, 3]
3)[index=2]++;
 - before: [1, 2, 3]
 - element = 3
 - after: [1, 2, 4]
4)[index]; element = 3
5)[index]++;
 - before: [1, 2, 3]
 - element = 3
 - after: [1, 2, 4]
6)[index]; element = 3


Hope it helps!
Kind regards,
Roel
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic