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

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

提交回复
热议问题