Use of this keyword in java

This keyword in java:

  • The this keyword is used to refers the current object in a method or constructor.
  • The most common use of the this keyword is used to eliminate the confusion between the same name of variables used in different methods (constructor parameter).
  • Mainly used for avoiding conflicts from methods or variables or passing parameters.

Example program without using this keyword:

  1. public class Item_Details

    {

        String item_name;

        int qty;

        float price;

        Item_Details(String x, float y, int z)

        {

            //Assigning parameters to global variable

            item_name=x;

            price=y;

            qty=z;

        }

        void display()

        {

            double amt=price*qty;

            System.out.println("Item name\t Unit price\t Qty \t Payment");

            System.out.println(item_name + "\t\t " + price + "\t\t " + qty+ "\t " +amt);

        }

    }

    class Without_this_keyword {

        public static void main(String args[]) {

            Item_Details obj1 = new Item_Details("Pen",5,2);

            obj1.display();

            Item_Details obj2 = new Item_Details("Eraser",2,3);

            obj2.display();

        }

     

    }

Output:

Assigning same parameter in global variable and it output become null:

  1. public class Item_Details

    {

        String item_name;

        int qty;

        float price;

        Item_Details(String item_name, int qty, float price)

        {

           //Assigning same parameter in global variable

            item_name=item_name;

            price=price;

            qty=qty;

        }

        void display()

        {

            double amt=price*qty;

            System.out.println("Item name\t Unit price\t Qty \t Payment");

            System.out.println(item_name + "\t\t " + price + "\t\t " + qty+ "\t " +amt);

        }

    }

    class Without_this_keyword {

        public static void main(String args[]) {

            Item_Details obj1 = new Item_Details("Pen",5,2);

            obj1.display();

            Item_Details obj2 = new Item_Details("Eraser",2,3);

            obj2.display();

        }

     

    }

Output:

Using this keyword:

Overcome above problem for using this keyword.

Example program with this keyword:

  1. public class Item_Details

    {

        String item_name;

        int qty;

        float price;

        Item_Details(String item_name, int qty, float price)

        {

            this.item_name=item_name;

            this.qty =qty;

            this.price=price;  

        }

        void display()

        {

            double amt=price*qty;

            System.out.println("Item name\t Unit price\t Qty \t Payment");

            System.out.println(item_name + "\t\t " + price + "\t\t " + qty+ "\t " +amt);

        }

    }

    class With_this_keyword {

        public static void main(String args[]) {

            Item_Details obj1 = new Item_Details("Pen",5,2);

            obj1.display();

            Item_Details obj2 = new Item_Details("Eraser",2,3);

            obj2.display();

        }

     

    }

Output:

Another method to overcome above problem without using this keyword:

  1. public class Item_Details

    {

        String item_name;

        int qty;

        float price;

        Item_Details(String item_name, int qty, float price)

        {

            double amt=price*qty;

            System.out.println("Item name\t Unit price\t Qty \t Payment");

            System.out.println(item_name + "\t\t " + price + "\t\t " + qty+ "\t " +amt);

        }

    }

    class Without_this_keyword {

        public static void main(String args[]) {

            Item_Details obj1 = new Item_Details("Pen",5,2);

            Item_Details obj2 = new Item_Details("Eraser",2,3);

        }

     

    }

Output:
Share:

Parameters or arguments in java

 Parameters:

  • It provides data communication between the calling function and the called function.
  • There are two types of parameters,
    (i). Actual parameters:
  • Used in data transferred from the main function to the user-defined function.
    (ii). Formal parameters:
  • Used in data transferred from a user-defined function into main function.
Local and global variables:
Local variable:
  • The local variables are defined within the body of the function.
  • This variable used within the function only, other functions can not access these variables.
Example:

int a=10,b=5;
if(a>b)
{
          int c=a+b;
}
else
{
        int c=a-b;
}

Global variable:
  • The global variables are defined outside the function or conditions ,these variables are only used in this particular part.
Example:

int x=5,y=10;
int z;
if(x<y)
{
         z=x+y;
}
else
{
         z=x-y;
}

Example program for local and global variables.

public class Parameters {

    int p=10,q=10;//Global variable

    int add(int x,int y)//Formal argument

    {

        return x+y;

    }

    void sub()

    {

        int r=p-q;//Local variable is r

        System.out.println("Subtraction value: "+r);

    }

    void mul()

    {

        int r=p*q;//Local variable is r

        System.out.println("Multiplications value: "+r);

    }

    public static void main (String args[])

    {

        int a=10,b=5;

        Parameters p=new Parameters();

        System.out.print("Result of a add b value is : " +p.add(a, b)+"\n");//Actual 

                                                                                                                 argument

        p.sub();

        p.mul();

       

    }

 

}

Output:
Share:

Parameter passing method and Recursion function in java

 Parameter passing methods:

In ‘Java’ language there are two ways that the parameters can be passed to a function, they are

i). Call by value

ii).Call by reference

1.  Call by value:

  • This method copies the values of actual parameters into the formal parameters of the function.
  • Here, the changes in the formal parameters cannot affect the actual parameters, because formal arguments are a photocopy of actual arguments.
  • Note: If no idea about actual and formal arguments means the below link to use for learning this topic.

Example program:

  1. public class Call_by_value {

        void display() {

            int n = 2;

            System.out.println("The " + n + " of cubic value is " + cube_func(n));//function calling

        }

        int cube_func(int x) {

            x = x * x * x;

            return (x);

        }

        public static void main(String args[]) {

            Call_by_value c = new Call_by_value();

            c.display();

        }

    }

Output:

2. Call by reference:

  • It is another way of passing parameters to the function.
  • Here, the address of arguments is copied into the parameters inside the function, the address is used to access the actual arguments used in the call.
  • Hence changes made in the arguments are permanent.
  • Here pointers are passed to function, just like any other arguments and we need not declare the parameters as a pointer type.

Example program:

  1. public class Call_by_reference {

        void display() {

            int a = 5, b = 7;

            interchange_func(a, b);//pass address to the function 

        }

        void interchange_func(int x, int y) {

            System.out.println("a and b values before interchanging : " + x + " " + y);

            int t;

            t = x;

            x = y;

            y = t;

            System.out.println("a and b values after interchanging : " + x + " " + y);

        }

        public static void main(String args[]) {

            Call_by_reference r = new Call_by_reference();

            r.display();

        }

    }

Output:

Recursion:

  • It is the process being performed where one of the instructions is to “repeat the process”.
  • This makes it sound very similar to a loop because it repeats the same code, and in some ways, it is similar to looping.
  • It is the process of calling the same function itself again and again until some condition is satisfied. This process is used for repetitive computation in which each action is satisfied in terms of a previous result.

Example program:

  1. import java.util.Scanner;

    public class Recursion_Demo {

        int a;

        void get_data() {

            Scanner sc = new Scanner(System.in);

            System.out.print("Enter the factorial number: ");

            a = sc.nextInt();

        }

        int recursive_func(int x)// function declaration

        {

            int f;

            if (x == 1) {

                return (1);

            } else {

                f = x * recursive_func(x - 1);// recursive function

            }

            return (f);

        }

        void display() {

            System.out.print("The factorial of  " + a + "!  = " + recursive_func(a)+"\n");// function call

        }

        public static void main(String args[]) {

            Recursion_Demo r = new Recursion_Demo();

            r.get_data();

            r.display();

        }

    }

Output:

Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me