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 OverflowInteger 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);
As Grodriguez says, Integer objects are immutable. The problem here is that you're trying to increment the int value of the player ID rather than the ID itself. In Java 5+, you can just write playerID++.
As a side note, never ever call Integer's constructor. Take advantage of autoboxing by just assigning ints to Integers directly, like Integer foo = 5. This will use Integer.valueOf(int) transparently, which is superior to the constructor because it doesn't always have to create a new object.
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 :)
How to Increment a class Integer references value in java from another method - Stack Overflow
How do I use Increment operators (++) to increase a value more than 1?
How to increment a global variable when a particular function is called?
Incrementing in different java class and how does it work?
Videos
When p is inside the method it is a local variable and is a unique value for each method invocation, initialized as 1 every time.
Moving it to the class it becomes an instance variable (AKA field) and maintains its latest value for the length of the life of the class instance.
Read this doc on the 4 types of variables for more infomation.
private int p = 1; //moved
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(p == 9) {
Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
return ;
}
else {
p = p+1;
holder.textViewQuantity.setText(""+p);
}
Variable p should be a class member:
int p = 1;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(p == 9) {
Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
return ;
}
else {
p = p+1;
holder.textViewQuantity.setText(""+p);
}
No, objects aren't passed by reference. 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.
Your n++; statement is effectively
n = Integer.valueOf(n.intValue() + 1);
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 the calling method.
EDIT: To answer your update: that's right. Presumably your "MyIntegerObj" type is mutable, and changes its internal state when you call plusplus(). Oh, and don't bother looking around for how to implement an operator - Java doesn't support user-defined operators.
You could use an AtomicInteger from java.util.concurrent.atomic. It has a 'incrementAndGet' method which is suitable for your showcase.