Imagine that there is a black box that takes in an orange, makes juice out of it inside, and throws out orange juice. You can imagine another black box that takes in an orange, peels it inside and throws out a peeled orange. Now imagine a black box that takes in an orange, but doesn't throw anything out. Instead, once an orange is inserted, it presses some switches that turns on a TV inside the box, and the TV displays the orange. It could also juice the orange and display a glass of orange juice on the TV, or maybe a peeled orange (depends on what processing happens inside the box, just like the first two boxes), but you don't get anything out that you can use or store for later use. It just appears on the TV. Here, imagine the first black box as a function with float return type, the second as an integer return type, and the third one with a void return type. The first one processes and returns a float which you can add 1 to, subtract 5 from, or print, or just store in a variable as it is and never use it - but it's output is "tangible" to the programmer. Same for the int function. Finally, the void function also does some processing, and the processing includes calling another TV function that displays it, but it is not returned/thrown/collected anywhere. It is just used in some form and the output is visible but not tangible/storable to the programmer. Hope this helps. Answer from Major-Sense8864 on reddit.com
🌐
W3Schools
w3schools.com › java › ref_keyword_void.asp
Java void Keyword
close() delimiter() findInLine() findWithinHorizon() hasNext() hasNextBoolean() hasNextByte() hasNextDouble() hasNextFloat() hasNextInt() hasNextLine() hasNextLong() hasNextShort() locale() next() nextBoolean() nextByte() nextDouble() nextFloat() nextInt() nextLine() nextLong() nextShort() radix() reset() useDelimiter() useLocale() useRadix() Java File Methods Java FileInputStream Java FileOutputStream Java BufferedReader Java BufferedWriter Java Iterator Methods Java Collections Methods Java System Methods Java Errors & Exceptions · Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } }
🌐
DataCamp
datacamp.com › doc › java › void
void Keyword in Java: Usage & Examples
It is a return type that indicates the method performs an action but does not produce a result that can be used elsewhere in the code. The void keyword is typically used in method declarations.
🌐
Reddit
reddit.com › r/javahelp › void methods?
r/javahelp on Reddit: Void methods?
October 29, 2024 -

I’ve been trying to find explanations or videos for so long explaining void methods to me and I just don’t get it still. Everyone just says “they dont return any value” i already know that. I don’t know what that means tho? You can still print stuff with them and u can assign variables values with them i don’t get how they are any different from return methods and why they are needed?

Top answer
1 of 14
18
Imagine that there is a black box that takes in an orange, makes juice out of it inside, and throws out orange juice. You can imagine another black box that takes in an orange, peels it inside and throws out a peeled orange. Now imagine a black box that takes in an orange, but doesn't throw anything out. Instead, once an orange is inserted, it presses some switches that turns on a TV inside the box, and the TV displays the orange. It could also juice the orange and display a glass of orange juice on the TV, or maybe a peeled orange (depends on what processing happens inside the box, just like the first two boxes), but you don't get anything out that you can use or store for later use. It just appears on the TV. Here, imagine the first black box as a function with float return type, the second as an integer return type, and the third one with a void return type. The first one processes and returns a float which you can add 1 to, subtract 5 from, or print, or just store in a variable as it is and never use it - but it's output is "tangible" to the programmer. Same for the int function. Finally, the void function also does some processing, and the processing includes calling another TV function that displays it, but it is not returned/thrown/collected anywhere. It is just used in some form and the output is visible but not tangible/storable to the programmer. Hope this helps.
2 of 14
4
I see many answers here, but I don't think they are addressing the misunderstanding OP is having. OP basically says: "They do return a value, I see it on my screen, what even is a method that doesn't return anything, it doesn't do anything!". This is because returning a value is different from doing something outside the method. A return value is a specific action that the method does with the outside world, but specifically it's the only way the method has to directly say to whatever called it "Here is the value you requested to have back". A void method can do a lot of other things, can manipulate objects you give it, can print to screen, can modify values in the program, but those are not return values (even if they seem to return something to the user), return values are specifically what comes out the method in the line it is called. To give a crude analogy: Let's take the line: int i = nextNumberAfter(5) Parameters (in this case 5) are what you provide the function with, the result is what the function provides you (in this case, probably a 6 that you later put into i, kinda like: int i <-- 6 <-- nextNumberAfter <-- 5 You take the 5, give it to nextNumberAfter, which does something, results in 6, and you put it into i. Let's see an example of a void method: printNextNumber(5) You still have parameters you put into the method, but here the method does something else to the number (it probably adds one and print to screen). It doesn't give you (the one who called the number) anything. You can't take the 6 and put it into i anymore, because the 6 is long gone to the screen, not in your code anymore. If you want to visualize it, it may be something like: <-X-- printNextNumber <-- 5 . | . |--> 6 --> (to screen) So you see the 6 escaping, but nothing is going back to you, it's going another way and you can't have it.
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-void-and-non-void-methods-in-java
Difference Between Void and Non Void Methods in Java - GeeksforGeeks
September 22, 2023 - It is used to perform some action or operation but does not return any data. public void methodName(parameter1, parameter2, ...) { // body of the method // do some operations here // ... } ... // Java Program to demonstrate // Void Method in ...
🌐
SheCodes
shecodes.io › athena › 5279-what-is-void-in-java-and-what-is-it-used-for
[JavaScript] - What is Void in Java and What is it Used | SheCodes
The `void` keyword in Java is a reserved type used to specify that a method does not return any data type.
🌐
Baeldung
baeldung.com › home › java › core java › void type in java
Void Type in Java | Baeldung
January 8, 2024 - Since JDK 1.1, Java provides us with the Void type. Its purpose is simply to represent the void return type as a class and contain a Class<Void> public value.
Top answer
1 of 8
7

First, the multiply method does not return anything; it prints the product, but does not return any value.

public static void multiply(int x, int y)
    {
        int z = x*y;
        System.out.println(z); //may look like a return, but actually is a side-effect of the function.
    } //there is no return inside this block

Secondly, public static void main provides an entry point into your program. Without it, you cannot run your program. Refer to the Java documentation for more information on the usage of public static void main.

The String[] args here means that it captures the command line arguments and stores it as an array of strings (refer to the same link posted above, in the same section). This array is called args inside your main method (or whatever else you call it. Oracle cites argv as an alternate name)

System.out.print tells the program to print something to the console, while return is the result of the method. For example, if you added print all over your method to debug (a common practice), you are printing things while the program runs, but this does not affect what the program returns, or the result of the program.

Imagine a math problem - every step of the way you are "print"ing your work out onto the paper, but the result - the "answer" - is what you ultimately return.

2 of 8
2
  1. When a method does not return anything, you specify its return type as "void". Your multiply method is not returning anything. Its last line is a print statement, which simply prints the value of its arguments on the standard output. If the method ended with the line "return z", then you would not be able to compile the program with the "void" return type. You would need to change the method signature to public static int multiply(int x, int y).

  2. All Java programs do require the public static void main(String[] args) if they are to be executable. It is the starting point of any runnable Java program. Here's what it means:

a. public - the main method is callable from any class. main should always be public because it is the method called by the operating system.

b. static - the main method should be static, which means the operating system need not form an object of the class it belongs to. It can call it without making an object.

c. void - the main method does not return anything (although it may throw an Exception which is caught by the operating system)

d. String[] args - when you run the program, you can pass arguments from the command line. For example, if your program is called Run, you can execute the command java Run 3 4. In that case, the arguments would be passed to the program Run in the form of an array of Strings. You would have "3" in args[0] and "4" in args[1].

That said, you could have a Java program without a main, which will not be runnable.

I hope that helps.

🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Keywords › void
Java Programming/Keywords/void - Wikibooks, open books for an open world
void is a Java keyword. Used at method declaration and definition to specify that the method does not return any type, the method returns void. It is not a type and there is no void references/pointers as in C/C++. For example: See also: Java Programming/Keywords/return ·
Find elsewhere
🌐
CodeHS
codehs.com › textbook › apcsa_textbook › 2.3
2.3 Calling a Void Method
2.2 Creating and Storing Objects (Instantiation) 2.3 Calling a Void Method · Overview · Method Signature · Using Methods · Calling Methods · User Input and the Scanner Class · Area of a Rectangle · Program Flow · Using the Scanner Class · Increase/Decrease by 1 ·
🌐
California State University, Northridge
csun.edu › ~jmotil › csun2003 › RoutinesInJava.pdf pdf
1 Routines: Void Methods in Java
Type methods return some value; they do not do an action. Examples of void methods include output methods such as:
🌐
Trinket
books.trinket.io › thinkjava › chapter4.html
Void methods | Think Java | Trinket
Method names should begin with a lowercase letter and use “camel case”, which is a cute name for jammingWordsTogetherLikeThis. You can use any name you want for methods, except main or any of the Java keywords. newLine and main are public, which means they can be invoked from other classes. They are both static, but we can’t explain what that means yet. And they are both void, which means that they don’t yield a result (unlike the Math methods, for example).
🌐
Reddit
reddit.com › r/learnjava › im trying to understand the difference between void method and return method but just cant
r/learnjava on Reddit: im trying to understand the difference between void method and return method but just cant
July 24, 2023 -

im exprimenting right now

CODE:
public class returnStatements {

public static void main(String[] args) {

System.out.println(sub(10, 20));  
System.out.println(add(5,15));  

}

public static int add(int a, int b) {  
	  
	return a+b;  
	  
	  
}  
public static void sub(int c, int d) {  
	int subtract = c-d;  
	  
}  

}
why does the void method doesn't work but the return does?

Top answer
1 of 5
7
In your subtract method, you aren't returning anything, the only thing that method does is subtract two numbers and assigns it to a variable but doesn't do anything with it. The void part means you won't return something, which means you can't print it. You can change the void to int like the other method and return an int to get it working!
2 of 5
7
An analogy that I was told about return is this: you can send your friend who takes things very literally to the grocery store with some task. For example, you can ask them to find out if pineapples are in stock, or you can ask them to buy 2 peaches. Your friend will go do those things, but if they don’t return the answer/purchase to you, you wouldn’t know if pineapples are in stock and you wouldn’t have your peaches. You gotta tell your friend “text me to let me know if there are pineapples in stock” or add “buy and bring me 2 peaches”, then your friend will return to you and you can do something with that information (plan to include pineapples in your fruit salad, or eat the peaches). When you use void, the program still does what you want it to, just like your friend still goes to the grocery store to do those things for you, but it’s not going to be much use to you because you weren’t told the answer and you weren’t delivered the product. So if you need information, you must have a return. Of course there are void methods because you don’t always need to be told information. You can tell your friend “on your way home, please give this peach to Sarah.” You don’t need to get any information from that (assuming your very literal friend is as honest as a computer). You already know the result, you don’t need to be told any additional information. All you wanted is for Sarah to be given a peach. Setter methods are void because they are used to assign a value to a variable, a value you already know (method parameter) to a variable that you already know.
🌐
O'Reilly
oreilly.com › library › view › think-java › 9781491929551 › ch04.html
Void Methods - Think Java [Book]
Chapter 4. Void MethodsSo far we’ve only written short programs that have a single class and a single method (main). In this chapter, we’ll show you how to organize longer programs... - Selection from Think Java [Book]
🌐
Quora
quora.com › Whats-the-purpose-of-void-methods-in-java
What's the purpose of void methods in java? - Quora
Answer (1 of 3): [code ]void[/code] is not a data type. You cannot create [code ]void[/code] fields in a class. It is at best a relic of the past, namely, C. At worst, it is a very confusing keyword that does not reflect its current function. [code ]procedure[/code] would have been a better alter...
🌐
Greenteapress
greenteapress.com › thinkapjava › html › thinkjava005.html
Void methods
In the last chapter we had some problems dealing with numbers that were not integers. We worked around the problem by measuring percentages instead of fractions, but a more general solution is to use floating-point numbers, which can represent fractions as well as integers.