website counters

Thursday, October 6, 2016

Swapping(call by reference-2nd way)

/* Swapping-call by reference-2nd way */

class Swappingref{

    Integer x;
    Integer y;

    Swappingref(Integer x,Integer y){
        this.x=x;
        this.y=y;
    }

    void swapping(Swappingref obj){
        Integer temp=obj.x;
        obj.x=obj.y;
        obj.y=temp;
    }

    public static void main(String[]args){

        Integer x=new Integer(5);
        Integer y=new Integer(6);

        Swappingref object=new Swappingref(x,y);

        object.swapping(object);

        System.out.println("After swap:\n"+"X:"+object.x+" Y:"+object.y);
    }
}

No comments:

Post a Comment