-2

Another common source of confusion is that you do not have to declare the types of arguments. The following is wrong!

int hour = 11;
int minute = 59;
printTime(int hour, int minute); // WRONG!

In this case, Java can tell the type of hour and minute by looking at their declarations. It is not necessary to include the type when you pass them as arguments. The correct syntax is

printTime(hour, minute)

Above is a quote from ThinkJava by Green Tea Press. I'm wondering:

  • Why does the author say "have to" and "not necessary"? I tried this code, and I had compile errors.
  • Is the author incorrect? Should he be saying "can't" and "impossible", respectively?
  • If the author is incorrect, please provide an example illustrating this point?

Strong note

Not looking at method declaration, but instead looking at method initialization, as in the example or as-in: printTime(int hour, int minute);.

Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
  • 2
    It's hardly "incorrect", it's *not* necessary, and you *don't* have to. Is the point of the question mere pedantry? – Dave Newton May 30 '14 at 23:16
  • 1
    In this case "can't" is just a stronger form of "do not have to" as is "impossible" a stronger form of "not necessary". The author isn't wrong, they are just not using statements that are as strong as they could be. – davmac May 30 '14 at 23:17
  • @DaveNewton I'm not asking if it's *not* necessary. I'm asking if it's possible, and if so, I'm seeking an example. – Wolfpack'08 May 30 '14 at 23:20
  • @davmac "Can't" carries a different meaning from "do not have to". I'm wondering if the sooner is more correct. – Wolfpack'08 May 30 '14 at 23:20
  • @Wolfpack'08 you both _can't_ and _do not have to_ declare the argument types in a method call expression. They are both equally correct (but _can't_ implies _do not have to_ and is a stronger restriction). Given that the text goes on to give an example which clearly shows that you should not declare the argument types, I really don't see what your issue is. – davmac May 31 '14 at 10:01

4 Answers4

1

In Java, printTime(int hour, int minute); will yield a syntax error.
This means it is impossible, unless you're using a compiler that allows you to do silly things like this.

Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
bstar55
  • 3,542
  • 3
  • 20
  • 24
  • This seems to be the most correct and concise answer. There's no situation in which `printTime(int hour, int minute); will not yield a syntax error, so the author's nuance was a bit off, basically. – Wolfpack'08 May 31 '14 at 00:45
1

I'm afraid you're misinterpreting what the author is saying:

int hour = 11;
int minute = 59;
printTime(int hour, int minute); // WRONG!

In Java, you do have to specify the type of each variable you use, but you only do so once - when you declare it initially, which is what is being done in the first two lines of the code above.

int hour = 11;
int minute = 59;
printTime(hour, minute); // RIGHT!

Thereafter, whenever Java encounters one of these variables, it looks back to its initial declaration to determine its type. This is done automatically: You do not have to re-specify a variable's type each time you use it - and indeed, as you've discovered, you cannot, as doing so results in a syntactical error that will prevent your code from compiling.

(Note that this paradigm holds true in all strongly typed languages, of which Java is one of many).

I wonder if you're confused after having seen things like this

void printTime(int hour, int minute){ 

}

which is correct. The key difference, of course, is that here we are looking at a method declaration: In this context, (int hour, int minute) is not analogous to the third line of the code above but rather to the first two lines: It is declaring two variables of type int named hour and minute in the context of that method.

What's different is that, in the code above, the variables are being declared and initialized in one step, but note that:

int hour; //variables DECLARED (given a name and type)
int minute; 
hour = 11; //variables INITIALIZED (set to an initial value) 
minute = 59;

is valid as well, and this is essentially what's happening in our method declaration: (int hour, int minute) declares two int variables named hour and minute, but they don't get initialized until the method is called, at which point they're set to the values passed in as parameters.

Update:

That being said, I'm merely speculating here about the source of your confusion, which is ultimately about how to invoke methods rather than with how to declare them.

printTime(int hour, int minute); // WRONG!
printTime(hour, minute); // RIGHT!

In your comment, and in your update, you referred to what is being done here as initializing a method, which I presume you're imagining as being similar to the way you initialize a variable - and I wonder if this might be at the root of your confusion:

You're not initializing the printTime( ) method here, you're invoking it - you're calling it. Methods do not get "initialized" in Java the way variables do, they get declared (see above) and then they get used, which is what we see happening here.

You're encouraged to use verbs for method names because methods do things - for instance, printTime (presumably) prints the time. Invoking a method is saying do that thing: You're not describing that thing, the way you describe a variable when you initialize it - that's what you do in your method declaration). When you invoke a method - when you call printTime() - you're saying do the thing - print the time. You're not initializing the method, you're using it.

A method invocation is signified by a set of parentheses at the end of it. Sometimes these parentheses are empty: For instance, one might expect a printTime() method to print the current time. Other times, we want methods to do things with things, and we give it those things by passing them in as parameters: printTime(int hour, int minute), will presumably print whatever time is signified by the hour and minute parameters passed into it: printTime(11,59) should print 11:59.

int hour = 11;
int minute = 59;
printTime(hour, minute); 

would do the same. Passing them in as parameters to methods is one of the many ways we use variables in Java. In order to use one in any of those ways, it must first be declared and given a type. Thereafter, it can be used in any of those ways without reiterating that type. Indeed, it must, because doing so will result in a syntax error every time.

(PS, I realize I veered a bit from answering your original question, but you inadvertently asked me another in your comment, one that I think is important)

Community
  • 1
  • 1
drew moore
  • 31,565
  • 17
  • 75
  • 112
  • What I'm seeing is, in the second code block, you're declaring a method. I'm specifically looking at method initiation, I think: when a method is used, not declared. See update. – Wolfpack'08 May 31 '14 at 00:39
  • If you don't mind, I could look through and edit it down, also. I'm a huge fan of the minimal information principal of memory. – Wolfpack'08 May 31 '14 at 02:23
  • Have at it :) Curious to know what seemed helpful – drew moore May 31 '14 at 02:41
  • All of the information is useful. I'd like to follow up by including an authentic error message at **double-declaration syntax error** (last section). – Wolfpack'08 May 31 '14 at 02:59
  • @Wolfpack'08 Well done. I'll go over your edits and accept when I have a few minutes. At a cursory glance, my only issue is with this term "double declaration" - you're inventing this term, and I'm all for inventing terms when they're helpful, but the helpfulness of this one isn't immediately apparent to me. In Java terms, a "double declaration" is simply gibberish, and my instinct is that the clearest thing we can do is simply call it that. Along those same lines, the "authentic error message" you'll get when you try to compile is simply "invalid method declaration" (read: "gibberish") – drew moore May 31 '14 at 03:09
  • @Wolfpack'08 Please let me know if you disagree, though. Like I said, that's just my intuitive feeling. – drew moore May 31 '14 at 03:10
  • I agree. For lack of a better term.... In fact, I thought it was a neologism (i.e., no term existed). Redeclaration, unnecessary declaraction, etc., could also be considered. I would assume someone writing a custom error message into a compiler would use "variable has already been declared". Anyway, I hope you'll improve rather than revert. Peace. – Wolfpack'08 May 31 '14 at 03:38
1

The author is correct in stating that you do not need to specify the data types when you invoke the method. Java understands that you have already declared the data types for the variables "hour" and "minute".

It can be confusing if you don't understand the difference between arguments and parameters. When creating a method, the data types need to be included with the parameters.

Derick
  • 11
  • 2
0

The example in the book is a description about how objects are always derived from a Class, so using an object in a method call carries along the Class "membership", so an explicit addition of the Class in the method call is syntactically wrong (and not needed in Java).

Note, however, that in the Class where the method is defined, the arguments DO need to have their Class membership denoted.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37