• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is 'void' seen as a return type  RSS feed

 
Shane Jensen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

In Mala Gupta's OCA Java SE 8 Programmer I Certification Guide, sample exam question 3.4 (page 214), it is asked whether the following statement is true:

'A method should accept at least one method argument or define its return type'

The answer is that this statement is false.

The first part of this statement is false, but I thought the second part is true. After all, a return type should always be defined for a method, right? Or is void not considered a return type? What is the reason behind this statement being false?

Thank you all in advance
 
Charles O'Leary
Ranch Hand
Posts: 175
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sybex (Chapter 5 Question 1 [Partial] Answer [to a larger question])

void ... it is a return type
 

You are absolutely correct.  Does that help?
 
Charles O'Leary
Ranch Hand
Posts: 175
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first part of this statement is false, but I thought the second part is true.
  These two fragments are not substitutes for each other.  At least, that's my "reading into" this statement.

After all, a return type should always be defined for a method, right? 
 
Shane Jensen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles O'Leary wrote:
The first part of this statement is false, but I thought the second part is true.
  These two fragments are not substitutes for each other.  At least, that's my "reading into" this statement.

After all, a return type should always be defined for a method, right? 

I know they're not substitutes. But if the first part is false, and the second part is true, then how can the statement as a whole be false?
 
Charles O'Leary
Ranch Hand
Posts: 175
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I don't have that book.  Could you please post the answer?  My interpretation is that the question appears to be asking if a valid method's signature will either have A or B.  Where A = ' accept at least one method argument' , while B = ' define its return type'.  Again, I don't own that book unfortunately, but that's my interpretation of the question.  So even IF they both (A & B) were true, constructing a valid method signature requires a lot of other rules to be followed.  Does that help?
 
Shane Jensen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles O'Leary wrote:Unfortunately, I don't have that book.  Could you please post the answer?  My interpretation is that the question appears to be asking if a valid method's signature will either have A or B.  Where A = ' accept at least one method argument' , while B = ' define its return type'.  Again, I don't own that book unfortunately, but that's my interpretation of the question.  So even IF they both (A & B) were true, constructing a valid method signature requires a lot of other rules to be followed.  Does that help?

All the book says about this question is "There's no constraint on the number of arguments that can be passed to a method, regardless of whether the method returns a value".

I agree with you, that's how I interpret the question as well. Which is why it's so strange to be that the statement is incorrect.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15890
82
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ultimate authority for such questions about the details of the language is the Java Language Specification.

Paragraph 8.4 is about method declarations, and paragraph 8.4.5 specifically about method results. The JLS says:
JLS 8.4.5 wrote:
The result of a method declaration either declares the type of value that the method returns (the return type), or uses the keyword void to indicate that the method does not return a value.

This sentence clearly says that there are two possibilities for the result:
  • the name of the return type
  • the keyword void

  • void is not a return type. Indeed, void is not a type at all, for example you cannot declare variables of the type void.

    So the second part of the statement is also false. A method is not required to have a return type; if it doesn't return anything, you use the keyword void (which is not a return type!) to indicate that.
     
    Charles O'Leary
    Ranch Hand
    Posts: 175
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:The ultimate authority for such questions about the details of the language is the Java Language Specification.

    Paragraph 8.4 is about method declarations, and paragraph 8.4.5 specifically about method results. The JLS says:
    JLS 8.4.5 wrote:
    The result of a method declaration either declares the type of value that the method returns (the return type), or uses the keyword void to indicate that the method does not return a value.

    This sentence clearly says that there are two possibilities for the result:
  • the name of the return type
  • the keyword void

  • void is not a return type. Indeed, void is not a type at all, for example you cannot declare variables of the type void.

    So the second part of the statement is also false. A method is not required to have a return type; if it doesn't return anything, you use the keyword void (which is not a return type!) to indicate that.


    Shane, I agree with Jesper 100%.  Hopefully, that doesn't add to any confusion that you/one may have.  When there is no return type for a method,  in the place where one would normally put the return type, one would instead put "void".  If I'm a new programmer looking at this, I'm thinking: HUH?  It's either a return type OR it is not  ... and this is just a little nuts.  As a forewarning,  "word problems" may sometimes have this "ambiguity".  Context is king.

    To button this up, it seems as if you were thinking of this as a Java keyword or, where only one side had to be true for the entire statement to be true.  While a second "interpretation" may be asking if there is a difference between half a dozen and 6 eggs.

    At the end of the day if nothing else,  I hope you walk away knowing that constructing a valid method signature is much more than a return type and an input parameter.  Many other Java rules must be adhered to.         
     
    Shane Jensen
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:The ultimate authority for such questions about the details of the language is the Java Language Specification.

    Paragraph 8.4 is about method declarations, and paragraph 8.4.5 specifically about method results. The JLS says:
    JLS 8.4.5 wrote:
    The result of a method declaration either declares the type of value that the method returns (the return type), or uses the keyword void to indicate that the method does not return a value.

    This sentence clearly says that there are two possibilities for the result:
  • the name of the return type
  • the keyword void

  • void is not a return type. Indeed, void is not a type at all, for example you cannot declare variables of the type void.

    So the second part of the statement is also false. A method is not required to have a return type; if it doesn't return anything, you use the keyword void (which is not a return type!) to indicate that.

    It makes sense to me now. Excellent advice as well, to consult the JLS.

    Thank you Jesper and Charles for your insights!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!