In order to pass the OCPJP and OCAJP exams, you need to have a strong understanding of both the five basic mathematical...
operations and the intricacies of the unary operators.
As far as mathematics go, you need to understand addition, subtraction, multiplication, division and the modulus/remainder operator. You also need to know the various mathematical shortcuts like += and *= that can be used with the five basic mathematical operators.
As far as the unary operators go, you need a good understanding of both the postfix and prefix operators and how they work when they're involved in a relatively complex expressions.
A tricky OCAJP mock exam question on operator precedence
Now, every Java developer knows the basics of how addition works, or how the unary increment or decrement operators work. But given a relatively unorthodox combination of these operators, would a typical developer have confidence in predicting the result? It's an unorthodox application of these operators that tend to trip users up on these Java certification exams.
Take a look at the following code snippet. It's symptomatic of what Oracle will throw at you on the Associate or Professional certification exams for Java 7:
int x = 2; x += x++ * x++ * x++; System.out.println(x);
What gets multiplied here? Do all three increments happen and then the multiplication happens? Will we have 2*2*2? Or will the increment happen first, giving us 5*5*5? Or will the increments on the variable x happen progressively, giving us 2*3*4?
And what about the mathematical operator? x+=1 translates into x=x+1. But what does x += x++ * x++ * x++; translate into? Here's a couple of suggestions that one might suppose was correct:
x = (x+x++) * x++ * x++; x = x + (x++ * x++ * x++); x = (x + x++ ) * (x + x++ ) *(x + x++ );
Mathematical shortcuts and operator precedence
Well, as far as the mathematical shortcut goes, any time you have a += type of expression, the variable on the left hand side gets applied to the result of the right hand side when the whole thing on the right hand side has completed. So in this case, our expression can be simplified from:
x += x++ * x++ * x++;
x = (x) + (x++ * x++ * x++);
And since the initial value of x is two, it could be simplified once more to:
2 + (x++ * x++ * x++);
Increment operator and operator precedence
Now, as far as the increment operator goes, with a postfix, the variable is used, and then immediately incremented. At runtime, our code would be equivalent to:
2 + (2 * 3 * 4);
As you can see, each time the postfix is encountered, it outputs the current value of x, and then increments the variable immediately after, giving us 2*3*4, or 24. When added to the number 2, we get the final output of our code snippet, which is the number 26.
So, you've got it? It's pretty straight forward once you've been walked through it. And there's no doubt that you'll have no difficulty figuring out what the result would be if we were using a unary prefix instead of a unary postfix.
int x = 2; x += ++x * ++x * ++x; System.out.println(x);
Work it out in your head, and then throw it into a compiler and see if your thought process is correct. That's really the best way to learn.
New to Git and distributed version control? Here are some Git examples and Jenkins-Git integration tutorials designed to help you master the popular source code versioning tool.
- The five key Git commands to master first
- Change a commit and manipulate commit history with this hard git reset example
- What happens when you git reset and push?
- Learn to undo a commit with git revert
- Use the git cherry-pick command across branches
- Change the default Git commit editor to Notepad++
- Where the Git configuration files are stored
- Make continuous integration part of your DevOps journey with this Jenkins CI tutorial
- Include Jenkins Git environment variables in your freestyle builds
- Why are Jenkins balls blue not green?
- See how you would do on the Jenkins, DevOps and GitHub interview