Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Return Types and the return Statement | Introduction to Methods
Mastering Methods in Java

bookReturn Types and the return Statement

Understanding Return Types and the return Statement

Every method in Java performs an action, and some methods also provide a result. The return type defines what kind of value a method gives back when it finishes running. If a method produces a value, you must specify its type, such as int, double, or String. If a method does not return any value, you use the special keyword void as its return type.

The return statement is used inside a method to send a value back to the place where the method was called. When the return statement runs, the method stops, and the specified value is sent back. If a method has a return type, you must use a return statement to provide a value of that type.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { // Method without parameters that returns an integer value public static int getYear() { return 2024; } public static void main(String[] args) { // Call the method and store the returned value int year = getYear(); // Print the returned value System.out.println("The current year is: " + year); } }

The method getYear() is a public static method that returns an integer value 2024. In main(), the method is called and its returned value is stored in the variable year.
Finally, System.out.println prints "The current year is: " followed by the value of year.

question mark

Which method declaration correctly returns an integer in Java?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookReturn Types and the return Statement

Veeg om het menu te tonen

Understanding Return Types and the return Statement

Every method in Java performs an action, and some methods also provide a result. The return type defines what kind of value a method gives back when it finishes running. If a method produces a value, you must specify its type, such as int, double, or String. If a method does not return any value, you use the special keyword void as its return type.

The return statement is used inside a method to send a value back to the place where the method was called. When the return statement runs, the method stops, and the specified value is sent back. If a method has a return type, you must use a return statement to provide a value of that type.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { // Method without parameters that returns an integer value public static int getYear() { return 2024; } public static void main(String[] args) { // Call the method and store the returned value int year = getYear(); // Print the returned value System.out.println("The current year is: " + year); } }

The method getYear() is a public static method that returns an integer value 2024. In main(), the method is called and its returned value is stored in the variable year.
Finally, System.out.println prints "The current year is: " followed by the value of year.

question mark

Which method declaration correctly returns an integer in Java?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
some-alt