Questions tagged [void]

An incomplete type used as syntactic place-holder for the return type of a method/function when no value is returned.

Programming languages derived from C or Algol68, like C++, C#, Java, etc., may define the return type of methods/functions as void when the method/function does not return a value, but simply completes execution.

An example of its use in Java, compared to a non-void method, is:

int nonVoidMethod {
    // do something
    return 0; // return a value to the caller
}

void voidMethod {
    // do something
    return; // no value allowed to be returned from a void method
    // a "return" statement is not required
}

Although void is used as a type, it's an incomplete type:

  • In some languages, such as Java and Algol68, void is only a keyword used as a return type. It is not considered as a valid type in other language constructs.

  • Other languages, like C and C++, consistently define void as a type with an empty set of values. This allows its use for example in compound type constructs (void pointers). But no void objects can be instantiated.

1743 questions
84
votes
3 answers

Why cast free's return value to void?

I am reading a book (Programming with POSIX Threads by Butenhof, 1997) that uses C, and I came across the following line: (void)free(data); Here, data is just a pointer to an allocated struct, data = malloc(sizeof(my_struct_t)); Why is the result…
Adam Johnston
  • 1,370
  • 10
  • 20
84
votes
6 answers

Java generics void/Void types

I am implementing a ResponseHandler for the apache HttpClient package, like so: new ResponseHandler() { public int handleResponse(...) { // ... code ... return 0; } } but I'd like for the handleResponse function to…
Travis Webb
  • 13,507
  • 6
  • 51
  • 101
75
votes
4 answers

How do I mock a static method that returns void with PowerMock?

I have a few static util methods in my project, some of them just pass or throw an exception. There are a lot of examples out there on how to mock a static method that has a return type other than void. But how can I mock a static method that…
Pete
  • 9,930
  • 22
  • 87
  • 134
73
votes
7 answers

What does the return keyword do in a void method in Java?

I'm looking at a path finding tutorial and I noticed a return statement inside a void method (class PathTest, line 126): if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) { return; } I'm a novice at Java.…
Relequestual
  • 9,096
  • 6
  • 40
  • 74
70
votes
4 answers

What is the point of void operator in JavaScript?

I've seen some people using void operator in their code. I have also seen this in href attributes: javascript:void(0) which doesn't seem any better than javascript:; So, what is the justification of using the void operator?
mkoryak
  • 54,015
  • 59
  • 193
  • 252
68
votes
4 answers

Returning from a void function

Which is more correct way to return from function: void function() { // blah some code } OR void function() { // blah some code return; } Rationale for second way: It expresses developer intentions more clearly. It helps detecting function…
Agnius Vasiliauskas
  • 10,413
  • 5
  • 46
  • 66
68
votes
5 answers

EasyMock: Void Methods

I have a method that returns void in a class that is a dependency of the class I want to test. This class is huge and I'm only using this single method from it. I need to replace the implementation of this method for the test as I want it to do…
Iker Jimenez
  • 6,671
  • 8
  • 45
  • 45
68
votes
6 answers

is f(void) deprecated in modern C and C++

I'm currently refactoring/tidying up some old C code used in a C++ project, and regularly see functions such as: int f(void) which I would tend to write as: int f() Is there any reason not to replace (void) with () throughout the codebase in order…
SmacL
  • 21,621
  • 10
  • 89
  • 143
65
votes
6 answers

What is the difference between java.lang.Void and void?

In API "The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void." What is "uninstantiable" place holder class? When will java.lang.Void be used? If the class is…
user1357722
  • 5,598
  • 12
  • 32
  • 41
63
votes
4 answers

What's the difference between Void and no parameter?

I have a class which defines two overloaded methods public void handle(Void e) protected void handle() Obviously they are different, especially handle(Void e) is public. What's the difference between those two? How to call the first method? I…
Adam Lee
  • 21,598
  • 43
  • 138
  • 208
62
votes
2 answers

What's the point of const void?

Apparently, it is possible to declare a function returning const void: const void foo() { } g++ seems to consider the const important, because the following code does not compile: #include static_assert(std::is_same
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
61
votes
4 answers

What is the need of Void class in Java

I am not clear with the class java.lang.Void in Java. Can anybody elaborate in this with an example.
giri
  • 24,958
  • 61
  • 134
  • 175
61
votes
8 answers

Difference between int main() and int main(void)?

What does the following mean : int main(void) {...} VS int main() {...} ? I think that int main() {...} means that main doesn't receive any parameters (from command line) , however: int main(int argc, char *argv[]) does. But what does int…
JAN
  • 18,509
  • 49
  • 147
  • 268
59
votes
3 answers

Is void a data type in C?

Is void a data type in the C programming language? If so, what type of values can it store? If we have int, float, char, etc., to store values, why is void needed? And what is the range of void?
suhel
  • 623
  • 1
  • 6
  • 5
56
votes
14 answers

Why can't I explicitly return void from a method?

void run() { ... if (done) return cancel(); ... } where cancel() return void. This won't compile... and I can almost understand why. But if I want to return a void from a void, why not? Instead, I end up writing something like this: if…
Travis Webb
  • 13,507
  • 6
  • 51
  • 101