Java Reference
In-Depth Information
Q I created two methods with the following signatures:
int total(int arg1, int arg2, int arg3) {...}
float total(int arg1, int arg2, int arg3) {...}
The Java compiler complains when I try to compile the class with these
method definitions, but their signatures are different. What have I done
wrong?
A Method overloading in Java works only if the parameter lists are different—either
in number or type of arguments. Return type is not part of a method signature, so
it's not considered when methods have been overloaded. Looking at it from the
point at which a method is called, this makes sense: If two methods have exactly
the same parameter list, how would Java know which one to call?
Q I wrote a program to take four arguments, but when I give it too few argu-
ments, it crashes with a runtime error. Why?
A Testing for the number and type of arguments your program expects is up to you in
your Java program; Java won't do it for you. If your program requires four argu-
ments, test that you have indeed been given four arguments by using the length
variable of an array and return an error message if you haven't.
Quiz
Review today's material by taking this three-question quiz.
Questions
1. If a local variable has the same name as an instance variable, how can you refer to
the instance variable in the scope of the local variable?
a. You can't; you should rename one of the variables.
b. Use the keyword this before the instance variable name.
c. Use the keyword super before the name.
2. Where are instance variables declared in a class?
a. Anywhere in the class
b. Outside all methods in the class
c. After the class declaration and above the first method
3. How can you send an argument to a program that includes a space character?
a. Surround it with quotes.
b. Separate the arguments with commas.
c. Separate the arguments with period characters.
 
Search WWH ::




Custom Search