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.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the difference between null and void? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "What is the difference between null and void?" Watch "What is the difference between null and void?" New topic
Author

What is the difference between null and void?

Ismael Upright
Ranch Hand

Joined: Feb 15, 2007
Posts: 166
Anyone knows?

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
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?


Android appsImageJ pluginsJava web charts
Ismael Upright
Ranch Hand

Joined: Feb 15, 2007
Posts: 166
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...
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference between null and void?
 
Similar Threads
Difference bet "" and null
difference between two type of null checks
ejbRemove and ejbPassivate
What is the difference between null and NULL in java?
question - methods - copy of a reference