File tree 3 files changed +24
-4
lines changed
src/test/java/itx/examples/javaisweird/tests 3 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ gradle --build-file di-examples/build.gradle clean test
41
41
* [ ssh server demo] ( ssh-server-demo )
42
42
* [ JCE demo] ( jce-demo )
43
43
* [ com.fasterxml.jackson] ( jackson-fasterxml-demo )
44
+ * [ weird java stuff] ( java-is-weird )
44
45
* __ Databases__
45
46
* [ mongodb demo] ( mongodb-demo )
46
47
* [ hibernate demo] ( hibernate-demo )
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change @@ -18,16 +18,23 @@ public void testBigDecimalCompare() {
18
18
19
19
@ Test
20
20
public void testIntegerCompare () {
21
- Integer a = 100 ;
22
- Integer b = 100 ;
21
+ Integer a = 42 ;
22
+ Integer b = 42 ;
23
23
24
- Integer c = 200 ;
25
- Integer d = 200 ;
24
+ Integer c = 666 ;
25
+ Integer d = 666 ;
26
26
27
27
assertTrue (a == b );
28
28
assertEquals (a , b );
29
29
assertFalse (c == d );
30
30
assertEquals (c , d );
31
31
}
32
32
33
+ @ Test
34
+ public void testCharArithmetic () {
35
+ char ch = '0' ;
36
+ ch *= 1.1 ;
37
+ assertEquals ('4' , ch );
38
+ }
39
+
33
40
}
You can’t perform that action at this time.
0 commit comments