• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCAJP 7. Operators. Increment and decrement

 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was doing some tests in the Internet on SCJP 6. There was one question on increment and decrement. Could someone help to get an understanding of it? The code:



Regards,
Sergei Zhylinski
 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sergei ,
Following is explanation .


Hope this helps.

this example is good candidate question for OCAJP 7 Exam.
Thank You for your question.I am preparing for OCAJP 7 Exam.
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Please have a look for yourself at other two examples. They may also be encountered on the test. They are pretty easy, but nevertheless.

Code 1:



Code 2:

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Zhylinsky wrote:Hi All,
I was doing some tests in the Internet on SCJP 6. There was one question on increment and decrement. Could someone help to get an understanding of it? The code:


also it would be good practice to place expression on right side in parentheses:

and pay some attention for %, %= and /, %= because it may throw ArithmeticException ( /0 ), especially %, %= because it has result that will differ in sign from /:
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This is usefull information to pay attention to.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation of aditya chaudhari is great but the "temporary" values of k (after using a prefix and/or postfix operator) are not mentioned. For the code snippet from the original post it's not really useful, because each time variable k is assigned another value (and this "temporary" value isn't used). But on the actual exam it could be important. So let's throw in another example and see if you have a good understanding of prefix and postfix operators.

What's the output of this code?


And what's the output if we swap postfix operators with prefix ones and vice versa?


If you answer both questions correctly, you'll ace every question on the actual exam about prefix and postfix operators.

Good luck!
 
Sibendu Dey
Greenhorn
Posts: 20
Java Oracle Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergei Zhylinski wrote:Thanks. Please have a look for yourself at other two examples. They may also be encountered on the test. They are pretty easy, but nevertheless.

Code 2:




Can anyone explain me why the value of i is taken as 1 instead of 2 while evaluating index for iArray ?

Thank you
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
15.26.1. Simple Assignment Operator =
If the left-hand operand is an array access expression (ยง15.13), possibly enclosed in one or more pairs of parentheses, then:
First, the array reference subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the index subexpression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.
Otherwise, the index subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and the right-hand operand is not evaluated and no assignment occurs.
Otherwise, the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
Otherwise, if the value of the array reference subexpression is null, then no assignment occurs and a NullPointerException is thrown.
Otherwise, the value of the array reference subexpression indeed refers to an array. If the value of the index subexpression is less than zero, or greater than or equal to the length of the array, then no assignment occurs and an ArrayIndexOutOfBoundsException is thrown.
Otherwise, the value of the index subexpression is used to select a component of the array referred to by the value of the array reference subexpression.

may be this can help



iArray[i] = z = ++i; - first iArray determine on which array you will work, next index expression determined iArray[1], next ++i, next z=2, next check if iArray refer to null, next check that index is right, next iArray[1] = 2;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic