-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathBinaryArithmetic.java
More file actions
38 lines (27 loc) · 1.04 KB
/
BinaryArithmetic.java
File metadata and controls
38 lines (27 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package oca.chapter.two;
public class BinaryArithmetic {
public static void main(String[] args) {
// Numeric promotion rules examples, this rules applies to any binary arithmetic operations (+, -, /, %).
short a = 1;
short b = 2;
/* Smaller data types (byte, short and char) are promoted to int,
/ so the result of a + b is a int value.
*/
int c = a + b;
System.out.println(c);
short x = 14;
float y = 14;
double z = 4;
/* Promotes x to int, then promotes x to float and multiplies x and y,
/ then promotes x and y result (float type) to double, so it can finnaly
/ be divided with z, resulting in a double value.
*/
double r = x * y / z;
System.out.println(r);
/* Obs.: Applying a unary operator (ex.: ++ or --) to a short value results in a short value,
/ unary operators are excluded from the third rule.
*/
short u = 1;
System.out.println(++u);
}
}