Author Topic:   Boone's mockexam question
Thorpe
unregistered
posted February 29, 2000 05:55 AM           
In Boone's mock exam he has given this qn.


What is the result of executing the following code


class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);

}
void test(double a, double b, short c) {

System.out.println("1");
}
void test(float a, byte b, byte c) {

System.out.println("2");
}
void test(double a, double b, double c) {

System.out.println("3");
}
void test(int a, long b, int c) {

System.out.println("4");
}
void test(long a, long b, long c) {

System.out.println("5");
}
}
a)1
b)2
c)3
d)4
e)5

His answer is c.
Why it cant be a?? Also I dont understand his explanation in the answer which says.

" The method with all double parameters is actually the only version of test() that the Java Virtual Machine can legally coerce the numbers to. The reason the other versions of test() are not invoked is that at least one of the parameters would have to be automatically coerced from a type with greater accuracy to a type with less accuracy, which is illegal. "


Can anybody help me??


Tony Alicea
sheriff
posted February 29, 2000 08:14 AM             
It can't be (a) because the int 3 can't be converted to short automatically. The long 2L can be converted to double automatically, and so can the int 3.

These "automatic" conversions are called widening conversions they are always OK and don't need a cast since no information is going to be lost by the conversion. The opposite, narrowing conversions, (as in (a), attempting to convert an int into a short) require explicit casting because of the possibility of lost information/accuracy.

vasha
unregistered
posted March 08, 2000 02:01 PM           
So why is this okay?

byte b = 12;

Isn't 12 an int? Why is it not necessary to do

byte b = (byte) 12;

maha anna
bartender
posted March 08, 2000 03:15 PM             
This is a special case called narrowing primitive conversion The rules for this are
1. The expression is a constant expression of type int.
2. The type of the variable is byte, short, or char.
3. The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

Here
byte b1 = 12; //ok because it satisfies all the 3 rules above
byte b2 = 1000; // Not ok because the 3rd rule is not satisfied.
regds
maha anna

vasha
unregistered
posted March 09, 2000 01:10 PM           
Makes sense, but back to the original question. Why isn't ans. a correct? It seems to follow all 3 rules. By the way I did try it and ans. a was not correct, but I don't see why not.

maha anna
bartender
posted March 09, 2000 01:27 PM             
Your qstn shows that you correctly UNDERSTOOD the concept. The above rule which I mentioned (automatic narrowing conversion) is applicable ONLY for assignment statements . For method invocation contexts it DOES NOT APPLY.. I am sorry that I should have mentioned this point in the previous post. So here the 3rd arg 3 is an int literal HAS TO BE casted to short in order to invoke this version of this method. I think I spoke to the point only. Any how I am happy that it MADE you to think..
 
void test(double a, double b, short c) {
System.out.println("1");
}
t.test(1.0, 2L, 3);

regds
maha anna

|