TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Tuesday, 7 April 2020

Recursion In Java | Java Recursion In Javapoint | Understanding Recursion In Java

                    Recursion in Java

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

Javapoint in recursion in java
Fig: Recursion in java

Syntax:

returntype methodname(){  
//code to be executed  
methodname();//calling same method  
}  

Example 1: Infinite times

    public class RecursionExample1 {  
     static void p(){  
     System.out.println("hello");  
     p();  
      }  
     public static void main(String[] args) {  
     p();  
     }  
     }     

    Output:

            hello
            hello
             ...
            java.lang.StackOverflowError

Example 2: Finite times


    public class RecursionExample2 {  
    static int count=0;  
    static void p(){  
    count++;  
    if(count<=5){  
    System.out.println("hello "+count);  
    p();  
    }  
    }  
    public static void main(String[] args) {  
    p();  
    }  
    }  
       

  Output:


hello 1
hello 2
hello 3
hello 4
hello 5

No comments:

Post a Comment