Skip to content

Commit f239d89

Browse files
update basics (#186)
1 parent dd6a3d7 commit f239d89

23 files changed

+319
-20
lines changed

src/main/java/com/examplehub/basics/Final.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.constant;
2+
3+
public class FinalExample {
4+
}

src/main/java/com/examplehub/basics/TypeCast.java renamed to src/main/java/com/examplehub/basics/conversion/TypeCast.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.examplehub.basics;
1+
package com.examplehub.basics.conversion;
22

33
public class TypeCast {
44
public static void main(String[] args) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.conversion;
2+
3+
public class TypeConversionExample {
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.basics.function;
2+
3+
public class ModifyFunctionArgs {
4+
static class MyObj{
5+
public int age = -1;
6+
}
7+
8+
public static void modify(int num, String str, Integer integer, int[] array, MyObj obj) {
9+
num = num + 1;
10+
str = "<" + str + ">";
11+
integer = integer + 1;
12+
array[0] = -array[0];
13+
obj.age = -obj.age;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
public class SingletonExample1 {
4+
private static final SingletonExample1 INSTANCE = new SingletonExample1();
5+
private SingletonExample1(){
6+
7+
}
8+
public static SingletonExample1 getInstance() {
9+
return INSTANCE;
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
public class SingletonExample2 {
4+
private static final SingletonExample2 INSTANCE;
5+
6+
static {
7+
/*
8+
* do more work here
9+
*/
10+
INSTANCE = new SingletonExample2();
11+
}
12+
private SingletonExample2(){
13+
}
14+
public static SingletonExample2 getInstance() {
15+
return INSTANCE;
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
public enum SingletonExample3 {
4+
INSTANCE
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
public class SingletonExample4 {
4+
private static SingletonExample4 INSTANCE;
5+
6+
private SingletonExample4(){
7+
}
8+
9+
public static SingletonExample4 getInstance() {
10+
if (INSTANCE == null) {
11+
INSTANCE = new SingletonExample4();
12+
}
13+
return INSTANCE;
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
public class SingletonExample5 {
4+
private static SingletonExample5 INSTANCE;
5+
6+
private SingletonExample5(){
7+
}
8+
9+
public static SingletonExample5 getInstance() {
10+
if (INSTANCE == null) {
11+
synchronized (SingletonExample5.class) {
12+
if (INSTANCE == null) {
13+
INSTANCE = new SingletonExample5();
14+
}
15+
}
16+
}
17+
return INSTANCE;
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
public class SingletonExample6 {
4+
5+
private SingletonExample6(){
6+
}
7+
8+
private static final class InstanceHolder {
9+
private static final SingletonExample6 INSTANCE = new SingletonExample6();
10+
}
11+
12+
public static SingletonExample6 getInstance() {
13+
return InstanceHolder.INSTANCE;
14+
}
15+
}

src/test/java/com/examplehub/basics/chars/CharExampleTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ void testUnicode() {
2323
assertEquals('A', '\u0041');
2424
assertEquals('中', '\u4e2d');
2525
}
26+
27+
@Test
28+
void testAddAndSub() {
29+
assertEquals('9', '0' + 9);
30+
assertEquals(43, '0' - 5);
31+
assertEquals(32, 'a' - 'A');
32+
assertEquals(9, '9' - '0');
33+
assertEquals(0, 'a' - 97);
34+
}
2635
}

src/test/java/com/examplehub/basics/chars/StringBuilderExampleTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ void testAppend() {
1111
builder.append("Hello").append(",").append("World").append("!");
1212
assertEquals("Hello,World!", builder.toString());
1313
}
14+
15+
@Test
16+
void testInitCapacity() {
17+
String firstStr = "123456789";
18+
String secondStr = "987654321";
19+
String thirdStr = "abcef";
20+
21+
StringBuilder builder = new StringBuilder(firstStr.length() + secondStr.length() + thirdStr.length());
22+
assertEquals("123456789987654321abcef", builder.append(firstStr).append(secondStr).append(thirdStr).toString());
23+
}
1424
}

src/test/java/com/examplehub/basics/chars/StringExampleTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ void testInit() {
2121
assertEquals("abc", abc);
2222
}
2323

24+
@Test
25+
void testPlusOperator() {
26+
assertEquals("Hi, Java", "Hi, " + "Java");
27+
assertEquals("Hi 3", "Hi " + 3);
28+
assertEquals("Hi 3.14", "Hi " + 3.14);
29+
assertEquals("3Hi4", 3 + "Hi" + 4);
30+
assertEquals("Hi10", "Hi" + 0xA);
31+
}
2432
@Test
2533
void testEqual() {
2634
String s1 = "hello";
@@ -88,11 +96,9 @@ void testSubstring() {
8896
void testConcat() {
8997
String s1 = "hello";
9098
String s2 = " world!";
91-
assertEquals("hello world!", s1 + s2);
9299
assertEquals("hello world!", s1.concat(s2));
93-
94100
int age = 25;
95-
assertEquals("I'm 25.", "I'm ".concat(25 + "."));
101+
assertEquals("I'm 25.", "I'm ".concat(age + "."));
96102
}
97103

98104
// @Test
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.examplehub.basics.constant;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class FinalExampleTest {
8+
@Test
9+
void test() {
10+
final double PI = 3.1415927;
11+
assertEquals(3.1415927, PI);
12+
13+
//PI = 3.14; // cannot assign a value to final variable PI
14+
}
15+
16+
@Test
17+
void testWithoutInit() {
18+
final double PI;
19+
//System.out.println(PI); // variable PI might not have been initialized
20+
}
21+
22+
@Test
23+
void testInitLater() {
24+
final double PI;
25+
PI = 3.1415927;
26+
assertEquals(3.1415927, PI);
27+
}
28+
29+
@Test
30+
void testStatic() {
31+
class MyMath {
32+
public static final double PI = 3.1415927;
33+
}
34+
assertEquals(3.1415927, MyMath.PI);
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.examplehub.basics.conversion;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.function.Executable;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class TypeConversionExampleTest {
9+
@Test
10+
void testObjectConversion() {
11+
class A {
12+
13+
}
14+
class B {
15+
16+
}
17+
// A a = new B(); // incompatible types: B cannot be converted to A
18+
19+
class Father {
20+
}
21+
class Son extends Father {
22+
}
23+
Father father = new Son(); // ok
24+
assertThrows(ClassCastException.class, () -> {
25+
Son son = (Son) new Father();
26+
});
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.examplehub.basics.function;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ModifyFunctionArgsTest {
8+
@Test
9+
void test() {
10+
int num = 1;
11+
String str = "java";
12+
Integer integer = 55;
13+
int[] array = {1, 2, 3, 4, 5};
14+
ModifyFunctionArgs.MyObj obj = new ModifyFunctionArgs.MyObj();
15+
ModifyFunctionArgs.modify(num, str, integer, array, obj);
16+
assertEquals(1, num);
17+
assertEquals("java", str);
18+
assertEquals(55, integer);
19+
assertEquals(-1, array[0]);
20+
assertEquals(1, obj.age);
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class SingletonExample1Test {
8+
@Test
9+
void test() {
10+
SingletonExample1 firstInstance = SingletonExample1.getInstance();
11+
SingletonExample1 secondInstance = SingletonExample1.getInstance();
12+
assertEquals(firstInstance.hashCode(), secondInstance.hashCode());
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class SingletonExample2Test {
8+
@Test
9+
void test() {
10+
SingletonExample2 firstInstance = SingletonExample2.getInstance();
11+
SingletonExample2 secondInstance = SingletonExample2.getInstance();
12+
assertEquals(firstInstance.hashCode(), secondInstance.hashCode());
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class SingletonExample3Test {
8+
@Test
9+
void test() {
10+
SingletonExample3 firstInstance = SingletonExample3.INSTANCE;
11+
SingletonExample3 secondInstance = SingletonExample3.INSTANCE;
12+
assertEquals(firstInstance.hashCode(), secondInstance.hashCode());
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class SingletonExample4Test {
8+
@Test
9+
void test() {
10+
SingletonExample4 firstInstance = SingletonExample4.getInstance();
11+
SingletonExample4 secondInstance = SingletonExample4.getInstance();
12+
assertSame(firstInstance, secondInstance);
13+
}
14+
15+
@Test
16+
void testMultiThreads() {
17+
//TODO
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class SingletonExample5Test {
8+
@Test
9+
void test() {
10+
SingletonExample5 firstInstance = SingletonExample5.getInstance();
11+
SingletonExample5 secondInstance = SingletonExample5.getInstance();
12+
assertSame(firstInstance, secondInstance);
13+
}
14+
15+
@Test
16+
void testMultiThreads() {
17+
//TODO
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.examplehub.designpatterns.singleton;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class SingletonExample6Test {
8+
@Test
9+
void test() {
10+
SingletonExample6 firstInstance = SingletonExample6.getInstance();
11+
SingletonExample6 secondInstance = SingletonExample6.getInstance();
12+
assertSame(firstInstance, secondInstance);
13+
}
14+
15+
@Test
16+
void testMultiThreads() {
17+
//TODO
18+
}
19+
}

0 commit comments

Comments
 (0)