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

后端 未结 10 535
攒了一身酷
攒了一身酷 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:24

    As Jon Skeet mentioned, all methods in Java are pass-by-value, not pass-by-reference. Since reference types are passed by value, what you have inside the function body is a copy of the reference - this copy and the original reference both point to the same value.

    However, if you reassign the copy inside the function body, it will have no effect on the rest of your program as that copy will go out of scope when the function exits.

    But consider that you don't really need pass-by-reference to do what you want to do. For instance, you don't need a method to increment an Integer - you can just increment it at the point in your code where it needs to be incremented.

    For more complex types, like setting some property on an Object, the Object you are working with is likely mutable; in that case a copy of the reference works just fine, since the automatic dereference will get you to the original object whose setter you are calling.

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

    Integer is a value object which means that is value can't change.

    Note that n++ is the equivalent of n = n + 1. You're just changing the value referred to by the local variable n, not the value of n itself.

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

    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.

    0 讨论(0)
  • 2020-12-09 03:28
    public class passByReferenceExample {
        static void incrementIt(Long b[]){
            Long c = b[0];
            c=c+1;
            b[0]=c;
    
        }
        public static void main(String[] args) {
            Long a[]={1L};  
    
            System.out.println("Before calling IncrementIt() : " + a[0]);
            System.out.println();
            incrementIt(a);
            System.out.println("after first call to incrementIt() : " + a[0]);
            incrementIt(a);
            System.out.println("after second call to incrementIt(): "+ a[0]);
            incrementIt(a);
            System.out.println("after third call to incrementIt() : "+ a[0]);
            incrementIt(a);
            System.out.println("after fourth  call to incrementIt(): "+ a[0]);    
        }
    }
    

    output:

    Before calling IncrementIt() : 1
    
    after first call to incrementIt() : 2
    after second call to incrementIt(): 3
    after third call to incrementIt() : 4
    after fourth  call to incrementIt(): 5
    
    0 讨论(0)
提交回复
热议问题