-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutJava7LiteralsEnhancements.java
More file actions
44 lines (35 loc) · 1.2 KB
/
AboutJava7LiteralsEnhancements.java
File metadata and controls
44 lines (35 loc) · 1.2 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
39
40
41
42
43
44
package java7;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutJava7LiteralsEnhancements {
@Koan
public void binaryLiterals() {
//binary literals are marked with 0b prefix
short binaryLiteral = 0b1111;
assertEquals(binaryLiteral, (short )15);
}
@Koan
public void binaryLiteralsWithUnderscores() {
//literals can use underscores for improved readability
short binaryLiteral = 0b1111_1111;
assertEquals(binaryLiteral,(short)255);
}
@Koan
public void numericLiteralsWithUnderscores() {
long literal = 111_111_111L;
//notice capital "B" - a valid binary literal prefix
short multiplier = 0B1_000;
assertEquals(literal * multiplier, 888888888l);
}
@Koan
public void negativeBinaryLiteral() {
int negativeBinaryLiteral = 0b1111_1111_1111_1111_1111_1111_1111_1100 / 4;
assertEquals(negativeBinaryLiteral, (int)-1);
}
@Koan
public void binaryLiteralsWithBitwiseOperator() {
int binaryLiteral = ~0b1111_1111;
assertEquals(binaryLiteral, -256);
}
}