0% found this document useful (0 votes)
46 views4 pages

Recursion in Java (Module-1)

Recursion in Java involves a method calling itself continuously. This makes the code more compact but also more complex. The strictfp keyword ensures floating-point operations produce the same results across platforms by enforcing IEEE 754 floating-point arithmetic. It can be applied to classes, interfaces, and methods but not abstract methods, variables, or constructors. Call by value in Java means changes made to a parameter in a method do not affect the argument used in the calling method.

Uploaded by

Manish Sirka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Recursion in Java (Module-1)

Recursion in Java involves a method calling itself continuously. This makes the code more compact but also more complex. The strictfp keyword ensures floating-point operations produce the same results across platforms by enforcing IEEE 754 floating-point arithmetic. It can be applied to classes, interfaces, and methods but not abstract methods, variables, or constructors. Call by value in Java means changes made to a parameter in a method do not affect the argument used in the calling method.

Uploaded by

Manish Sirka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Recursion in Java(Module-1)

Recursion in java is a process in which a method calls itself continuously. A method in


java that calls itself is called recursive method.

It makes the code compact but complex to understand.

SYNTAX

1. returntype methodname(){  
2. //code to be executed  
3. methodname();//calling same method  
4. }  

Java Recursion Example 2: Finite times


1. public class RecursionExample2 {  
2. static int count=0;  
3. static void p(){  
4. count++;  
5. if(count<=5){  
6. System.out.println("hello "+count);  
7. p();  
8. }  
9. }  
10.public static void main(String[] args) {  
11.p();  
12.}  
13.}  

Output:

hello 1
hello 2
hello 3
hello 4
hello 5
Java Recursion Example 3: Factorial Number
Java Recursion Example 4: Fibonacci Series
Difference between object and class
Call by Value and Call by Reference in Java
There is only call by value in java, not call by reference. If we call a method passing
a value, it is known as call by value. The changes being done in the called method, is
not affected in the calling method.

Example of call by value in java


In case of call by value original value is not changed. Let's take a simple example:

1. class Operation{  
2.  int data=50;  
3.   
4.  void change(int data){  
5.  data=data+100;//changes will be in the local variable only  
6.  }  
7.      
8.  public static void main(String args[]){  
9.    Operation op=new Operation();  
10.  
11.   System.out.println("before change "+op.data);  
12.   op.change(500);  
13.   System.out.println("after change "+op.data);  
14.  
15. }  
16.}  
Output:before change 50

after change 50
Another Example
In case of call by reference original value is changed if we made changes in the called
method. If we pass object in place of any primitive value, original value will be
changed. In this example we are passing object as a value.

1. class Operation2{  
2.  int data=50;  
3.   
4.  void change(Operation2 op){  
5.  op.data=op.data+100;//changes will be in the instance variable  
6.  }  
7.      
8.     
9.  public static void main(String args[]){  
10.   Operation2 op=new Operation2();  
11.  
12.   System.out.println("before change "+op.data);  
13.   op.change(op);//passing object  
14.   System.out.println("after change "+op.data);  
15.  
16. }  
17.}  

Output:before change 50
after change 150

Java Strictfp Keyword


Java strictfp keyword ensures that you will get the same result on every platform if you
perform operations in the floating-point variable. The precision may differ from platform
to platform that is why java programming language have provided the strictfp keyword,
so that you get same result on every platform.

The strictfp keyword can be applied on methods, classes and interfaces.

1. strictfp class A{}//strictfp applied on class  
1. strictfp interface M{}//strictfp applied on interface  

1. class A{  
2. strictfp void m(){}//strictfp applied on method  
3. }  

Illegal code for strictfp keyword


The strictfp keyword cannot be applied on abstract methods, variables or constructors.

1. class B{  
2. strictfp abstract void m();//Illegal combination of modifiers  
3. }  
1. class B{  
2. strictfp int data=10;//modifier strictfp not allowed here  
3. }  
1. class B{  
2. strictfp B(){}//modifier strictfp not allowed here  
3. }  

You might also like