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

后端 未结 10 534
攒了一身酷
攒了一身酷 2020-12-09 02:29
package myintergertest;

/**
 *
 * @author Engineering
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void          


        
相关标签:
10条回答
  • 2020-12-09 03:04

    Side note: In my humble opinion, writing n++ on an Integer object is extremely misleading. This relies on a feature of Java called "autoboxing" where it converts primitives to their corresponding boxing objects -- like int to Integer or boolean to Boolean -- automatically and without warning. Frankly I think it's a bad feature. Yes, sometimes it makes your life simpler by automatically doing a handy conversion for you, but it can also do conversions that you did not intend and do not realize are happening, with unintended side effects.

    Anyway, there are two ways to accomplish what you are apparently trying to do:

    One: Have the increment function return a new Integer with the updated value. Like:

       public Integer increment(Integer n)
        {
          Integer nPlus=Integer.valueOf(n.intValue()+1);
          return nPlus;
        }
    

    then call it with:

    Integer n=Integer.valueOf(0);
    n=increment(n); // now n==1
    n=increment(n); // now n==2
    

    Etc.

    Two: Create your own class that resembles Integer but that is mutable. Like:

    class MutableInteger
    {
      int value;
      public MutableInteger(int n)
      {
        value=n;
      }
      public void increment()
      {
        ++value;
      }
      ... whatever other functions you need ...
    }
    

    Of course the big catch to this is that you pretty much have to re-invent the Integer class.

    0 讨论(0)
  • 2020-12-09 03:08

    You are looking for something similar to C++'s reference or to C#' out parameter. Java (and many other languages) does not support this.

    This type of behavior is typically achieved by changing the method from void to (in this case) int: public static int increment(int n) { return ++n; }

    If the method is already returning something then you can create a new class that has two fields (can even be public): the original return value and the new value of n.

    Alternatively, you can also wrap the integer inside a generic Cell class. You only need to write this Cell class once:

    public class Cell<T> {
      public Cell(T t) { value = t; }
      public void set(T t) { value = t; }
      public T get() { return value; }
    }
    

    You can then use it in all situations where you want to a method to change a primitive:

    public static void increment(Cell<Integer> n) {
      n.set(n.get()++);
    }
    
    Cell<Integer> n = new Cell<Integer>(0);
    increment(n);
    increment(n);
    increment(n);
    System.out.println(n.get()); // Output: 3
    
    0 讨论(0)
  • 2020-12-09 03:13

    You could use an AtomicInteger from java.util.concurrent.atomic. It has a 'incrementAndGet' method which is suitable for your showcase.

    0 讨论(0)
  • 2020-12-09 03:15

    As DerMike mentioned you can achieve this as follows:

    public void doStuff() {
      AtomicInteger i = new AtomicInteger(0);
      increment(i);
      System.out.println(i);
     }
    
      public void increment(AtomicInteger i) {
        i.set(i.get() + 1);
     }
    
    0 讨论(0)
  • 2020-12-09 03:19

    You can't. Java is strictly pass-by-value.

    See http://javadude.com/articles/passbyvalue.htm

    Your choices are to wrap the integer in an object (or array), pass that in and update, return a value, or make the integer a class/instance variable.

    Which is better depends on the situation.

    0 讨论(0)
  • 2020-12-09 03:19

    See AtomicInteger: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicInteger.html

    You can do it thread-safe using this java.util.concurrent class.

    0 讨论(0)
提交回复
热议问题