File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method arguments ?

 
rohit surve
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic