Java Reference
In-Depth Information
Now consider the following statements and try to determine which convert method is
invoked at runtime:
byte b = -41;
System.out.println(convert(b));
The compiler looks for a convert method with a byte parameter. Because one doesn't
exist, it looks for a convert method with a compatible parameter that a byte can be
promoted to, starting with the smallest promotion, which in this example is a short .
Therefore, the convert method on line 15 is invoked when a byte is the argument. The
output of the previous two lines of code is “ short ”.
Let's look at an example. The following Email class has four overloaded send methods.
Study the code carefully and try to determine its output.
1. public class Email {
2. public void send(float f) {
3. System.out.println(“float parameter”);
4. }
5.
6. public void send(Object x) {
7. System.out.println(“Object parameter”);
8. }
9.
10. public void send(String s) {
11. System.out.println(“String parameter”);
12. }
13.
14. public void send(int id) {
15. System.out.println(“int parameter”);
16.
17. }
18.
19. public static void main(String [] args) {
20. Email email = new Email();
21. email.send(12.5);
22. email.send(123456);
23. email.send(new String(“Hello”));
24. email.send(new java.util.Date());
25. }
26. }
Search WWH ::




Custom Search