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

Monday, June 27, 2016

String(uppercase to lowercase,replace)


package stringuppertolower;

public class StringUpperToLower {

    public static void main(String[] args) {
        
        String name="Abdul Mukit";
        System.out.println("Before change:"+name);
        
        String change=name.toLowerCase();
        System.out.println("After change:"+change);
        
        char c=name.charAt(1);
        System.out.println("CharAt:"+c);
        
        String replace=name.replace("Abdul Mukit","Mukit ahmed");
        System.out.println("replace name:"+replace);
    }
    
}

string(CompareTo and indexOf)


package stringcompare;

public class StringCompare {

    
    public static void main(String[] args) {
        String myName="Bangladesh";
        String YourName="Africa";
        char c='k';
        
        System.out.println("Index of k from myName:"+myName.indexOf(c));
        System.out.println("Index of k from yourName:"+YourName.indexOf(c));
        
        int result=myName.compareTo(YourName);
        
        if(result>0){
            
            System.out.println("greater:"+result);
        }
        else if(result<0){
            System.out.println("less:"+result);
        }
        else if(result==0){
            System.out.println("Equal:"+result);
        }
    }
    
}