I am having a hard time understanding some code that shows an example how a double in Java could be transformed into a byte[] and vice versa.
Here is the code being
This is due to how operator precedence and associativity works in Java.1
Unfortunately, the Oracle Java Tutorial provides only a partial overview, and the Java Language Specification is not of great help either, as it mostly leaves the exercise of figuring out operator precedence to the reader by stating:
Precedence among operators is managed by a hierarchy of grammar productions.
In general, expressions are evaluated from left to right. In terms of operator precedence, the following table2 applies:
╔═══════╦══════════════╦══════════════════════╦═════════════════╗
║ Level ║ Operator ║ Description ║ Associativity ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 16 ║ [] ║ access array element ║ left to right ║
║ ║ . ║ access object member ║ ║
║ ║ () ║ parentheses ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 15 ║ ++ ║ unary post-increment ║ not associative ║
║ ║ -- ║ unary post-decrement ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 14 ║ ++ ║ unary pre-increment ║ right to left ║
║ ║ -- ║ unary pre-decrement ║ ║
║ ║ + ║ unary plus ║ ║
║ ║ - ║ unary minus ║ ║
║ ║ ! ║ unary logical NOT ║ ║
║ ║ ~ ║ unary bitwise NOT ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 13 ║ () ║ cast ║ right to left ║
║ ║ new ║ object creation ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 12 ║ * ║ multiplicative ║ left to right ║
║ ║ / ║ ║ ║
║ ║ % ║ ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 11 ║ + - ║ additive ║ left to right ║
║ ║ + ║ string concatenation ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 10 ║ << >> ║ shift ║ left to right ║
║ ║ >>> ║ ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 9 ║ < <= ║ relational ║ not associative ║
║ ║ > >= ║ ║ ║
║ ║ instanceof ║ ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 8 ║ == ║ equality ║ left to right ║
║ ║ != ║ ║ ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 7 ║ & ║ bitwise AND ║ left to right ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 6 ║ ^ ║ bitwise XOR ║ left to right ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 5 ║ | ║ bitwise OR ║ left to right ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 4 ║ && ║ logical AND ║ left to right ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 3 ║ || ║ logical OR ║ left to right ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 2 ║ ?: ║ ternary ║ right to left ║
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
║ 1 ║ = += -= ║ assignment ║ right to left ║
║ ║ *= /= %= ║ ║ ║
║ ║ &= ^= |= ║ ║ ║
║ ║ <<= >>= >>>= ║ ║ ║
╚═══════╩══════════════╩══════════════════════╩═════════════════╝
For your specific question, this means that no extra parentheses need to be placed around the cast operation, as the precedence of the cast operator ()
is higher than that of the bitwise AND &
operator (level 13 vs. level 7).
1 I wrote this up as a canonical answer to address questions about operator precedence and associativity in Java. I found a lot of existing answers that gave partial information, but I couldn't find one that gave an overview of the complete precedence and associativity table.
2 Operator precedence and associativiy table reproduced from https://introcs.cs.princeton.edu/java/11precedence/.