Studying Java -5

Increment while now. Also, a few compound assignment operators. It's also a story that you should do it at the time of four arithmetic operations.

Increment

There is an increment operator (++) By adding "++" before and after a variable, you can add "1" to that variable.

"++ a" and "a ++" are almost the same as "a = a + 1"

But I was told that the two movements are slightly different. I couldn't quite understand the meaning.

Maybe it wasn't because I saw "++ a" or "a ++" alone.

By using a variable other than the variable to increment You may have noticed something that made a difference.

For this ↓, first do "a = a + 1" and put the result in "b".

b = ++a;

If you imagine the calculation order, it feels like "b = (a = a + 1)"? On the other hand, ↓ This is "a = a + 1" after entering the value of "a" in "b".

b = a++;

If you imagine the calculation order, it feels like "a = a + 1 from b = a"?

I decided to run it for the time being and see the difference in the results. First of all, if you put "++ a" in the usual uninflected word.

int a, b;

a = 1;
b = ++a;

System.out.println("a = " + a);
System.out.println("b = " + b);

・ Because "a = a + 1" is done first, "a = 2" ・ Because "b = a", "2" is entered, so "b = 2"

Execution result pic002.JPG Right.

Next is when "a ++" is inserted

int a, b;

a = 1;
b = a++;

System.out.println("a = " + a);
System.out.println("b = " + b);

・ Because "b = a" is done first, "b = 1" ・ Next, "a = a + 1", so "a = 2"

Execution result pic001.JPG I see, i see.

Depending on the situation, "++ a" and "a ++" may be used properly.

By the way. .. .. The decrement is subtracted by "1", and the operator is "-".

Compound assignment operator

Similar to increment, but with the formula omitted? But I think.

You can also write "a = a + 5" as "a + = 5".

This "+ =" seems to be a compound assignment operator.

・ Be careful in order "+ =" instead of "= +" ・ There are also "+ =", "-=", "* =", "/ =", etc. ・ There are ">> =" and ">>> =", but I forget them now, and I can't remember them all at once.

I will check the operation when I somehow keep in mind. Add ↓ to the usual basic form and execute.

Yomogi.java


int a = 1, b = 1;

a += 5;
b = b + 5;

System.out.println("a = " + a);
System.out.println("b = " + b);

Confirm that the same result is obtained with "+ =".

Run pic003.JPG

Until you can afford it, the main usage is likely to be "a = a + 5".

This time up to here.

Recommended Posts

Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying Java 8 (Optional)
Studying java9 (jShell)
Studying Java 8 (Stream)
Studying Java 8 (Collector / Collectors)
Studying Java 8 (see method)
Studying Java 8 (see constructor)
Java
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
Java
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
Studying Java # 6 (How to write blocks)
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Array
[Java] Polymorphism
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo