I am playing around with code in one of the questions in Mala Gupta's excellent certification guides.
The question asks you to identify which method accepts three integers and returns their sum as a decimal number.
One of the correct options is listed below and I'm trying to find out why it's correct.
My hypothesis was:
* that the value in byte arg1 would be auto-converted to int
* then then the sum would be cast to double and returned
However the code return this error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
Java Result: 1
SomeClass.java:12: error: cannot find symbol
System.out.println(sc.numbers(1, 120, 1));
symbol: method numbers(int,int,int)
location: variable sc of type SomeClass
You are passing an integer, which is a compile‑time constant. Check carefully what you can do with compile‑time constants; I am pretty sure you can assign that 1 to a byte. But you cannot pass it as a byte to an argument; it is passed as an int, and there is no method which can accept that int.
* a variable of type byte can be assigned a whole number (provided it is in the range of byte)
* but, that whole number is passed to an argument as int and since the parameter expects a byte, an error results.
I recall that in a previous chapter Mala Gupta says that the default type of integer literals int.
So byte value = 1 would mean that:
* the type of value is byte
* but the type of '1' is int.
I have a suspicion its connected to my query.
The fog is almost cleared. Thank you for your help.
Thank you Henry for sharing that very illuminating article.
I think I see the central message: running un-compilable code in an IDE will report the errors in the order in which they are encountered during execution which may/may not be the the order that the compiler encounters them. And, since we should correct the first error during compile time and re-compile (and repeat if necessary), focusing on errors reported when running un-compilable code could be misleading - and I bet, cause further errors until you get a can of worms!
Norman, a slip of the fingers - the two methods indeed have different names (method signatures).
The multiple choice question (Question 3.3 Chapter 3 to be exact) in Mrs. Gupta's book asks the reader to select the correct methods that would return the sum of three integers as a decimal number.
A choice of four methods is given.
In order to test one of the methods, I had to define it in a class.