Skip to content

Commit e622435

Browse files
author
Juraj Veverka
committed
added java weird example and documentation
1 parent 67db97c commit e622435

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ gradle --build-file di-examples/build.gradle clean test
4141
* [ssh server demo](ssh-server-demo)
4242
* [JCE demo](jce-demo)
4343
* [com.fasterxml.jackson](jackson-fasterxml-demo)
44+
* [weird java stuff](java-is-weird)
4445
* __Databases__
4546
* [mongodb demo](mongodb-demo)
4647
* [hibernate demo](hibernate-demo)

java-is-weird/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Examples of weird Java stuff :)
2+
3+
* [testBigDecimalCompare](src/test/java/itx/examples/javaisweird/tests/WeirdTesting) -
4+
BigDecimal.equals(?) compares content of objects, however BigDecimal.compare(?) deliberately considers values like '1' and '1.0' as same value.
5+
* [testIntegerCompare](src/test/java/itx/examples/javaisweird/tests/WeirdTesting) -
6+
The Integer type keeps a cache of all objects with a value in the range of -128 to 127 for performance reasons. So when you declare new variables in that range, you’re actually referring to the same object.
7+
* [testCharArithmetic](src/test/java/itx/examples/javaisweird/tests/WeirdTesting) -
8+
```
9+
char ch = '0'; // ASCII for ‘0’ is 48
10+
ch *= 1.1; // 48 x 1.1 is 52.8 which turns to 52 when cast to char
11+
assertEquals('4', ch); // 52 represents ‘4’ in ASCII
12+
```

java-is-weird/src/test/java/itx/examples/javaisweird/tests/WeirdTesting.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ public void testBigDecimalCompare() {
1818

1919
@Test
2020
public void testIntegerCompare() {
21-
Integer a = 100;
22-
Integer b = 100;
21+
Integer a = 42;
22+
Integer b = 42;
2323

24-
Integer c = 200;
25-
Integer d = 200;
24+
Integer c = 666;
25+
Integer d = 666;
2626

2727
assertTrue(a == b);
2828
assertEquals(a, b);
2929
assertFalse(c == d);
3030
assertEquals(c, d);
3131
}
3232

33+
@Test
34+
public void testCharArithmetic() {
35+
char ch = '0';
36+
ch *= 1.1;
37+
assertEquals('4', ch);
38+
}
39+
3340
}

0 commit comments

Comments
 (0)