I’m calling various methods and the parameters Integer values are all defaulting to int, even values such as 1 or 0, is there anything I should be doing to solve this, other than type casting? Here’s an example of the code
public class Launch {
public static void main(String[] args) {
CoordinateHandler handler = new Handler();
short x = 3901;
short y = 5921;
byte h = 2;
handler.toCoordinates(x, y, h, "modern");
}
}
public class CoordinateHandler extends CoordinateMain {
public void toCoordinates(short absX, short absY, byte height, String type) {
if (type.equals("modern")) {
super.toCoordinates(absX, absY, height);
} else {
coordinateUpdateEvent(absX, absY, height, type);
}
}
}
I would get a compile error saying possible loss of precision on the lines of short x, short y, and byte h, and I would also get a compile error saying toCoordinates(int, int, int, String) can not be applied to toCoordinates(short, short, byte, String)
It is not true that these lines
result in any compiler error; same goes for the subsequent method call. This is completely legal and correct Java. You are probably not showing the literal code that causes these problems for you, such as assigning from a variable or parameter and not from literal value, which is automatically cast into the type of the LHS.
If you are indeed assigning from an
intparameter, then the compiler is quite right: your assignment really may result in overflow.