You are using Integer which is an immutable object.
Basically your code is
y = new Integer(y.intValue() + 1);
and
y = new Integer(y.intValue() - 1);
Therefore you're creating two new Integer objects that are not the same (==) as the previous objects.
This behaviour is called autoboxing in Java.
Answer from Uwe Plonus on Stack OverflowTutorial Gateway
tutorialgateway.org › increment-and-decrement-operators-in-java
Increment and Decrement Operators in Java
March 26, 2025 - Increment and Decrement Operators in Java are used to increase or decrease the value by 1. For example, the Incremental operator ++ is useful to increase the existing variable value by 1 (i = i + 1). Moreover, the decrement operator – – is ...
Baeldung
baeldung.com › home › java › core java › a guide to increment and decrement unary operators in java
A Guide to Increment and Decrement Unary Operators in Java | Baeldung
February 17, 2025 - int operand = 2; operand--; //operand = 1 int number = operand--; // operand = 0, number 1 · Similarly, post-increment and post-decrement unary operators should be on their own line rather than including them in larger expressions. In this quick tutorial, we learned about the increment and decrement unary operators in Java.
InformIT
informit.com › articles › article.aspx
Increment and Decrement Operators | Java Operators: Performing Operations on Primitive Data Types | InformIT
February 7, 2003 - In computer programming it is quite ... The increment operator is denoted by two plus signs (++), and the decrement operator is denoted by two minus signs (--)....
Top answer 1 of 8
6
You are using Integer which is an immutable object.
Basically your code is
y = new Integer(y.intValue() + 1);
and
y = new Integer(y.intValue() - 1);
Therefore you're creating two new Integer objects that are not the same (==) as the previous objects.
This behaviour is called autoboxing in Java.
2 of 8
3
Change your
Integer y = 567;
Integer x = y;
to
int y = 567;
int x = y;
and the suprprising behavior will be gone. My guess is that you have stumbled upon Java's implicit autoboxing of primitive values into wrapper objects, and are lead to believe that you are directly manipulating the numbers.
CodeGym
codegym.cc › java blog › java numbers › increment and decrement unary operators in java
Increment and Decrement Unary Operators in Java
It decrements the value by 1 where --x = x-1. It logically inverts the value of a boolean like if x = true, then !x will be false. The increment (++) operator (also known as increment unary operator) in Java is used to increase the value of ...
Published December 5, 2024
Mathbits
mathbits.com › JavaBitsNotebook › Looping › Increment.html
Increments and Decrements - JavaBitsNotebook.com
increment and decrement operators work only with integer variables -- not on floating point variables and not on literals · the execution of the prefix and postfix is under Java control -- nothing you can do will change the order of execution -- parentheses will not force an early execution: ...
CodeScracker
codescracker.com › java › java-increment-decrement.htm
Java Increment and Decrement Operator
The postfix decrement operator, of course, decreases the value by one, but it does so after it is used. In Java, the postfix decrement operator has the following general form: ... public class PrefixIncrementExample { public static void main(String[] args) { int x = 5; int y = x--; ...
Freejavaguide
freejavaguide.com › increment_decrement_operators.htm
Increment & decrement operators - Java tutorial | freejavaguide.com
Before you can develop corejava applications, you'll need to download the Java Development Kit (JDK). ... There are 2 Increment or decrement operators -> ++ and --. These two operators are unique in that they can be written both before the operand they are applied to, called prefix increment/decrement, or after, called postfix increment/decrement. The meaning is different in each case. ... x = 1; y = ++x; System.out.println(y); prints 2, but x = 1; y = x++; System.out.println(y); prints 1
TutorialKart
tutorialkart.com › java › java-operators › java-decrement
Java Decrement
November 23, 2020 - Java Decrement operator takes single operand as input and decrements the value of operand by 1.. ... Following is the syntax for Decrement Operator. ... The first form of decrement is called pre-decrement.
Saylor Academy
learn.saylor.org › mod › book › view.php
Java Data and Operators: Increment and Decrement Operators | Saylor Academy
In addition to the increment operator, Java also supplies the decrement operator --, which can also be used in the predecrement and postdecrement forms. The expression --k will first decrement k's value by 1 and then use k in any expression in which it is embedded.
YouTube
youtube.com › watch
24 - Incrementing and Decrementing Variables in Java Code - YouTube
Get more lessons like this at http://www.MathTutorDVD.comIn this lesson, we learn how to increment and decrement variables in Java code. This is useful when...
Published March 16, 2018
Oracle
blogs.oracle.com › javamagazine › java-increment-decrement-operators
Quiz yourself: Understanding the syntax of Java’s increment and decrement operators | javamagazine
January 9, 2023 - All that happens is that the contents of the variable are unboxed, the resulting int is incremented or decremented, and then a new Integer is created with that new value. Finally, the reference to the new Integer is assigned to the variable. Look at line 1 considering the information above. The effect would be to assign zero to an element of the array at a subscript one greater than the int value of i before executing the line. A second effect would be that the int value in the object referred to by i would now be one greater than before.
Programiz
programiz.com › article › increment-decrement-operator-difference-prefix-postfix
Increment ++ and Decrement -- Operator as Prefix and Postfix
In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.