Question:
java return type confusing?
anonymous
2011-07-23 22:42:19 UTC
// do these 2 method means same thing?one has void and return and the other one doesnt have void and return?and if it doesnt return something(which means void)does it matter i write return or i dont?
void someM()
{

return;
}
///////////////////////////////////////////////////////////////////////
someM()
{

}
Four answers:
green meklar
2011-07-24 09:16:12 UTC
If you don't specify the return type, and the method is not a constructor, then the compiler probably won't let you compile it. It requires that you specify a return type.



Moreover, if the return type is anything other than void, you must make sure that all paths through the procedure either return something of that type, or throw an exception. It is only with void methods and constructors that you get to omit the return statement completely.
Here we go
2011-07-24 05:53:19 UTC
Void means you cannot return values.

So it is wrong.

Where as method a method can return values.

Correct answer is:

Void SomeM()

{

}

SomeM

{

return;

}
MANOJ
2011-07-24 07:40:10 UTC
a method cannot be declared without return type. a method could have a return type (void is also a return type). Only constructors can be declared without return type and constructor must have same name as the Class.



class Abc(){



Abc(){

//constructor

}



public void Abc(){

//this is a method not constructor but same name as class

}



}
Blackcompe
2011-07-24 06:22:58 UTC
The first method is correct. "return;" means return void. The second method won't compile because it doesn't specify a return type. Note that when void is the return type, the method doesn't have to have a return statement.



//correct

void foo(){return;}

void foo(){}



//incorrect

void foo(){return void;}

foo(){}

foo(){return;}

foo(){return;}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...