aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method arguments ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method arguments ?" Watch "method arguments ?" New topic
Author

method arguments ?

rohit surve
Greenhorn

Joined: Mar 19, 2009
Posts: 17
This is from danchisholm mock questions .....

1.
class GFC216 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
char a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}

What is the result of attempting to compile and run the program?
a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compile-time error
f. Run-time error
g. None of the above

answer is float,float...how ? please explain



In this case (question 2)answer is compile error ? how?

2.

class GFC217 {
static String m(int i) {return "int";}
static String m(float i) {return "float";}
public static void main (String[] args) {
long a1 = 1; double b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}



What is the result of attempting to compile and run the program?
a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compile-time error
f. Run-time error
g. None of the above
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
1. the possible choices are float and double, widening chooses the next closest. float comes before double when your promoting variables.

byte->short->char->int->long->float->double

converting from left to right is called widening or promotion. It requires no explicit cast.



converting from right to left requires an explicit cast.



You can also widen to an object, but you have to box first. For example,





2.

The compile error is caused because there's no method that can accept the double primitive. Like I said widening goes from left to right. A double can't be widened.

Hope that helps my man.
rohit surve
Greenhorn

Joined: Mar 19, 2009
Posts: 17
thanks Ryan i got it !!! but say if the argument is a wrapper

Integer i= 2;...argument to be passed

and if the method parameter are of object type and Number...How to go for this ?which one should i go for ?
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
It would choose the closest super class. You could almost consider float a supertype of integer and a integer a supertype of char. I've heard authors refer to them in that way.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: method arguments ?