This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I mean also the physical difference - like reserved space in the memory, consequences, what can we do with null that we cannot do with void and vice versa....
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35253
7
posted
0
Not sure what you mean by "what can we do with one that we can't do with the other", since the two keywords (and concepts) are not interchangeable.
Void means that a method does not return a result.
Null means that an object reference does not currently reference an object (in other words, it references nothing).
What do you think the similarities and differences are?
So the difference is that null is a reference to nothing and void is just nothing, not even the reference?
Do we reserve any memory for the null?
By "what can we do with one that we can't do with the other" I meant that i.e. we can put a null in the second argument of the assertions but we cannot put there void. But now I think that there are loads of such differences...
It is not really meaningful to speak about the difference between null and void, because they are two separate concepts:
- When a variable has the value null, it means that the variable isn't referring to any object. - A void method is a method that doesn't return a value.
So null is a specific value that a variable can have, and void indicates that a method has a certain property - two completely different things.
A variable takes up space in memory, no matter if it refers to an object or not.
By "what can we do with one that we can't do with the other" I meant that i.e. we can put a null in the second argument of the assertions but we cannot put there void. But now I think that there are loads of such differences...
The keyword void is only used to indicate that a method doesn't return a value. The keyword void doesn't make any sense anywhere else in Java source code. [ May 06, 2008: Message edited by: Jesper Young ]