Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boone's mockexam question

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can't be (a) because the <CODE>int</CODE> 3 can't be converted to short automatically. The <CODE>long</CODE> 2L can be converted to double automatically, and so can the <CODE>int</CODE> 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 <CODE>int</CODE> into a <CODE>short</CODE>) require explicit casting because of the possibility of lost information/accuracy.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
<pre>
void test(double a, double b, short c) {
System.out.println("1");
}
t.test(1.0, 2L, 3);
</pre>
regds
maha anna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic