How to do call by reference in Java?

前端 未结 12 648
悲&欢浪女
悲&欢浪女 2021-01-30 17:36

Since Java doesnt support pointers, How is it possible to call a function by reference in Java like we do in C and C++??

12条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 18:12

    You cannot do call by reference in Java. Period. Nothing even comes close. And passing a reference by value is NOT the same as call by reference.

    I used an array to do this...

        package method;
    
    
        import java.util.Scanner;
    
        public class InterChange {
    
    
    
            public static void main(String[] args) {
    
            Scanner sc=new Scanner(System.in);
    
            int a[]=new int[2];
    
            System.out.println("Enter two values");
    
            for(int i=0;i<2;i++) {
    
                a[i]=sc.nextInt();
            }
    
    
    
    
            hange(a);
    
            for(int i=0;i<2;i++) {
    
                System.out.println(a[i]);
            }
    
    
        }
    
    
    
    
         static int hange(int b[])
        {
            int temp;
    
            temp=b[0];
            b[0]=b[1];
            b[1]=temp;
            return b[0]&b[1];
    
        }
    
        }
    

提交回复
热议问题