Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Return Types and the return Statement | Introduction to Methods
Quizzes & Challenges
Quizzes
Challenges
/
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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you show me the code for the `getYear()` method?

What does `public static` mean in this context?

Can you explain how the return value is used in the `main()` method?

bookReturn Types and the return Statement

Glissez pour afficher le menu

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3
some-alt