In object oriented programming language,sometimes the method or we can say function will need to refer to the object that invoked it.To do so there’s this keyword in java.this can be used within any method that’s currently refer to the working object.

You will get better idea about however it works after examine below code

rectangle(int width,int length)
{
this.width=width;
this.length=length;
}

Here you can see that the value of actual parameters are initialized with help of formal parameters in function body.In java it’s strictly prohibited to declare different variables having same name in one scope.So there’s concept of global and formal parameters.

In function body,local variable overlaps global variable.If you still confused, then you should execute below program.

CODE

import java.util.Scanner;
class temp{
int a,b,c;

temp(int a,int b,int c)
{
this.a=a;
this.b=b;
this.c=c;
}
void display()
{
System.out.println(“a= “+a+” b= “+b+” c= “+c);
}
}
class initial{
public static void main(String args[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println(“Enter three numbers”);
x=s.nextInt();
y=s.nextInt();
z=s.nextInt();
temp t=new temp(x,y,z);
t.display();
}
}

OUTPUT

Enter three numbers
1
2
3
a=1 b=2 c=3

LOGIC

Here we have used java-scanner file to import three numbers from user.Then with the help of this keyword we have initialized actual variable of class.After it we have called display function to check that variables are initialized or not and it works well!

Recursion

You know recursion? Recursion is defined as function calls itself.To avoid infinite  loop situation we’ve to mention condition that become true at one stage of recursive function .So its necessary that every recursive function use correct conditions to avoid infinite loop.For recursive function we can say that there must be one function call statement in function body through which calls itself.It’s great way to use this feature instead of writing long block of code..

function
{
function body//
.
.
.
function call//
}
We can see that at the end of body function calls itself.I think from this syntax you get the exact idea about recursion.If however unsure then visit the next code and execute it in your system.

CODE

import java.util.Scanner;
class temp{
int a;
int sum=1;
void cal(int a)
{
if(a==0)
{
System.out.println(“Factorial =”+sum);
}
else
{
sum=sum*a;
a–;
cal(a);
}
}
}
class rec{
public static void main(String args[])
{
int k;
Scanner s=new Scanner(System.in);
System.out.println(“Enter the number to find factorial”);
k=s.nextInt();
temp t=new temp();
t.cal(k);
}
}

OUTPUT

Enter the number to find factorial
5
Factorial=120

LOGIC

We have firstly imported java library file.Then in temp class we have made one method cal that have one parameter.Inside recursive function there is if-else statement.Here if have condition that a==0 and else will execute until if condition become true.In else part we have simply multiply value of a with sum where sum is initialized firstly to zero,and stored its value inside sum variable. Now we have decreased value of a and at end calls the cal function again.So it become loop of code and will continuously execute until a==0.

I hope you coders get the exact idea concerning this keyword and formula in java.If you find problem regarding this then comment below.

By jigar

Leave a Reply

Your email address will not be published. Required fields are marked *