can anyone explain the meaning of Void in java..even though i referred many books,not able to understand clear as i am a learner..

Recommended Answers

All 10 Replies

A method can return a value, in which case you specify the type of the returned value in the method header, eg
String convertToString() // returns a String

but some methods do not return anything, so where you would normally specify the return type, you use the keyword "void" to show that there is no return value, eg

void setDateOfBirth(Date d) // does not return anything

Or, if you meant the reference type Void, this might be a good read.

Void in java means that no value will be returned.
If your method is of type int, that method will return an integer value...
if your method is of type string, that method will return a string value...
but a void method will return nothing.

Let's put it in an example, I think a simple example of this is the setters.
Say you have a global variable String 'name', you would want to set it using setName("Alex"). You then create a method that sets that value

public void setName(String str){
    name = str;

The above code woudn't return a value but it will set at value to the variable name. I hope this helps.

21310051: Java is very case sensitive: void and Void are not the same.

We can't really provide a complete and correct answer until the OP states whether he meant void or Void

Well that's a point stultuske but the poster says what's the meaning of void in java, meaning its already inside java and since 'Void' could be a class which is still needed to be created, one must assume that the poster means 'void' and not 'Void'.

A class which is far from meaningless.
It's used heavily when you use reflection.

More formerly,
I would say; void defines a keyword that is used to explicitly indicate that a method of an object or class does not take in or bring out any data.To exemplify
this consider the method definition of move below:
public void move(void) - The first void tells us that move doesnt bring out any data(technically returns nothing). The second void says move does not take in any data(accepts no inputs).This statement is the same as public void move().
Remember whenever a method returns a value,this value may be used by another method,stored in a variable or it may be just a message to tell the OS that program execution was successfull ie the case of return 0.
Hope that this gives you some insight.

More formally:

public void move(void)

is not a valid Java method signature, so all this post adds to the previous answers is a mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.