Differences Between Void and Non-Void Methods in Java24 Jun 2025 | 4 min read Methods can be classified into void methods and non-void methods based on whether they return a value or not. Java MethodsA method in Java is a block of code that performs a specific task. Methods improve code reusability, modularity, and readability. Methods can be:
Void MethodsVoid methods are methods that do not return any value. It is primarily used to operate, such as displaying output, modifying a variable, or updating a database. Void methods are ideal when an action must be performed, but no data is needed after the method completes. Since it does not return a value, the method's return type is declared as void. Syntax: Advantages
Disadvantages
When to Use Non-Void Methods?
Example of Void MethodOutput: Hello, this is a void method! Explanation: The method displayMessage() performs a task (printing) but does not return anything. The void keyword in its declaration indicates it. In the main() method, the object obj is used to call the method, and the message is printed to the console. Non-Void MethodsA non-void method returns a value of a specific data type, such as int, double, String, or even an object. The return type is explicitly declared in the method signature, and the method must use a return statement to return a value. Non-void methods are used when a method is supposed to perform a calculation or fetch a result that must be used elsewhere in the program. Syntax: Advantages
Disadvantages
When to Use Void Methods?
Example of Non-Void MethodsOutput: Sum: 15 Explanation: The method add() takes two integers, calculates their sum, and uses a return statement to provide the result. The result can then be stored in a variable or used in expressions. Differences Between Void and Non-Void Methods
Void and Non-Void Methods MCQs1. What does a void method return?
Answer: c) Explanation: A void method in Java returns nothing. That's literally what void means-it indicates the absence of a return value. 2. Which of the following is a valid void method declaration?
Answer: b) Explanation: A valid void method declaration in Java includes the access modifier, the void return type, a method name, parentheses (with or without parameters), and a method body. 3. Which keyword is used to return a value from a non-void method?
Answer: c) Explanation: In a non-void method, return is used to send a value back to the caller. 4. Which method type allows you to assign its result to a variable?
Answer: b) Explanation: In Java, if a method has a return type (like int, String, boolean, etc.), you can assign its result to a variable. 5. What happens if a non-void method does not return a value?
Answer: b) Explanation: If a non-void method in Java does not return a value, the compiler throws an error, because it is expecting a return statement that matches the declared return type. |
In the ever-evolving landscape of software development, programming languages continually adapt to meet the demands of modern application development. Java, a language known for its robustness and cross-platform capabilities, took a significant leap forward with the release of Java 9. One of the notable improvements that Java...
4 min read
Eclipse is one of the most outstanding IDEs for Java and Android designers. There are many issues with Eclipse on the off chance that it isn't arranged as expected. You want to endeavour all of them individually. A few clients are kicking the Java Was started...
5 min read
The noneMatch() method in Java's Stream API is an essential function employed to evaluate whether none of the elements in a given stream satisfy a particular condition. It is particularly useful when we need to prove that none of the items in a collection match...
11 min read
At times we want the output of a program to be printed in a given specific format. In the C programming language this is possible using the printf( ) function. In this section, we will discuss the different output formatting. Let's discuss how we can format the...
4 min read
To determine the number of equal pairs in a string, locate all instances where the same characters appear in various locations inside the text. A pair is deemed "equal" if two characters are identical but appear at separate indices. The aim is to determine how many...
5 min read
The conversion of N-ary trees to binary tree serves as a standard computer science operation for reducing hierarchy complexity while maintaining hierarchical structures. An N-ary tree allows each node to have multiple children, making it complex to manage using standard tree structures. To efficiently represent an N-ary...
5 min read
The Java program used to get the size of a directory can calculate the total size of a directory and all the files and folders inside it using a recursive approach. By using this program, one can obtain valuable insights into the size of a directory. Algorithm Declare...
3 min read
In the world of Java programming, event-driven applications often rely on various types of events to handle user input, respond to system events, or perform other crucial tasks. Java provides a comprehensive event-handling framework that includes interfaces, classes, and methods to manage events effectively. One such...
5 min read
In this section, we will discuss what is method hiding in Java, method hiding factors (MHF), and the differences between method hiding and method overriding. Also, implement the method hiding concept in a Java program. To understand the method hiding concept in Java, first, we will understand...
3 min read
In object-oriented programming, a class is a blueprint or template for creating objects. Each object created from a class has its own set of attributes (data) and methods (functions) that define its behaviour. In some cases, we may only want one instance of a class to...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India