Skip to content

update basics #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/main/java/com/examplehub/basics/Final.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.constant;

public class FinalExample {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.examplehub.basics;
package com.examplehub.basics.conversion;

public class TypeCast {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.conversion;

public class TypeConversionExample {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.examplehub.basics.function;

public class ModifyFunctionArgs {
static class MyObj{
public int age = -1;
}

public static void modify(int num, String str, Integer integer, int[] array, MyObj obj) {
num = num + 1;
str = "<" + str + ">";
integer = integer + 1;
array[0] = -array[0];
obj.age = -obj.age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.examplehub.designpatterns.singleton;

public class SingletonExample1 {
private static final SingletonExample1 INSTANCE = new SingletonExample1();
private SingletonExample1(){

}
public static SingletonExample1 getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.examplehub.designpatterns.singleton;

public class SingletonExample2 {
private static final SingletonExample2 INSTANCE;

static {
/*
* do more work here
*/
INSTANCE = new SingletonExample2();
}
private SingletonExample2(){
}
public static SingletonExample2 getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.examplehub.designpatterns.singleton;

public enum SingletonExample3 {
INSTANCE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.examplehub.designpatterns.singleton;

public class SingletonExample4 {
private static SingletonExample4 INSTANCE;

private SingletonExample4(){
}

public static SingletonExample4 getInstance() {
if (INSTANCE == null) {
INSTANCE = new SingletonExample4();
}
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.examplehub.designpatterns.singleton;

public class SingletonExample5 {
private static SingletonExample5 INSTANCE;

private SingletonExample5(){
}

public static SingletonExample5 getInstance() {
if (INSTANCE == null) {
synchronized (SingletonExample5.class) {
if (INSTANCE == null) {
INSTANCE = new SingletonExample5();
}
}
}
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.examplehub.designpatterns.singleton;

public class SingletonExample6 {

private SingletonExample6(){
}

private static final class InstanceHolder {
private static final SingletonExample6 INSTANCE = new SingletonExample6();
}

public static SingletonExample6 getInstance() {
return InstanceHolder.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ void testUnicode() {
assertEquals('A', '\u0041');
assertEquals('中', '\u4e2d');
}

@Test
void testAddAndSub() {
assertEquals('9', '0' + 9);
assertEquals(43, '0' - 5);
assertEquals(32, 'a' - 'A');
assertEquals(9, '9' - '0');
assertEquals(0, 'a' - 97);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ void testAppend() {
builder.append("Hello").append(",").append("World").append("!");
assertEquals("Hello,World!", builder.toString());
}

@Test
void testInitCapacity() {
String firstStr = "123456789";
String secondStr = "987654321";
String thirdStr = "abcef";

StringBuilder builder = new StringBuilder(firstStr.length() + secondStr.length() + thirdStr.length());
assertEquals("123456789987654321abcef", builder.append(firstStr).append(secondStr).append(thirdStr).toString());
}
}
12 changes: 9 additions & 3 deletions src/test/java/com/examplehub/basics/chars/StringExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ void testInit() {
assertEquals("abc", abc);
}

@Test
void testPlusOperator() {
assertEquals("Hi, Java", "Hi, " + "Java");
assertEquals("Hi 3", "Hi " + 3);
assertEquals("Hi 3.14", "Hi " + 3.14);
assertEquals("3Hi4", 3 + "Hi" + 4);
assertEquals("Hi10", "Hi" + 0xA);
}
@Test
void testEqual() {
String s1 = "hello";
Expand Down Expand Up @@ -88,11 +96,9 @@ void testSubstring() {
void testConcat() {
String s1 = "hello";
String s2 = " world!";
assertEquals("hello world!", s1 + s2);
assertEquals("hello world!", s1.concat(s2));

int age = 25;
assertEquals("I'm 25.", "I'm ".concat(25 + "."));
assertEquals("I'm 25.", "I'm ".concat(age + "."));
}

// @Test
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/examplehub/basics/constant/FinalExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.examplehub.basics.constant;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class FinalExampleTest {
@Test
void test() {
final double PI = 3.1415927;
assertEquals(3.1415927, PI);

//PI = 3.14; // cannot assign a value to final variable PI
}

@Test
void testWithoutInit() {
final double PI;
//System.out.println(PI); // variable PI might not have been initialized
}

@Test
void testInitLater() {
final double PI;
PI = 3.1415927;
assertEquals(3.1415927, PI);
}

@Test
void testStatic() {
class MyMath {
public static final double PI = 3.1415927;
}
assertEquals(3.1415927, MyMath.PI);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.examplehub.basics.conversion;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import static org.junit.jupiter.api.Assertions.*;

class TypeConversionExampleTest {
@Test
void testObjectConversion() {
class A {

}
class B {

}
// A a = new B(); // incompatible types: B cannot be converted to A

class Father {
}
class Son extends Father {
}
Father father = new Son(); // ok
assertThrows(ClassCastException.class, () -> {
Son son = (Son) new Father();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.examplehub.basics.function;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ModifyFunctionArgsTest {
@Test
void test() {
int num = 1;
String str = "java";
Integer integer = 55;
int[] array = {1, 2, 3, 4, 5};
ModifyFunctionArgs.MyObj obj = new ModifyFunctionArgs.MyObj();
ModifyFunctionArgs.modify(num, str, integer, array, obj);
assertEquals(1, num);
assertEquals("java", str);
assertEquals(55, integer);
assertEquals(-1, array[0]);
assertEquals(1, obj.age);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.examplehub.designpatterns.singleton;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SingletonExample1Test {
@Test
void test() {
SingletonExample1 firstInstance = SingletonExample1.getInstance();
SingletonExample1 secondInstance = SingletonExample1.getInstance();
assertEquals(firstInstance.hashCode(), secondInstance.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.examplehub.designpatterns.singleton;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SingletonExample2Test {
@Test
void test() {
SingletonExample2 firstInstance = SingletonExample2.getInstance();
SingletonExample2 secondInstance = SingletonExample2.getInstance();
assertEquals(firstInstance.hashCode(), secondInstance.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.examplehub.designpatterns.singleton;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SingletonExample3Test {
@Test
void test() {
SingletonExample3 firstInstance = SingletonExample3.INSTANCE;
SingletonExample3 secondInstance = SingletonExample3.INSTANCE;
assertEquals(firstInstance.hashCode(), secondInstance.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.examplehub.designpatterns.singleton;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SingletonExample4Test {
@Test
void test() {
SingletonExample4 firstInstance = SingletonExample4.getInstance();
SingletonExample4 secondInstance = SingletonExample4.getInstance();
assertSame(firstInstance, secondInstance);
}

@Test
void testMultiThreads() {
//TODO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.examplehub.designpatterns.singleton;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SingletonExample5Test {
@Test
void test() {
SingletonExample5 firstInstance = SingletonExample5.getInstance();
SingletonExample5 secondInstance = SingletonExample5.getInstance();
assertSame(firstInstance, secondInstance);
}

@Test
void testMultiThreads() {
//TODO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.examplehub.designpatterns.singleton;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SingletonExample6Test {
@Test
void test() {
SingletonExample6 firstInstance = SingletonExample6.getInstance();
SingletonExample6 secondInstance = SingletonExample6.getInstance();
assertSame(firstInstance, secondInstance);
}

@Test
void testMultiThreads() {
//TODO
}
}