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.

1913 questions
99
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
  • 14,688
  • 7
  • 55
  • 109
97
votes
3 answers

Why does TypeScript have both `void` and `undefined`?

In TypeScript, you can annotate a function as returning void: function fn1(): void { // OK } function fn2(): void { // Error return 3; } You can also annotate a function to return undefined: function fn3(): undefined { // OK …
Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
92
votes
1 answer

Mockito - thenCallRealMethod() on void function

I have been running into a problem when trying to write a JUnit test case and am relatively new to Mockito. I have a function of a class that I am mocking, this function happens to be of a void return type. When calling this function from my mocked…
CRDamico
  • 923
  • 1
  • 6
  • 4
91
votes
5 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
  • 10,720
  • 25
  • 94
  • 139
87
votes
4 answers

Why does this Java 8 lambda fail to compile?

The following Java code fails to compile: @FunctionalInterface private interface BiConsumer { void accept(A a, B b); } private static void takeBiConsumer(BiConsumer bc) { } public static void main(String[] args) { …
Rag
  • 6,405
  • 2
  • 31
  • 38
86
votes
8 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
  • 11,631
  • 6
  • 47
  • 83
86
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
  • 57,086
  • 61
  • 201
  • 257
83
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,399
  • 2
  • 12
  • 23
81
votes
7 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
  • 7,088
  • 13
  • 34
  • 43
76
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,935
  • 5
  • 50
  • 70
72
votes
4 answers

What does casting to `void` really do?

An often used statement like (void)x; allows to suppress warnings about unused variable x. But if I try compiling the following, I get some results I don't quite understand: int main() { int x; (short)x; (void)x; (int)x; } Compiling…
Ruslan
  • 18,162
  • 8
  • 67
  • 136
70
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
  • 733
  • 1
  • 6
  • 5
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
  • 7,105
  • 9
  • 49
  • 46
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
  • 22,555
  • 12
  • 95
  • 149
67
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
  • 256,549
  • 94
  • 388
  • 662