• Post Reply Bookmark Topic Watch Topic
  • New Topic

empty statement  RSS feed

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

m()[i=2]++; how this is valid ? after the method invocation there should be ; i.e m(); [i=2]++;


Exception in thread "main" java.lang.NullPointerException
at No.main(No.java:10)
 
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing invalid here. your method m() returning an integer array. you are mentioning the m()[i=2]++;
While you are assigning the 2 to the i, you are also trying to access the 3rd element of the array(array returned by the method m()). then at the same time you are increasing the value of the element by one.
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i am getting ++ has higher precedence than = , so ++ will be evaluate first than i=2 ?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I don't feel that precedence is playing any role here. you are asking to increment the value of array element by one by mentioning ++.
If I says:

then to evaluate the value of the array element it is required to execute the statement inside the [] brackets so that I can know on which element I am going to perform an operation. Then I can look for the further operation. and as soon as you execute the statement inside [] you get the value of i=4 so any operation that you want to do will be operated on arr[4].
it may be ++arr[i=4].
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:Personally I don't feel that precedence is playing any role here...

Actually, I think it does, but the fact is that the precedence of '=' plays no part.

@Shalini: The only operators that have any precedence you need to worry about here are: '()', '[]' and '++', and I'm pretty sure that their precedence is in the order they appear (always have to look it up ).
So: the method is evaluated first, the 'element of' second (and, in order to do that, the '=' inside it has to be evaluated), and then the postfix '++'. Think of the '[]' as acting like a set of brackets around the '='.

PS: I have no idea what your question has to do with the subject line.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!