Integer objects are immutable, so you cannot modify the value once they have been created. You will need to create a new Integer and replace the existing one.

playerID = new Integer(playerID.intValue() + 1);
Answer from Grodriguez on Stack Overflow
🌐
Reddit
reddit.com › r/javahelp › how do i use increment operators (++) to increase a value more than 1?
r/javahelp on Reddit: How do I use Increment operators (++) to increase a value more than 1?
March 6, 2022 -

Just to preface, this isnt homework - Im self learning. Ive used Stack overflow but I dont think im searching the right thing, so I cant find what Im looking for

For my study book (Learn Java the Hard Way) , i'm on an exercise where I have to increase

i = 5;

to 10 by only using ++ , i know I could do

i++; on repeated lines but my study drill says I can do it on one. Which I would like to figure out

"Add code below the other Study Drill that resets i’s value to 5, then using only ++, change i’s value to 10 and display it again. You may change the value using several lines of code or with just one line if you can figure it out. "

Perhaps ive read it wrong but I cant find a way of using only ++ to do this in one line.

Ty for the help :)

Discussions

How to Increment a class Integer references value in java from another method - Stack Overflow
References are passed by value - there's a big difference. Integer is an immutable type, therefore you can't change the value within the method. ... So, that assigns a different value to the variable n in Increment - but as Java only has pass-by-value, that doesn't affect the value of n in ... More on stackoverflow.com
🌐 stackoverflow.com
How do I use Increment operators (++) to increase a value more than 1?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
32
13
March 6, 2022
How to increment a global variable when a particular function is called?
Declare your global variable outside of the function: turn = 0 def winner(): global turn roll1 = throw() print P1," rolls a ",roll1 roll2 = throw() print P2," rolls a ",roll2 if roll1 > roll2: print P1," wins with a ",roll1,"!" return P1 elif roll2 > roll2: print P2," wins with a ",roll2,"!" return P2 else: print "It's a tie, throw again." winner() turn += 1 The global keyword doesn't make your variable global. It simply exists so you can signal to the interpreter that you indeed are referencing a global variable and not by-chance a local variable that happens to have the same name. Global variables are generally considered to be poor design, but they are handy when you don't yet understand higher-order concepts like classes or just want to make something quick and dirty. More on reddit.com
🌐 r/learnpython
9
3
August 17, 2014
Incrementing in different java class and how does it work?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
5
2
September 8, 2022
🌐
TutorialKart
tutorialkart.com › java › java-operators › java-increment
Java Increment
November 23, 2020 - public class IncrementExample { public static void main(String[] args) { int a = 7; System.out.println(++a); System.out.print(a); } } Output · 8 8 · In the following program, we shall increment a float. Java Program · </> Copy · public class IncrementExample { public static void main(String[] args) { float a = 7.2f; a++; System.out.print(a); } } Output · 8.2 · In this Java Tutorial, we learned how to use Java Increment Operator.
🌐
Rip Tutorial
riptutorial.com › the increment/decrement operators (++/--)
Java Language Tutorial => The Increment/Decrement Operators (++/--)
Using ThreadPoolExecutor in MultiThreaded applications. ... Variables can be incremented or decremented by 1 using the ++ and -- operators, respectively. When the ++ and -- operators follow variables, they are called post-increment and ...
🌐
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
This is probably how Java (and other programming language) designers felt about simple increment operations. Using the assignment operator = to increment integers by 1 at a time, is akin to a local government using nuclear weapons to stop civilian ...
🌐
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 - That’s because the increment or decrement operator is also a type of assignment operator because it changes the value of the variable it applies to. You can also use an increment or decrement operator in an assignment statement: int a = 5; ...
Find elsewhere
🌐
EXLskills
exlskills.com › courses › introduction to java › introduction to java
Increment | Introduction To Java - EXLskills
August 1, 2018 - package exlcode; public class ...intln(exampleVariableOne); } } There are two ways to use the increment operator; prefix and postfix increment....
🌐
javaspring
javaspring.net › blog › increment-operator-in-java
Understanding the Increment Operator in Java — javaspring.net
int num = 5; // Increment and use the new value int newNum = ++num; Be careful when using the increment operator in complex expressions. For example, avoid using the increment operator multiple times on the same variable in a single expression, ...
🌐
Tutorial Gateway
tutorialgateway.org › increment-and-decrement-operators-in-java
Increment and Decrement Operators in Java
March 26, 2025 - Increment Operator : ++x or x++ Decrement Operator: --x or x-- This example helps to understand the Increment and Decrement Operators practically. This program allows the user to enter two integer ...
🌐
Medium
medium.com › @AlexanderObregon › javas-atomicinteger-incrementandget-method-explained-8a4f4f521c4e
Java’s AtomicInteger.incrementAndGet() Explained | Medium
December 20, 2024 - This method performs two operations in one atomic step: it increments the value of the AtomicInteger instance by one and then returns the updated value. By atomic, it means the entire operation is treated as a single, indivisible unit, preventing interference from other threads during execution. Atomicity is achieved using low-level CPU instructions like compare-and-swap (CAS), which allows changes to be made only if the current value matches the expected value.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-increment-by-1-to-all-the-digits-of-a-given-integer
Java Program to Increment by 1 to all the Digits of a given Integer - GeeksforGeeks
November 24, 2020 - Take the integer input. Add value 1 to the input. Multiply 1 with 10 and again add them. Keep on repeating step 2 and 3 till both of them have the same length. Print the result. ... // Java Program to Increment by 1 All the Digits of a given ...
🌐
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.
🌐
JavaBeat
javabeat.net › home › how to use increment ++ operator in java
How to Use Increment ++ Operator in Java
March 20, 2024 - We declare a variable “inputNum” and initialize it with an integer “72”. Next, we employ the addition assignment operator “+=” to increment the “inputNum” value by 2.
🌐
Processing
processing.org › reference › increment.html
++ (increment) / Reference / Processing.org
int a = 1; // Sets 'a' to 1 int b = a++; // Sets 'b' to 1, then increments 'a' to 2 int c = a; // Sets 'c' to 2
🌐
EXLskills
exlskills.com › courses › java variables › java variables
Increment | Java Variables - EXLskills
August 22, 2018 - Feel free to reach out to us via live chat here! ... The increment operator changes a variable by the value of one. Instead of writing varOne = varOne + 1; you can write varOne++; and it will do the same thing.
🌐
CodingTechRoom
codingtechroom.com › question › -understanding-java-increment-operator
Understanding the Java Increment (++) Operator: How to Use It Effectively - CodingTechRoom
It can be used in two forms: pre-increment and post-increment. Understanding these forms and their effects on variable values is crucial for programming in Java. ... int a = 5; int b = ++a; // Pre-increment example: b is 6, a is 6 int c = a++; // Post-increment example: c is 6, a is 7
🌐
InformIT
informit.com › articles › article.aspx
Increment and Decrement Operators | Java Operators: Performing Operations on Primitive Data Types | InformIT
February 7, 2003 - Suffixing a variable with the increment or decrement operator returns the value to be used in the operation, and then performs the increment or decrement. For example: // Value of i is 11, but the value of a is 10 // Note that the assignment preceded the increment int i = 10; int a = i++; // Value ...
🌐
Medium
medium.com › @er.nancybhardwaj › java-basics-increment-operators-5c479c60921c
Java Basics — Increment Operators | by Nancy Bhardwaj | Medium
June 8, 2023 - In simple words, it means value is first assigned and then its incremented. ... int y= x++; // This is post increment operator, it means first value is assigned, after that it will be incremented.