Core Java Durga
Core Java Durga
Operators are the most important topic in SCJP. Do you know the value of 2 * 3?
Don't think too much; its value is 6. Okay, from our learning of addition,
subtraction, multiplication, and so on, there is nothing new in the concept of
operators, right? However, do you know the best book for SCJP is by Kathi Sierra?
If you open this book and read the section on operators, she mentions an important
point in the introduction: most exam takers score the least in the operator
concept. Like, I remember when I prepared for my exam, I didn't face any difficulty
in mathematics, so I ignored the topic. But then, in the exam, I struggled. Later,
students started coming to me saying, "Sir, this is my score," with many students
barely passing the SCJP exam due to low scores in the operator concept.
I remember one student who scored 87% overall in SCJP, scoring 100% in language
fundamentals, exception handling, and collections concepts, 95% in multithreading
concepts, but only 36% in operators and assignment concepts. The score distribution
in the old version of the exam showed this clearly. When I opened the book again, I
underlined the statement: "most exam takers score the least in the operator
concept." That shows the power of this concept. Do not underestimate this topic,
even though it is very basic. There are several loopholes that you need to remember
very carefully.
Now, let's discuss the agenda related to operators. Our agenda includes:
Pre-increment (++x)
Post-increment (x++)
For example, if int x = 10; int y = ++x;, y will be 11 because it is pre-increment.
If int x = 10; int y = x++;, y will be 10 because it is post-increment.
Pre-decrement (--x)
Post-decrement (x--)
For example, if int x = 10; int y = --x;, y will be 9 because it is pre-decrement.
If int x = 10; int y = x--;, y will be 10 because it is post-decrement.
We can apply increment and decrement operators only to variables, not to constant
values. For example, 10++ is invalid and will cause a compile-time error.
Next, nesting of increment and decrement operators is not allowed. For example, ++
(++x) is invalid.
Let’s conclude this section with an example:
csharp
Copy code
int x = 10;
int y = ++x + x++;
System.out.println(y); // Output will be 22 because ++x is 11 and x++ is also 11
before incrementing to 12.
In summary, the key points for increment and decrement operators are: