Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Increment a class Integer references value in java from another method

package myintergertest;  /**  *  * @author Engineering  */ public class Main {      /**      * @param args the command line arguments      */     public static void main(String[] args) {         //this one does not increment          Integer n = new Integer(0);         System.out.println("n=" + n);         Increment(n);         System.out.println("n=" + n);         Increment(n);         System.out.println("n=" + n);         Increment(n);         System.out.println("n=" + n);         Increment(n);          //this one will increment         MyIntegerObj myInt = new MyIntegerObj(1);         Increment(myInt);         System.out.println("myint = " + myInt.get());         Increment(myInt);         System.out.println("myint = " + myInt.get());         Increment(myInt);         System.out.println("myint = " + myInt.get());      }      public static void Increment(Integer n) {         //NO.  this doesn't work because a new reference is being assigned         //and references are passed by value in java         n++;     }      public static void Increment(MyIntegerObj n) {         //this works because we're still operating on the same object         //no new reference was assigned to n here.         n.plusplus();   //I didn't know how to implement a ++ operator...     } } 

The result for all of those is n=0. Integer n is an object and therefore passed by reference, so why isn't the increment reflected back in the caller method (main)? I expected output to be n=0 n=1 n=2 etc...

UPDATE: Notice I updated the code example above. If I'm understanding correctly, Jon Skeet answered the question of why myInt would increment and why n does not. It is because n is getting a new reference assigned in the Increment method. But myInt does NOT get assigned a new reference since it's calling a member function.

Does that sound like I understand correctly lol ?

like image 424
cchampion Avatar asked Feb 05 '10 17:02

cchampion


People also ask

Can you increment an integer in Java?

Integer objects are immutable, so you cannot modify the value once they have been created.

How do you increase an int variable in Java?

There are two ways to use the increment operator; prefix and postfix increment. The prefix increment looks like ++variablename; while the postfix increment looks like variablename++; . Both of these operations add one to the value in the variable.

How do you increment a method in Java?

In Java, the increment unary operator increases the value of the variable by one while the decrement unary operator decreases the value of the variable by one. Both update the value of the operand to its new value.

How do you increase by 2 in Java?

Java Increment and Decrement Operators 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.


1 Answers

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.

like image 177
Jon Skeet Avatar answered Sep 23 '22 03:09

Jon Skeet