Not quite. Both variables in line 1 are bytes, It is only when you try arithmetic on them that there is promotion to an int. Both variables are promoted to ints. The reason you need () is that the cast operator has a higher precedence than +.
Brecht Geeraerts wrote:Hi Pablo,
When writing the following:
Only the variable a gets cast to byte, b will be an int.
No, it isn't OK to use bytes for arithmetic. A beginner shouldn't use bytes at all. They are there so you can create bytes and those arrays are sent across networks. Otherwise, stick to proper integer types: int and long.
. . . Is this okay? . . .
Apology accepted (). Remember that cert exam books include confusing code which nobody would use in real life. Part of the skill for passing a cert exam is knowing which variables are which type, and which code will and won't compile. We would have answered differently if this same question had come up on a cert exam forum rather than here on BJ.
Brecht Geeraerts wrote:. . . My apologies if it has caused any confusion.
as its result, so the last operation, which has the lowest precedence, is assignment which cannot proceed because the right type (int) cannot fit into the left type (byte).
the promoted type of its operands
Pablo Napoli wrote:... maybe now my doubt is related with how the compiler works, because I still don't fully understand why the compiler can't know the new value of the variable "c" (in case it has changed). I mean, with variable "b" it was able to read it knowing the value was too large. So in case value from "c" changes, it can check it again and if it doesn't fill into byte sum3, just in that case claims. I'm not sure if I'm being enough clear. Anyway with those explanations is enough to detect the right answers in my next oca exam, but I like going a bit deeper
Compilers are at risk of going into exponential complexity if they are asked to do too much, so who who knows how long it will take to compile a large program.
Knute Snortum wrote:. . . the compiler does the least amount of work to compile a valid Java program. It does this mainly for speed. . . . .
You are correct in how you say that expression is evaluated, remembering you go left‑to‑ight (or even left‑to‑right if I want to spell it correctly ).
Pablo Napoli wrote:
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
professionally read, modify and write PDF files from Javahttps://products.aspose.com/pdf/java