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 Overflow
🌐
Tutorial 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 ...
🌐
Dummies
dummies.com › article › technology › programming-web-design › java › increment-and-decrement-operators-in-java-172144
Increment and Decrement Operators in Java | dummies
July 1, 2025 - Increment (++) and decrement (--) operators in Java programming let you easily add 1 to, or subtract 1 from, a variable.
🌐
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 (--)....
🌐
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
🌐
Dyclassroom
dyclassroom.com › java › java-increment-decrement-operators
Java - Increment Decrement Operators - Java - dyclassroom | Have fun learning :-)
// declare variable int x; // assign value x = 10; // decrease value by 1 x = x - 1; To achieve the same result we use the decrement operator --.
🌐
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: ...
Find elsewhere
🌐
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.
🌐
Codedamn
codedamn.com › news › java
Increment (++) and Decrement (–) Operators in Java Explained
June 25, 2023 - Among these operators are the increment (++) and decrement (–) operators, which are used to increase or decrease the value of a variable by 1. In this blog post, we will discuss these operators in detail and explore their use cases with examples.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › interesting-facts-increment-decrement-operators-java
Interesting facts about Increment and Decrement operators in Java - GeeksforGeeks
September 11, 2024 - Decrement operator is used for decrementing the value by 1. There are two varieties of decrement operators. Post-decrement: Value is first used for computing the result and then decremented.
🌐
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
🌐
Quora
quora.com › What-is-the-Java-expression-of-increment-the-value-of-w-by-1
What is the Java expression of increment the value of w by 1? - Quora
The ++ operator when used on an ... literal, and definitely not on an expression that evaluates to an integer value. ... The — operator when used on an integer (the types in the int family) variable, decrements its value ...
🌐
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.