From 7fa904d855a215e216850285553d70fbd8fd5492 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Tue, 22 Mar 2022 20:41:34 +0800 Subject: [PATCH 01/10] =?UTF-8?q?java=20=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- javase/pom.xml | 24 +++++++++++++ .../src/main/java/com/hongyi/day1/Test01.java | 12 +++++++ .../src/main/java/com/hongyi/day1/Test02.java | 34 +++++++++++++++++++ .../src/test/java/com/hongyi/day1/Test01.java | 15 ++++++++ 4 files changed, 85 insertions(+) create mode 100644 javase/pom.xml create mode 100644 javase/src/main/java/com/hongyi/day1/Test01.java create mode 100644 javase/src/main/java/com/hongyi/day1/Test02.java create mode 100644 javase/src/test/java/com/hongyi/day1/Test01.java diff --git a/javase/pom.xml b/javase/pom.xml new file mode 100644 index 0000000..7e12f3c --- /dev/null +++ b/javase/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.hongyi + javase + 1.0-SNAPSHOT + + + 11 + 11 + + + + + junit + junit + 4.13.2 + test + + + \ No newline at end of file diff --git a/javase/src/main/java/com/hongyi/day1/Test01.java b/javase/src/main/java/com/hongyi/day1/Test01.java new file mode 100644 index 0000000..d27ffe9 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day1/Test01.java @@ -0,0 +1,12 @@ +package com.hongyi.day1; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/22 17:06 + * @Version 1.0 + */ +public class Test01 { + public static void main(String[] args) { + Integer[] arr = new Integer[1024*1024*1024]; + } +} diff --git a/javase/src/main/java/com/hongyi/day1/Test02.java b/javase/src/main/java/com/hongyi/day1/Test02.java new file mode 100644 index 0000000..b1ca563 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day1/Test02.java @@ -0,0 +1,34 @@ +package com.hongyi.day1; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/22 20:07 + * @Version 1.0 + */ +public class Test02 { + public static void main(String[] args) { + FileInputStream fis = null; + try{ + File file = new File("hello.txt"); + fis = new FileInputStream(file); + int data = fis.read(); + while (data != -1) { + System.out.println((char) data); + data = fis.read(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/javase/src/test/java/com/hongyi/day1/Test01.java b/javase/src/test/java/com/hongyi/day1/Test01.java new file mode 100644 index 0000000..e784e5d --- /dev/null +++ b/javase/src/test/java/com/hongyi/day1/Test01.java @@ -0,0 +1,15 @@ +package com.hongyi.day1; + +import org.junit.Test; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/22 20:01 + * @Version 1.0 + */ +public class Test01 { + @Test + public void test1(){ + System.out.println(); + } +} -- Gitee From efbc0de732f307b37d2ec99cc3ba41eaa9ae2d99 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Wed, 23 Mar 2022 14:47:09 +0800 Subject: [PATCH 02/10] =?UTF-8?q?java=E5=9F=BA=E7=A1=80=20=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hongyi/day2/MyException.java | 23 +++++++++ .../com/hongyi/day2/ReturnExceptionDemo.java | 33 ++++++++++++ .../java/com/hongyi/day2/StudentTest.java | 39 ++++++++++++++ .../src/main/java/com/hongyi/day2/Test01.java | 51 +++++++++++++++++++ .../src/main/java/com/hongyi/day2/Test02.java | 28 ++++++++++ 5 files changed, 174 insertions(+) create mode 100644 javase/src/main/java/com/hongyi/day2/MyException.java create mode 100644 javase/src/main/java/com/hongyi/day2/ReturnExceptionDemo.java create mode 100644 javase/src/main/java/com/hongyi/day2/StudentTest.java create mode 100644 javase/src/main/java/com/hongyi/day2/Test01.java create mode 100644 javase/src/main/java/com/hongyi/day2/Test02.java diff --git a/javase/src/main/java/com/hongyi/day2/MyException.java b/javase/src/main/java/com/hongyi/day2/MyException.java new file mode 100644 index 0000000..8d29ad0 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day2/MyException.java @@ -0,0 +1,23 @@ +package com.hongyi.day2; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/23 14:26 + * @Version 1.0 + */ + +// 1.继承现有的异常体系结构RuntimeException、Exception +public class MyException extends RuntimeException{ + + // 2.提供全局常量serialVersionUID + static final long serialVersionUID = -7034897190745766939L; + + // 3.重载的构造器 + public MyException() { + + } + + public MyException(String msg) { + super(msg); + } +} diff --git a/javase/src/main/java/com/hongyi/day2/ReturnExceptionDemo.java b/javase/src/main/java/com/hongyi/day2/ReturnExceptionDemo.java new file mode 100644 index 0000000..13c4e9f --- /dev/null +++ b/javase/src/main/java/com/hongyi/day2/ReturnExceptionDemo.java @@ -0,0 +1,33 @@ +package com.hongyi.day2; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/23 14:34 + * @Version 1.0 + */ +public class ReturnExceptionDemo { + static void methodA() { + try { + System.out.println("进入方法A"); + throw new RuntimeException("制造异常"); + }finally { + System.out.println("用A方法的finally"); + } + } + static void methodB() { + try { + System.out.println("进入方法B"); + return; + } finally { + System.out.println("调用B方法的finally"); + } + } + public static void main(String[] args) { + try { + methodA(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + methodB(); + } +} diff --git a/javase/src/main/java/com/hongyi/day2/StudentTest.java b/javase/src/main/java/com/hongyi/day2/StudentTest.java new file mode 100644 index 0000000..5150a43 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day2/StudentTest.java @@ -0,0 +1,39 @@ +package com.hongyi.day2; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/23 14:16 + * @Version 1.0 + */ +public class StudentTest { + public static void main(String[] args) { + try { + Student s = new Student(); + s.registry(-1); + System.out.println(s); + } catch (Exception e) { + // e.printStackTrace(); + System.out.println(e.getMessage()); + } + } +} + +class Student { + private int id; + public void registry(int id) { + if (id > 0) { + this.id = id; + } else { + // System.out.println("Error!"); + // 手动抛出异常 + throw new MyException("输入数据非法"); + } + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + '}'; + } +} diff --git a/javase/src/main/java/com/hongyi/day2/Test01.java b/javase/src/main/java/com/hongyi/day2/Test01.java new file mode 100644 index 0000000..ab090c7 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day2/Test01.java @@ -0,0 +1,51 @@ +package com.hongyi.day2; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/23 13:50 + * @Version 1.0 + */ +public class Test01 { + // main调用m2,捕获异常并处理 + // main调用m3,因为m3已做了异常捕获和处理,因此不做异常处理 + public static void main(String[] args) { + try { + m2(); + } catch (IOException e) { + e.printStackTrace(); + } + m3(); + } + + // m3调用m1,做捕获异常并异常处理 + public static void m3() { + try { + m1(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + // m2调用m1,也将异常抛出,但不做异常处理 + public static void m2() throws IOException { + m1(); + } + + // m1将异常抛出给调用者,不做异常处理 + public static void m1() throws IOException { + File file = new File("hello.txt"); + FileInputStream fis = new FileInputStream(file); + int data = fis.read(); + while (data != -1) { + System.out.println((char) data); + data = fis.read(); + } + fis.read(); + System.out.println("Hello World"); + } +} diff --git a/javase/src/main/java/com/hongyi/day2/Test02.java b/javase/src/main/java/com/hongyi/day2/Test02.java new file mode 100644 index 0000000..7005787 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day2/Test02.java @@ -0,0 +1,28 @@ +package com.hongyi.day2; + +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/23 14:02 + * @Version 1.0 + */ +public class Test02 { + +} + +class SuperClass { + public void method() throws IOException{ + + } +} + +class SubClass1 extends SuperClass{ + public void method() throws FileNotFoundException { + + } +} + +class SubClass2 extends SuperClass{ +} -- Gitee From 3c8f3021e9d3345aa83ba012337a82af00409cc6 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Wed, 23 Mar 2022 15:49:21 +0800 Subject: [PATCH 03/10] =?UTF-8?q?java=20=E5=B8=B8=E7=94=A8=E7=B1=BB-?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/hongyi/day2/StringTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 javase/src/main/java/com/hongyi/day2/StringTest.java diff --git a/javase/src/main/java/com/hongyi/day2/StringTest.java b/javase/src/main/java/com/hongyi/day2/StringTest.java new file mode 100644 index 0000000..f012a6f --- /dev/null +++ b/javase/src/main/java/com/hongyi/day2/StringTest.java @@ -0,0 +1,42 @@ +package com.hongyi.day2; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/23 14:52 + * @Version 1.0 + */ +public class StringTest { + public static void main(String[] args) { + test2(); + } + + static void test1() { + String s1 = "abc"; // 字面量的定义方式 + String s2 = "abc"; + + System.out.println(s1 == s2); // 比较s1和s2的地址值 + + s1 = "hello"; + System.out.println(s1); + System.out.println(s2); + + } + + static void test2() { + // 此时s1和s2地址对应的数据都在字符串常量池中 + String s1 = "abc"; + String s2 = "abc"; + + // 此时s3和s4地址对应的数据都在堆空间中 + String s3 = new String("abc"); + String s4 = new String("abc"); + + System.out.println(s1 == s2); // true + System.out.println(s1 == s3); // false + System.out.println(s3 == s4); // false + } +} + +class Person { + +} -- Gitee From 57ab8ea3c49568a6466d99572f8489250cad89a4 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Fri, 25 Mar 2022 19:19:52 +0800 Subject: [PATCH 04/10] =?UTF-8?q?java=E9=AB=98=E7=BA=A7=20String=E5=B8=B8?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E6=B3=95=E5=92=8C=E6=95=B0=E6=8D=AE=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/hongyi/day2/StringTest.java | 6 +- .../src/test/java/com/hongyi/day1/Test01.java | 6 + .../src/test/java/com/hongyi/day2/Test01.java | 117 ++++++++++++++++++ .../src/test/java/com/hongyi/day2/Test02.java | 66 ++++++++++ 4 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 javase/src/test/java/com/hongyi/day2/Test01.java create mode 100644 javase/src/test/java/com/hongyi/day2/Test02.java diff --git a/javase/src/main/java/com/hongyi/day2/StringTest.java b/javase/src/main/java/com/hongyi/day2/StringTest.java index f012a6f..8c2957f 100644 --- a/javase/src/main/java/com/hongyi/day2/StringTest.java +++ b/javase/src/main/java/com/hongyi/day2/StringTest.java @@ -7,7 +7,7 @@ package com.hongyi.day2; */ public class StringTest { public static void main(String[] args) { - test2(); + test3(); } static void test1() { @@ -35,8 +35,8 @@ public class StringTest { System.out.println(s1 == s3); // false System.out.println(s3 == s4); // false } -} -class Person { + static void test3() { + } } diff --git a/javase/src/test/java/com/hongyi/day1/Test01.java b/javase/src/test/java/com/hongyi/day1/Test01.java index e784e5d..cfeb952 100644 --- a/javase/src/test/java/com/hongyi/day1/Test01.java +++ b/javase/src/test/java/com/hongyi/day1/Test01.java @@ -1,6 +1,7 @@ package com.hongyi.day1; import org.junit.Test; +import org.junit.runner.RunWith; /** * @Author Kisugi Takumi @@ -12,4 +13,9 @@ public class Test01 { public void test1(){ System.out.println(); } + + @Test + public void test2() { + System.out.println("Hello World"); + } } diff --git a/javase/src/test/java/com/hongyi/day2/Test01.java b/javase/src/test/java/com/hongyi/day2/Test01.java new file mode 100644 index 0000000..61da1ef --- /dev/null +++ b/javase/src/test/java/com/hongyi/day2/Test01.java @@ -0,0 +1,117 @@ +package com.hongyi.day2; + +import org.junit.Test; + +import java.util.Locale; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/25 17:03 + * @Version 1.0 + */ +public class Test01 { + @Test + public void test0() { + String s1 = "javaEE"; + String s2 = "hadoop"; + + String s3 = "javaEEhadoop"; + String s4 = "javaEE" + "hadoop"; + String s5 = s1 + "hadoop"; + String s6 = "javaEE" + s2; + String s7 = s1 + s2; + + System.out.println(s3 == s4); // true + System.out.println(s3 == s5); // false + System.out.println(s3 == s6); // false + System.out.println(s3 == s7); // false + System.out.println(s5 == s6); // false + System.out.println(s5 == s7); // false + System.out.println(s6 == s7); // false + + String s8 = s5.intern(); + System.out.println(s3 == s8); // true + } + + @Test + public void test1() { + String s1 = "HelloWorld"; + System.out.println(s1.length()); // 10 + System.out.println(s1.charAt(1)); // e + System.out.println(s1.isEmpty()); // false + + String s2 = s1.toUpperCase(Locale.ROOT); + System.out.println(s1); // HelloWorld + System.out.println(s2); // HELLOWORLD + + String s3 = " hello world "; + String s4 = s3.trim(); + System.out.println(s3); // 不变 + System.out.println(s4); // hello world + } + + @Test + public void test2() { + String s1 = "abc"; + String s2 = "def"; + String s3 = s1.concat(s2); + System.out.println(s3); // abcdef + + String s4 = "abc"; + String s5 = new String("abz"); + String s6 = "abc"; + System.out.println(s4.compareTo(s5)); // -23 + System.out.println(s4.compareTo(s6)); // 0 + + String s7 = "Hongyi"; + String s8 = s7.substring(1); + String s9 = s7.substring(1,3); + System.out.println(s8); // ongyi + System.out.println(s9); // on + } + + @Test + public void test3() { + String s1 = "helloworld"; + boolean b1 = s1.endsWith("rld"); + boolean b2 = s1.startsWith("ll"); + System.out.println(b1); // true + System.out.println(b2); // false + + boolean b3 = s1.contains("llo"); + System.out.println(b3); // true + + int i1 = s1.indexOf("lo"); + int i2 = s1.indexOf("m"); + System.out.println(i1); // 3 + System.out.println(i2); // -1 + } + + @Test + public void test4() { + String s1 = "helloworld"; + String s2 = s1.replace("h", "a"); + System.out.println(s1); // helloworld + System.out.println(s2); // aelloworld + + String s3 = s1.replace("hello", "hongyi"); + System.out.println(s3); // hongyiworld + + String s4 = "12hello34world4"; + // 把字符串中的数字替换成逗号 , 如果结果中开头和结尾有 , 的话则去掉 + String s5 = s4.replaceAll("\\d+", ",").replaceAll("^,|,$", ""); + System.out.println(s5); // hello,world + } + + @Test + public void test5() { + String s1 = "hello|world|java"; + String[] s2 = s1.split("\\|"); + for (String s : s2) { + System.out.println(s); + // hello + // world + // java + } + } +} diff --git a/javase/src/test/java/com/hongyi/day2/Test02.java b/javase/src/test/java/com/hongyi/day2/Test02.java new file mode 100644 index 0000000..cc5021b --- /dev/null +++ b/javase/src/test/java/com/hongyi/day2/Test02.java @@ -0,0 +1,66 @@ +package com.hongyi.day2; + +import org.junit.Test; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/25 18:53 + * @Version 1.0 + */ +public class Test02 { + @Test + public void test0() { + String s1 = "123"; + int num = Integer.parseInt(s1); + System.out.println(num); + + String s2 = "true"; + boolean flag = Boolean.parseBoolean(s2); + System.out.println(s2); + + String s3 = "123.123"; + double num1 = Double.parseDouble(s3); + System.out.println(num1); + } + + @Test + public void test1() { + int num = 123; + String s = String.valueOf(num); + System.out.println(s); + } + + @Test + public void test2() { + String s1 = "123abc"; + char[] charArray = s1.toCharArray(); + for (char c : charArray){ + System.out.println(c); + } + } + + @Test + public void test3() { + char[] arr = new char[]{'h', 'e', 'l', 'l', 'o'}; + String s = new String(arr); + System.out.println(s); + } + + @Test + public void test4() { + String s1 = "abc123"; + byte[] bytes = s1.getBytes(); + for(byte b:bytes) { + System.out.print(b + ","); // 97,98,99,49,50,51 + } + } + + @Test + public void test5() { + String s1 = "abc123"; + byte[] bytes = s1.getBytes(); + // 采用默认字符集解码 + String s = new String(bytes); + System.out.println(s); // abc123 + } +} -- Gitee From 47716490f18f261476cf87b5942fabc52d537d42 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Sat, 26 Mar 2022 16:33:45 +0800 Subject: [PATCH 05/10] =?UTF-8?q?java=E9=AB=98=E7=BA=A7=20StringBuffer=20S?= =?UTF-8?q?tringBuilder=20=E6=97=A5=E6=9C=9F=E5=92=8C=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hongyi/day3/DateTimeTest.java | 32 ++++++++ .../hongyi/day3/StringBufferBuilderTest.java | 77 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 javase/src/test/java/com/hongyi/day3/DateTimeTest.java create mode 100644 javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java diff --git a/javase/src/test/java/com/hongyi/day3/DateTimeTest.java b/javase/src/test/java/com/hongyi/day3/DateTimeTest.java new file mode 100644 index 0000000..3d0a896 --- /dev/null +++ b/javase/src/test/java/com/hongyi/day3/DateTimeTest.java @@ -0,0 +1,32 @@ +package com.hongyi.day3; + +import org.junit.Test; + +import java.util.Date; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/26 16:09 + * @Version 1.0 + */ +public class DateTimeTest { + @Test + public void test0() { + long time = System.currentTimeMillis(); + System.out.println(time); + } + + @Test + public void test1() { + Date date1 = new Date(); + System.out.println(date1); + System.out.println(date1.getTime()); + + Date date2 = new Date(1648282572968L); + System.out.println(date2); + + java.sql.Date date3 = new java.sql.Date(1648282572968L); + System.out.println(date3); + + } +} diff --git a/javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java b/javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java new file mode 100644 index 0000000..90d5043 --- /dev/null +++ b/javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java @@ -0,0 +1,77 @@ +package com.hongyi.day3; + +import org.junit.Test; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/26 15:23 + * @Version 1.0 + */ +public class StringBufferBuilderTest { + @Test + public void test0() { + StringBuffer sb1 = new StringBuffer("abc"); + // 该方法没有返回值,修改的就是sb1本身的值 + sb1.setCharAt(0, 'm'); + System.out.println(sb1); + } + + @Test + public void test1() { + StringBuffer s1 = new StringBuffer("abc"); + s1.append('1'); + s1.append(1); + System.out.println(s1); // abc11 + + s1.delete(0, 1); + System.out.println(s1); // bc11 + + s1.replace(0, 1, "ab"); + System.out.println(s1); // abc11 + + System.out.println(s1.reverse()); // 11cba + } + + @Test + public void test2() { + //初始设置 + long startTime = 0L; + long endTime = 0L; + String text = ""; + StringBuffer buffer = new StringBuffer(""); + StringBuilder builder = new StringBuilder(""); + //开始对比 + startTime = System.currentTimeMillis(); + for (int i = 0; i < 20000; i++) { + buffer.append(String.valueOf(i)); + } + endTime = System.currentTimeMillis(); + System.out.println("StringBuffer的执行时间:" + (endTime - startTime)); + startTime = System.currentTimeMillis(); + for (int i = 0; i < 20000; i++) { + builder.append(String.valueOf(i)); + } + endTime = System.currentTimeMillis(); + System.out.println("StringBuilder的执行时间:" + (endTime - startTime)); + startTime = System.currentTimeMillis(); + for (int i = 0; i < 20000; i++) { + text = text + i; + } + endTime = System.currentTimeMillis(); + System.out.println("String的执行时间:" + (endTime - startTime)); + } + + @Test + public void test3() { + String str = null; + StringBuffer sb = new StringBuffer(); + sb.append(str); + + System.out.println(sb.length()); // 4 + System.out.println(sb); // "null" + + StringBuffer sb1 = new StringBuffer(str); + System.out.println(sb1); // 报错java.lang.NullPointerException + } + +} -- Gitee From b3b3d09e8e4b0ee3ba4244da6c82ac9d441f98d1 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Mon, 28 Mar 2022 15:09:19 +0800 Subject: [PATCH 06/10] =?UTF-8?q?java=E9=AB=98=E7=BA=A7=20=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=97=B4=E7=9B=B8=E5=85=B3=E7=9A=84api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hongyi/day3/DateTimeTest.java | 102 ++++++++++++++++++ .../hongyi/day3/StringBufferBuilderTest.java | 4 +- 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/javase/src/test/java/com/hongyi/day3/DateTimeTest.java b/javase/src/test/java/com/hongyi/day3/DateTimeTest.java index 3d0a896..d4dc022 100644 --- a/javase/src/test/java/com/hongyi/day3/DateTimeTest.java +++ b/javase/src/test/java/com/hongyi/day3/DateTimeTest.java @@ -2,6 +2,12 @@ package com.hongyi.day3; import org.junit.Test; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.time.temporal.TemporalAccessor; import java.util.Date; /** @@ -29,4 +35,100 @@ public class DateTimeTest { System.out.println(date3); } + + @Test + public void test2() throws ParseException { + // 实例化SimpleDateFormat,使用默认的构造器 + SimpleDateFormat sdf = new SimpleDateFormat(); + // 格式化日期 + Date date = new Date(); + // System.out.println(date); + String format = sdf.format(date); + System.out.println(format); // 2022/3/28 下午2:11 + + // 解析 + String str = "22/3/28 上午11:43"; + Date date1 = sdf.parse(str); + System.out.println(date1); // Mon Mar 28 11:43:00 CST 2022 + + // ---------------------------- + // 按照指定的方式格式化和解析 + SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String format1 = sdf1.format(date); + System.out.println(format1); // 2022-03-28 14:18:45 + + // 解析:要求符合sdf识别的格式 + Date date2 = sdf1.parse("2020-01-01 00:00:00"); + System.out.println(date2); // Wed Jan 01 00:00:00 CST 2020 + } + + @Test + public void test3() { + // 获取当前日期、时间、日期+时间 + LocalDate localDate = LocalDate.now(); + LocalTime localTime = LocalTime.now(); + LocalDateTime localDateTime = LocalDateTime.now(); + + System.out.println(localDate); // 2022-03-28 + System.out.println(localTime); // 14:36:49.369528800 + System.out.println(localDateTime); // 2022-03-28T14:36:49.369528800 + + // of() + // 设置指定的年、月、日、时、分、秒,没有偏移量 + LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10,6, 13, 23, 59); + System.out.println(localDateTime1); // 2020-10-06T13:23:59 + + //getXXX(): + System.out.println(localDateTime.getDayOfMonth()); // 28 + System.out.println(localDateTime.getDayOfWeek()); // MONDAY + } + + @Test + public void test4() { + // 获取UTC时区(本初子午线)的Instant类的对象 + Instant instant = Instant.now(); + System.out.println(instant); // 2022-03-28T06:46:17.166469400Z + + // 加8个小时 + OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8)); + System.out.println(offsetDateTime); // 2022-03-28T14:49:04.847567700+08:00 + + // 获得时间戳 + long milli = instant.toEpochMilli(); + System.out.println(milli); // 1648450257945 + + // 通过时间戳创建Instant对象 + Instant instant1 = Instant.ofEpochMilli(1648450257945L); + System.out.println(instant1);// 2022-03-28T06:50:57.945Z + } + + @Test + public void test5() { + // 方式一:预定义的标准格式 + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + // 格式化:日期 --> 字符串 + LocalDateTime localDateTime = LocalDateTime.now(); + String str1 = formatter.format(localDateTime); + System.out.println("格式化之前: " + localDateTime); // 2022-03-28T14:58:27.893263500 + System.out.println("格式化之后: " + str1); // 2022-03-28T14:58:27.8932635 + + // 解析 + TemporalAccessor parse = formatter.parse("2022-03-28T14:58:27.8932635"); + System.out.println(parse); // {},ISO resolved to 2022-03-28T14:58:27.893263500 + + // 方式二:本地相关的格式 + DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT); + // 格式化 + String str2 = formatter1.format(localDateTime); + System.out.println(str2); // 2022/3/28 下午3:01 + + // 方式三:自定义格式,重点 + DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + // 格式化 + String str3 = formatter2.format(localDateTime); + System.out.println(str3); // 2022-03-28 15:04:37 + // 解析 + TemporalAccessor parse1 = formatter2.parse("2021-01-01 13:00:01"); + System.out.println(parse1); // {},ISO resolved to 2021-01-01T13:00:01 + } } diff --git a/javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java b/javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java index 90d5043..027dee5 100644 --- a/javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java +++ b/javase/src/test/java/com/hongyi/day3/StringBufferBuilderTest.java @@ -70,8 +70,8 @@ public class StringBufferBuilderTest { System.out.println(sb.length()); // 4 System.out.println(sb); // "null" - StringBuffer sb1 = new StringBuffer(str); - System.out.println(sb1); // 报错java.lang.NullPointerException + StringBuffer sb1 = new StringBuffer(str); // 报错java.lang.NullPointerException + System.out.println(sb1); } } -- Gitee From 4c878a65f4230a150f4fd1a62b081e1306bd9735 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Mon, 28 Mar 2022 20:01:06 +0800 Subject: [PATCH 07/10] =?UTF-8?q?java=E9=AB=98=E7=BA=A7=20=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E5=99=A8=E5=92=8CSystem=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- javase/pom.xml | 5 ++ .../src/main/java/com/hongyi/day4/Goods.java | 38 +++++++++ .../java/com/hongyi/day4/CompareTest.java | 83 +++++++++++++++++++ .../test/java/com/hongyi/day4/SystemTest.java | 35 ++++++++ 4 files changed, 161 insertions(+) create mode 100644 javase/src/main/java/com/hongyi/day4/Goods.java create mode 100644 javase/src/test/java/com/hongyi/day4/CompareTest.java create mode 100644 javase/src/test/java/com/hongyi/day4/SystemTest.java diff --git a/javase/pom.xml b/javase/pom.xml index 7e12f3c..3265392 100644 --- a/javase/pom.xml +++ b/javase/pom.xml @@ -20,5 +20,10 @@ 4.13.2 test + + org.projectlombok + lombok + 1.18.22 + \ No newline at end of file diff --git a/javase/src/main/java/com/hongyi/day4/Goods.java b/javase/src/main/java/com/hongyi/day4/Goods.java new file mode 100644 index 0000000..f998f42 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day4/Goods.java @@ -0,0 +1,38 @@ +package com.hongyi.day4; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.ToString; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/28 19:15 + * @Version 1.0 + */ +@Data +@AllArgsConstructor +@ToString +public class Goods implements Comparable{ + private String name; + private double price; + + // 先按照价格从低到高进行排序 + // 再按照产品名称从高到低排序 + @Override + public int compareTo(Object o) { + if (o instanceof Goods){ + Goods goods = (Goods) o; + if(this.price > goods.price){ + return 1; + }else if(this.price < goods.price){ + return -1; + }else{ + // return 0; + return -this.name.compareTo(goods.name); + } + // 方式二: + // return Double.compare(this.price, goods.price); + } + throw new RuntimeException("传入的数据类型不一致"); + } +} diff --git a/javase/src/test/java/com/hongyi/day4/CompareTest.java b/javase/src/test/java/com/hongyi/day4/CompareTest.java new file mode 100644 index 0000000..764e34a --- /dev/null +++ b/javase/src/test/java/com/hongyi/day4/CompareTest.java @@ -0,0 +1,83 @@ +package com.hongyi.day4; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/28 19:06 + * @Version 1.0 + */ +public class CompareTest { + @Test + public void test0() { + String[] arr = new String[]{"AA", "CC", "KK", "MM", "GG", "JJ", "DD"}; + Arrays.sort(arr); + System.out.println(Arrays.toString(arr)); // [AA, CC, DD, GG, JJ, KK, MM] + } + + @Test + public void test1() { + Goods[] goods = new Goods[5]; + goods[0] = new Goods("lenovo", 34); + goods[1] = new Goods("dell", 43); + goods[2] = new Goods("xiaomi", 12); + goods[3] = new Goods("huawei", 65); + goods[4] = new Goods("microsoft", 43); + + Arrays.sort(goods); + System.out.println(Arrays.toString(goods)); + //[Goods(name=xiaomi, price=12.0), + // Goods(name=lenovo, price=34.0), + // Goods(name=microsoft, price=43.0), + // Goods(name=dell, price=43.0), + // Goods(name=huawei, price=65.0)] + } + + @Test + public void test2(){ + String[] arr = new String[]{"AA", "CC", "KK", "MM", "GG", "JJ", "DD"}; + Arrays.sort(arr, new Comparator() { + @Override + public int compare(String o1, String o2) { + // 从大到小排序 + return -o1.compareTo(o2); + } + }); + System.out.println(Arrays.toString(arr)); + // [MM, KK, JJ, GG, DD, CC, AA] + } + + @Test + public void test3(){ + Goods[] goods = new Goods[6]; + goods[0] = new Goods("lenovo", 34); + goods[1] = new Goods("dell", 43); + goods[2] = new Goods("xiaomi", 12); + goods[3] = new Goods("huawei", 65); + goods[4] = new Goods("microsoft", 43); + goods[5] = new Goods("microsoft", 12); + + Arrays.sort(goods, new Comparator() { + @Override + public int compare(Goods o1, Goods o2) { + // 先照产品名称从低到高排序 + // 再按照价格从高到低进行排序 + if (o1.getName().equals(o2.getName())){ + return -Double.compare(o1.getPrice(), o2.getPrice()); + }else{ + return o1.getName().compareTo(o2.getName()); + } + } + }); + System.out.println(Arrays.toString(goods)); + // [Goods(name=dell, price=43.0), + // Goods(name=huawei, price=65.0), + // Goods(name=lenovo, price=34.0), + // Goods(name=microsoft, price=43.0), + // Goods(name=microsoft, price=12.0), + // Goods(name=xiaomi, price=12.0)] + } +} diff --git a/javase/src/test/java/com/hongyi/day4/SystemTest.java b/javase/src/test/java/com/hongyi/day4/SystemTest.java new file mode 100644 index 0000000..c993a76 --- /dev/null +++ b/javase/src/test/java/com/hongyi/day4/SystemTest.java @@ -0,0 +1,35 @@ +package com.hongyi.day4; + +import org.junit.Test; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/28 19:56 + * @Version 1.0 + */ +public class SystemTest { + @Test + public void test0(){ + String javaVersion = System.getProperty("java.version"); + System.out.println("java的version:" + javaVersion); + String javaHome = System.getProperty("java.home"); + System.out.println("java的home:" + javaHome); + String osName = System.getProperty("os.name"); + System.out.println("os的name:" + osName); + String osVersion = System.getProperty("os.version"); + System.out.println("os的version:" + osVersion); + String userName = System.getProperty("user.name"); + System.out.println("user的name:" + userName); + String userHome = System.getProperty("user.home"); + System.out.println("user的home:" + userHome); + String userDir = System.getProperty("user.dir"); + System.out.println("user的dir:" + userDir); + // java的version:11.0.10 + // java的home:C:\Users\Hongyi\.jdks\corretto-11.0.10 + // os的name:Windows 10 + // os的version:10.0 + // user的name:Hongyi + // user的home:C:\Users\Hongyi + // user的dir:E:\develop\study\backend_study\javase + } +} -- Gitee From 69813119b3802ac35c56a49fb481e54e903e3be7 Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Tue, 29 Mar 2022 19:22:10 +0800 Subject: [PATCH 08/10] =?UTF-8?q?java=E9=AB=98=E7=BA=A7=20=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E7=B1=BB=E5=92=8C=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hongyi/day4/AnnotationTest.java | 59 ++++++++++ .../test/java/com/hongyi/day4/EnumTest.java | 105 ++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 javase/src/test/java/com/hongyi/day4/AnnotationTest.java create mode 100644 javase/src/test/java/com/hongyi/day4/EnumTest.java diff --git a/javase/src/test/java/com/hongyi/day4/AnnotationTest.java b/javase/src/test/java/com/hongyi/day4/AnnotationTest.java new file mode 100644 index 0000000..8f13744 --- /dev/null +++ b/javase/src/test/java/com/hongyi/day4/AnnotationTest.java @@ -0,0 +1,59 @@ +package com.hongyi.day4; + +import org.junit.Test; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/29 19:04 + * @Version 1.0 + */ +public class AnnotationTest { + @Test + public void test0(){ + Person p = new Person("Hongyi", 24); + p.eat(); + + @SuppressWarnings("unused") + int a = 10; + } +} + +class Person{ + private String name; + private int age; + + public Person() { + + } + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public void walk(){ + System.out.println("walk..."); + } + + @Deprecated + public void eat(){ + System.out.println("eat..."); + } + +} + +interface Infor{ + void show(); +} + +class Student extends Person implements Infor{ + @Override + public void walk() { + System.out.println("student walks..."); + } + + @Override + public void show() { + System.out.println("student shows..."); + } +} \ No newline at end of file diff --git a/javase/src/test/java/com/hongyi/day4/EnumTest.java b/javase/src/test/java/com/hongyi/day4/EnumTest.java new file mode 100644 index 0000000..d2ae8dd --- /dev/null +++ b/javase/src/test/java/com/hongyi/day4/EnumTest.java @@ -0,0 +1,105 @@ +package com.hongyi.day4; + +import org.junit.Test; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/29 18:23 + * @Version 1.0 + */ +public class EnumTest { + @Test + public void test0(){ + System.out.println(Season.SPRING); // SPRING + System.out.println(Season.class.getSuperclass()); // class java.lang.Enum + } + + @Test + public void test1(){ + Season summer = Season.SUMMER; + // 枚举类对象的toString方法 + // 输出枚举类对象的名称 + System.out.println(summer.toString()); // SUMMER + // values()返回枚举类型的对象数组 + Season[] seasons = Season.values(); + for(Season season:seasons){ + System.out.println(season); + // SPRING + // SUMMER + // AUTUMN + // WINTER + } + Thread.State[] states = Thread.State.values(); + for(Thread.State state:states){ + System.out.println(state); + // NEW + // RUNNABLE + // BLOCKED + // WAITING + // TIMED_WAITING + // TERMINATED + } + //--------------------------------------------- + Season winter = Season.valueOf("WINTER"); + System.out.println(winter); // WINTER + } + + @Test + public void test2(){ + Season autumn = Season.AUTUMN; + autumn.show(); // 这是秋天 + } +} + +interface Info{ + void show(); +} + +enum Season implements Info{ + // 1.提供当前枚举类的对象 + // 多个对象之间用 , 隔开,最后一个用 ; + SPRING("春天", "春暖花开"){ + @Override + public void show() { + System.out.println("这是春天"); + } + }, + SUMMER("夏天", "烈日当空"){ + @Override + public void show() { + System.out.println("这是夏天"); + } + }, + AUTUMN("秋天", "秋高气爽"){ + @Override + public void show() { + System.out.println("这是秋天"); + } + }, + WINTER("冬天", "冰天雪地"){ + @Override + public void show() { + System.out.println("这是冬天"); + } + }; + + + // 2.声明Season对象的属性 + private final String seasonName; + private final String seasonDesc; + + // 3.私有化类的构造器,并给对象属性赋值 + private Season(String seasonName, String seasonDesc){ + this.seasonName = seasonName; + this.seasonDesc = seasonDesc; + } + + // 4.获取枚举类对象的属性 + public String getSeasonName() { + return seasonName; + } + + public String getSeasonDesc() { + return seasonDesc; + } +} -- Gitee From 434897a75a218f0efa7afcde67473b16a423310c Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Wed, 30 Mar 2022 13:43:47 +0800 Subject: [PATCH 09/10] =?UTF-8?q?java=E9=AB=98=E7=BA=A7=20=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- javase/src/main/java/com/hongyi/day5/MyAnnotation.java | 10 ++++++++++ .../src/test/java/com/hongyi/day4/AnnotationTest.java | 3 +++ 2 files changed, 13 insertions(+) create mode 100644 javase/src/main/java/com/hongyi/day5/MyAnnotation.java diff --git a/javase/src/main/java/com/hongyi/day5/MyAnnotation.java b/javase/src/main/java/com/hongyi/day5/MyAnnotation.java new file mode 100644 index 0000000..6896d43 --- /dev/null +++ b/javase/src/main/java/com/hongyi/day5/MyAnnotation.java @@ -0,0 +1,10 @@ +package com.hongyi.day5; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/30 13:24 + * @Version 1.0 + */ +public @interface MyAnnotation { + String value() default "world"; +} diff --git a/javase/src/test/java/com/hongyi/day4/AnnotationTest.java b/javase/src/test/java/com/hongyi/day4/AnnotationTest.java index 8f13744..3799e65 100644 --- a/javase/src/test/java/com/hongyi/day4/AnnotationTest.java +++ b/javase/src/test/java/com/hongyi/day4/AnnotationTest.java @@ -1,5 +1,6 @@ package com.hongyi.day4; +import com.hongyi.day5.MyAnnotation; import org.junit.Test; /** @@ -16,8 +17,10 @@ public class AnnotationTest { @SuppressWarnings("unused") int a = 10; } + } +@MyAnnotation(value = "hello") class Person{ private String name; private int age; -- Gitee From f830cbf1e786af506ba0581cbb404f1ac88624df Mon Sep 17 00:00:00 2001 From: zenghongyi <277382367@qq.com> Date: Wed, 30 Mar 2022 14:30:21 +0800 Subject: [PATCH 10/10] =?UTF-8?q?java=E9=AB=98=E7=BA=A7=20Collections?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hongyi/day5/CollectionsTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 javase/src/test/java/com/hongyi/day5/CollectionsTest.java diff --git a/javase/src/test/java/com/hongyi/day5/CollectionsTest.java b/javase/src/test/java/com/hongyi/day5/CollectionsTest.java new file mode 100644 index 0000000..c99e719 --- /dev/null +++ b/javase/src/test/java/com/hongyi/day5/CollectionsTest.java @@ -0,0 +1,69 @@ +package com.hongyi.day5; + +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * @Author Kisugi Takumi + * @Date 2022/3/30 14:02 + * @Version 1.0 + */ +public class CollectionsTest { + @Test + public void test0(){ + List list = new ArrayList<>(); + list.add(123); + list.add(43); + list.add(0); + list.add(-97); + list.add(765); + System.out.println(list); // [123, 43, 0, -97, 765] + // 倒序处理 + Collections.reverse(list); + System.out.println(list); // [765, -97, 0, 43, 123] + // 升序排序 + Collections.sort(list); + System.out.println(list); // [-97, 0, 43, 123, 765] + // 定制排序 + Collections.sort(list, new Comparator() { + // 按照降序排序 + @Override + public int compare(Integer o1, Integer o2) { + return -Integer.compare(o1, o2); + } + }); + System.out.println(list); // [765, 123, 43, 0, -97] + } + + @Test + public void test1(){ + List list = new ArrayList<>(); + list.add(123); + list.add(43); + list.add(0); + list.add(-97); + list.add(765); + // 错误的写法:Source does not fit in dest + // List dest = new ArrayList<>(); + // Collections.copy(dest, list); + + List dest = Arrays.asList(new Integer[list.size()]); + System.out.println(dest.size()); // 5 + Collections.copy(dest, list); + System.out.println(dest); // [123, 43, 0, -97, 765] + } + + @Test + public void test2(){ + List list = new ArrayList<>(); + list.add(123); + list.add(43); + list.add(0); + list.add(-97); + list.add(765); + // 返回一个线程安全的list1 + List list1 = Collections.synchronizedList(list); + } +} -- Gitee