Return 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
123456789101112131415package 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.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
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?
Awesome!
Completion rate improved to 8.33
Return Types and the return Statement
Deslize para mostrar o 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
123456789101112131415package 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.
Obrigado pelo seu feedback!