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);
    }
}

Swapping(Call by reference)

/*Swapping-call by reference */

class Swappingref{

    int x,y;

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

    void swapping(Swappingref obj){

        int temp=obj.x;
        obj.x=obj.y;
        obj.y=temp;
    }

    public static void main(String[]args){

        Swappingref object=new Swappingref(5,6);

        object.swapping(object);

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

Swapping(call by value)

/*Swapping-pass by value */


class Swapping{

    void swapping(int x,int y){

        int temp=x;
        x=y;
        y=temp;

        System.out.println("After swap by value: X:"+x+" y:"+y);
    }

    public static void main(String[] args){

        Swapping object=new Swapping();

        object.swapping(4,5);
    }
}

Sunday, October 2, 2016

objectTo Numeric Primitive type

/***object to numeric primitive type***/


class objectToNumeric{
    
    public static void main(String[] args){

        Integer object=new Integer(4);
        Double object2=new Double(10.4);

        System.out.println(object);

        float value=object.floatValue();

        System.out.println("float:"+value);

        int val=object.intValue();

        System.out.println("int:"+val);
        
        String str=new String("10");

        int x=Integer.parseInt(str);

        double d=Double.parseDouble(str);
        
        System.out.println("double to integer:"+object2.intValue());
        System.out.println("String to integer:"+x);
        System.out.println("String to double:"+d);
        System.out.println("Integer to string:"+object.toString());
    }
}

variable(2)

/*In a class there are three types of variable*/

class Variable{

    int x=0;//instance variable
    static int y=9;//static variable

    void function(){

        int var=1;//Local variable of function method
        System.out.println("Local variable of function method:"+var);
        
    }
        public static void main(String[]args){

            Variable obj1=new Variable();

            System.out.println("instance variable:"+obj1.x);
            System.out.println("static variable:"+Variable.y);
            System.out.println("static variable:"+y);

            obj1.function();
        }
}