This site requires JavaScript, please enable it in your browser!
Greenfoot back
keizerbob
keizerbob wrote ...

2016/3/7

Book 2nd edition p. 9 concept

keizerbob keizerbob

2016/3/7

#
The page 9 left margin Concept about a method's signature says that return type is part of it in addition to method name and parameters. I'm still climbing up the foothills of Java mountain, but I was under the impression that -- unlike C++ -- a Java method's signature does not include the return type.
danpost danpost

2016/3/7

#
keizerbob wrote...
I was under the impression that -- unlike C++ -- a Java method's signature does not include the return type.
Only class constructors do not include a return type. All methods, which does not include constructors, in java must have a return type. If no value is to be returned by the method its return type is declared to be 'void'; otherwise, the type of data returned must be declared. The java tutorials has this to say about methods and their return types.
keizerbob keizerbob

2016/3/8

#
I realize that non-constructor methods must have a return type (or 'void') specified. My comment was that there is an error in the 2nd edition of the book on page 9 left margin. A Java method's signature is formed only from the name and the input parameters. Unlike C++, the return type does not contribute to the method's signature.
danpost danpost

2016/3/8

#
Ah, I see. I read your post incorrectly. However, the statement about the signature on page 9 is correct (it is not an error). By not having the return type as part of the signature, you are limited in that you cannot have two methods with only the return type being different. This makes sense in that when calling a method, how else would the decision be made as to which method is being called if the only thing different was the return type..
keizerbob keizerbob

2016/3/9

#
I hate to be a wiseguy but here is what Wikipedia says at https://en.wikipedia.org/wiki/Type_signature (scroll down to "Method signatures") about method signatures:
Java In the Java programming language, a method signature is the method name and the number and type of its parameters. Return types and thrown exceptions are not considered to be a part of the method signature.
The Java Language Specification at http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.2 says:
8.4.2. Method Signature Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (ยง8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types.
As an experiment using leaves-and-wombats I created a non-void second version of setDirection(int direction) which returns a boolean. The Greenfoot compile fails with the message "method setDirection(int) is already defined in class Wombat". I'm just trying to be helpful for the next edition of the (really helpful) book.
danpost danpost

2016/3/9

#
Okay. I see what you are saying. The margin text does say that the return type is part of the method signature; when it is actually not part of it. The main text along side it also implies that the return type is part of the method signature. It is given as part of the method description in the object menus provided by greenfoot; but, again is not actually part of the method signature. So, yes -- a correction in the book should be recommended.
You need to login to post a reply.