Java Tutorial 120 lessons · 2:32:01+ of video
Java Home What Java Is, Where Java Is Used and How Java Runs Your Code
What Java Is Where Java Is Used How Java Runs Your Code Installing the JDK, Trying Java in JShell and Your First Program
Installing the JDK Trying Java in JShell Your First Program The main Method, println and print, and Comments
The main Method println and print Comments Variables, Primitive Types, int and long
Variables Primitive Types int and long double and float, Arithmetic, Division and Remainder
double and float Arithmetic Division and Remainder char and boolean, Type Casting, Practice: A Simple Calculator
char and boolean Type Casting Practice: A Simple Calculator Strings, Joining Strings and String Methods
Strings Joining Strings String Methods Comparing Strings, Escapes and Text Blocks, String.format
Comparing Strings Escapes and Text Blocks String.format Reading Input with Scanner, Reading Numbers, Practice: A Receipt
Reading Input with Scanner Reading Numbers Practice: A Receipt Comparison Operators, if, else and else if
Comparison Operators if else and else if Logical Operators, The Ternary Operator and switch
Logical Operators The Ternary Operator switch Switch Expressions, Nested Decisions, Practice: Grade Checker
Switch Expressions Nested Decisions Practice: Grade Checker while Loops, do while and for Loops
while Loops do while for Loops break and continue, Nested Loops and Loop Patterns
break and continue Nested Loops Loop Patterns Counting and Summing, Infinite Loops and Off-by-One, Practice: Times Table
Counting and Summing Infinite Loops and Off-by-One Practice: Times Table What an Array Is, Creating Arrays and Looping Over Arrays
What an Array Is Creating Arrays Looping Over Arrays The Enhanced for Loop, The Arrays Class and Searching an Array
The Enhanced for Loop The Arrays Class Searching an Array Two-Dimensional Arrays, Common Array Mistakes, Practice: Top Scores
Two-Dimensional Arrays Common Array Mistakes Practice: Top Scores Defining Methods, Parameters and Return Values
Defining Methods Parameters Return Values Overloading, Scope and Pass by Value
Overloading Scope Pass by Value Recursion, Varargs, Practice: Temperature Converter
Recursion Varargs Practice: Temperature Converter Classes and Objects, Fields and Constructors
Classes and Objects Fields Constructors The this Keyword, Instance Methods and static
The this Keyword Instance Methods static Access Modifiers, Getters and Setters, and toString
Access Modifiers Getters and Setters toString Encapsulation, Inheritance and super
Encapsulation Inheritance super Overriding, Polymorphism and Abstract Classes
Overriding Polymorphism Abstract Classes Interfaces, Records and Enums
Interfaces Records Enums ArrayList, Generics and Wrapper Classes
ArrayList Generics Wrapper Classes and Autoboxing HashMap, Looping Over a Map and HashSet
HashMap Looping Over a Map HashSet Sorting with Comparator, Removing While Looping, Practice: Word Counter
Sorting with Comparator Removing While Looping Practice: Word Counter What Exceptions Are, try and catch, and finally
What Exceptions Are try and catch finally Checked and Unchecked, throw and Custom Exceptions, try-with-resources
Checked and Unchecked throw and Custom Exceptions try-with-resources Writing Files, Reading Files, Practice: Save and Load
Writing Files Reading Files Practice: Save and Load Lambdas, Functional Interfaces and Method References
Lambdas Functional Interfaces Method References Streams, filter, map and collect, More Stream Operations
Streams filter, map and collect More Stream Operations Optional, var and Pattern Matching
Optional var Pattern Matching StringBuilder, Dates and Times, Packages and Imports
StringBuilder Dates and Times Packages and Imports Project: The Plan, The Task Record and The TaskList Class
Project: The Plan Project: The Task Record Project: The TaskList Class Project: Saving Tasks, Loading Tasks and Reports with Streams
Project: Saving Tasks Project: Loading Tasks Project: Reports with Streams Project: The Menu, Where to Go Next and Course Recap
Project: The Menu Where to Go Next Course Recap Java · Lesson 61 of 120
Recursion A method that calls itself.
⏱ 2 min read ▶ Video at 1:16:02 🎓 Free
▶ starts at 1:16:02 A method can call itself. That's called recursion. It's a neat way to solve a problem, by solving a smaller version of the same problem.
Calls itself Called recursion Smaller and smaller Every recursive method needs two parts. A base case, where it stops, and returns without calling itself. And a recursive case, where it calls itself, with a smaller problem. Without a base case, it never stops.
key value Base case stop here Recursive case call yourself, smaller Each call gets closer to the base No base case crash
Countdown prints n, then calls itself with n minus one. When n reaches zero, the base case prints lift off, and returns. Factorial of five is five times four times three times two times one. That's five times the factorial of four. So factorial calls itself, until n is one. Run it. A countdown, then a hundred and twenty. And twenty factorial is huge, so we used long.
Main.java java Copy
public class Main {
static void countdown ( int n ) {
if (n == 0 ) {
System.out. println ( "Lift off!" );
return ;
}
System.out. print (n + " " );
countdown (n - 1 );
}
static long factorial ( int n ) {
if (n <= 1 ) return 1 ;
return n * factorial (n - 1 );
}
public static void main ( String [] args ) {
countdown ( 5 );
System.out. println ( "5! = " + factorial ( 5 ));
System.out. println ( "20! = " + factorial ( 20 ));
}
} Output
5 4 3 2 1 Lift off!
5! = 120
20! = 2432902008176640000 Prefer to watch? The full Java course is free All lessons in one video with animations, real code and chapters.
Watch free This method calls itself forever. Each call uses a little memory, on what's called the stack. Run it. Stack overflow error. The stack ran out of space.
Main.java java Copy
public class Main {
static int forever ( int n ) {
return forever (n + 1 );
}
public static void main ( String [] args ) {
forever ( 1 );
}
} Output
Exception in thread "main" java.lang.StackOverflowError
at Main.forever(Main.java:3) Recursion is a method calling itself, with a smaller problem each time. Always have a base case, where it stops. Next, methods that take any number of arguments.
Key takeaways Base case Recursive case StackOverflowError Next up:
Varargs — any number of arguments