Return Keywords
Start course
3h 15m

In this course, we look at control flow statements in the Java programming language. 

Learning Objectives

  • Decision-making statements
  • Looping statements
  • Branching statements 

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge

Hi there, in this video, we'll talk about the return keyword. The return statement is used to exit from the current method and control flow returns to where the method was invoked. Let's make some examples with the return statement. In the exercise project, right-click on the branching package and select 'New,' 'Class.' Specify the class name as ReturnStatement and select the checkbox for the main method. First, we'll use the return statement in the switch case statement. Let's declare a variable k and its initial value will be one. The conditional switch expression is variable k. We have two case values. 

First, is 1, if the value of the variable is 1, then we'll print 1 to the console. We add the break statement. Let's copy the code and change the case value. And if the case value is 2, then we will print 2 to the console. We add the return statement. Finally, we add the print method after the switch case statement to test the statement. Let's run the code. As you can see, the print method in the first case and the print method outside the switch statement are executed and messages are displayed, because break is used to exit a switch statement. So, the second print method is executed. 

Let's change the value of variable k with 2. Let's run the code. As you can see, only the print method inside the second case is executed. But this time the print method outside the switch statement is not executed, because the return statement is used to exit the current method. The current method for this code is the main method. Since the second print method is inside the main method, it is not executed. Okay, let's try to use the return statement anywhere. Let's declare a variable bool with boolean type, its initial value is true. In the print method, we write Before the return. 

Now, I will create the if statement. If bool, means if the condition is true, then we add the return statement, and we add the second print method that includes "This statement will not be executed." Let's run the code. As you can see, only the first method is executed and the 'Before the return' message is displayed, because the compiler ignores every statement after return in the same method. Now, let's do another example with for each loop. I'll convert these codes to the comment line. First, I'll create a string array. The name of this array can be animals and I will initialize this array. 

The first element of this array can be bird, the second element can be cat, the third element can be dog, the fourth can be bear, the fifth can be bee. And now, let's create the for each loop, for(String animal : animals). And I'll create an if statement in the for each loop, if(animal == "Bear"), I will use the return keyword and after the if statement, I'll print the animal object to the console. Also, I'll add the second print method that includes 'This statement will not be executed' outside the for-each loop. Before running the code, let's examine it. The string array has five elements. With the for each loop, we access the elements of the array in order. In other words, the bird element will be transferred to the animal variable in the first iteration, cat in the second iteration, dog in the third. In this way, the loop will continue. 

Of course, we checked the value passed to the animal object with the if statement in each loop. Whenever the value passed to the animal object is bear, this time, the if condition will be satisfied and the compiler will enter in the if block. Since we are using the return keyword here, the method will terminate at this stage and the compiler will go out of the method. Therefore, Bird, Cat, and Dog will be printed on the console screen, but the Bear, Bee, and the last print method will not execute. Let's run it and test it. As you can see, Bird, Cat, and Dog were printed on the console screen. Also, I'd like to point out that if you remember, we used the label with the break and continue keywords, but we cannot use label with the return keyword. 

For example, let's put a label at the beginning of the for each loop. The label name can be animalsLoop. Now, let's write the name of the label, that is, the animalsLoop after the return statement. As you can see, we are getting a compilation error. It says, "AnimalsLoop cannot be resolved to a variable." You'll understand this better in the method section. Yes, so we have completed branching or jump statements. Let's take a short break here. See you in the next lesson.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics