Browse Source

0403 javaSE

Qing 1 year ago
parent
commit
3e6390d295

+ 38 - 0
spring-demo/.gitignore

@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 5 - 0
spring-demo/.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,5 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+  </profile>
+</component>

+ 157 - 0
spring-demo/.idea/workspace.xml

@@ -0,0 +1,157 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="AutoImportSettings">
+    <option name="autoReloadType" value="SELECTIVE" />
+  </component>
+  <component name="ChangeListManager">
+    <list default="true" id="5aa6272e-23b4-48f4-9aaa-8caf45810aa0" name="Changes" comment="">
+      <change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/Main.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/StaticInterview.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/TestCollection.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/TestMath.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/TestString.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/TestStringIntern.java" afterDir="false" />
+    </list>
+    <option name="SHOW_DIALOG" value="false" />
+    <option name="HIGHLIGHT_CONFLICTS" value="true" />
+    <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
+    <option name="LAST_RESOLUTION" value="IGNORE" />
+  </component>
+  <component name="FileTemplateManagerImpl">
+    <option name="RECENT_TEMPLATES">
+      <list>
+        <option value="Class" />
+      </list>
+    </option>
+  </component>
+  <component name="Git.Settings">
+    <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
+  </component>
+  <component name="ProjectColorInfo"><![CDATA[{
+  "associatedIndex": 1
+}]]></component>
+  <component name="ProjectId" id="2eZtY2aHbAlH73NNbcsXZlSA5aT" />
+  <component name="ProjectLevelVcsManager">
+    <ConfirmationsSetting value="2" id="Add" />
+  </component>
+  <component name="ProjectViewState">
+    <option name="showLibraryContents" value="true" />
+  </component>
+  <component name="PropertiesComponent"><![CDATA[{
+  "keyToString": {
+    "Application.Main.executor": "Run",
+    "Application.StaticInterview.executor": "Run",
+    "Application.TestCollection (1).executor": "Run",
+    "Application.TestCollection.executor": "Run",
+    "Application.TestMath.executor": "Run",
+    "Application.TestStringIntern.executor": "Run",
+    "Maven. [org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate].executor": "Run",
+    "RunOnceActivity.OpenProjectViewOnStart": "true",
+    "RunOnceActivity.ShowReadmeOnStart": "true",
+    "git-widget-placeholder": "master",
+    "kotlin-language-version-configured": "true",
+    "node.js.detected.package.eslint": "true",
+    "node.js.detected.package.tslint": "true",
+    "node.js.selected.package.eslint": "(autodetect)",
+    "node.js.selected.package.tslint": "(autodetect)",
+    "nodejs_package_manager_path": "npm",
+    "settings.editor.selected.configurable": "MavenSettings",
+    "vue.rearranger.settings.migration": "true"
+  }
+}]]></component>
+  <component name="RunManager" selected="Application.TestCollection (2)">
+    <configuration name="TestCollection (1)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
+      <option name="MAIN_CLASS_NAME" value="com.sf.TestCollection" />
+      <module name="spring-demo" />
+      <extension name="coverage">
+        <pattern>
+          <option name="PATTERN" value="com.sf.*" />
+          <option name="ENABLED" value="true" />
+        </pattern>
+      </extension>
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <configuration name="TestCollection (2)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
+      <option name="MAIN_CLASS_NAME" value="com.sf.TestCollection" />
+      <module name="spring-demo" />
+      <extension name="coverage">
+        <pattern>
+          <option name="PATTERN" value="com.sf.*" />
+          <option name="ENABLED" value="true" />
+        </pattern>
+      </extension>
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <configuration name="TestCollection" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
+      <option name="MAIN_CLASS_NAME" value="com.sf.TestCollection" />
+      <module name="spring-demo" />
+      <extension name="coverage">
+        <pattern>
+          <option name="PATTERN" value="com.sf.*" />
+          <option name="ENABLED" value="true" />
+        </pattern>
+      </extension>
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <configuration name="TestMath" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
+      <option name="MAIN_CLASS_NAME" value="com.sf.TestMath" />
+      <module name="spring-demo" />
+      <extension name="coverage">
+        <pattern>
+          <option name="PATTERN" value="com.sf.*" />
+          <option name="ENABLED" value="true" />
+        </pattern>
+      </extension>
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <configuration name="TestStringIntern" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
+      <option name="MAIN_CLASS_NAME" value="com.sf.TestStringIntern" />
+      <module name="spring-demo" />
+      <extension name="coverage">
+        <pattern>
+          <option name="PATTERN" value="com.sf.*" />
+          <option name="ENABLED" value="true" />
+        </pattern>
+      </extension>
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <recent_temporary>
+      <list>
+        <item itemvalue="Application.TestCollection (2)" />
+        <item itemvalue="Application.TestCollection (1)" />
+        <item itemvalue="Application.TestCollection" />
+        <item itemvalue="Application.TestMath" />
+        <item itemvalue="Application.TestStringIntern" />
+      </list>
+    </recent_temporary>
+  </component>
+  <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
+  <component name="TaskManager">
+    <task active="true" id="Default" summary="Default task">
+      <changelist id="5aa6272e-23b4-48f4-9aaa-8caf45810aa0" name="Changes" comment="" />
+      <created>1712123972326</created>
+      <option name="number" value="Default" />
+      <option name="presentableId" value="Default" />
+      <updated>1712123972326</updated>
+      <workItem from="1712123973520" duration="6752000" />
+    </task>
+    <servers />
+  </component>
+  <component name="TypeScriptGeneratedFilesManager">
+    <option name="version" value="3" />
+  </component>
+</project>

+ 20 - 0
spring-demo/pom.xml

@@ -0,0 +1,20 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>com.sf</groupId>
+  <artifactId>spring-demo</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>spring-demo</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+
+  </dependencies>
+</project>

+ 34 - 0
spring-demo/src/main/java/com/sf/Main.java

@@ -0,0 +1,34 @@
+package com.sf;
+
+public class Main {
+
+    // 三个重载方法,参数类型分别为以上三种类型
+    static void sayHello(Human human) {
+        System.out.println("human say Hello");
+    }
+
+    static void sayHello(Man man) {
+        System.out.println("man say Hello");
+    }
+
+    static void sayHello(Woman woman) {
+        System.out.println("woman say Hello");
+    }
+
+    public static void main(String[] args) {
+        Human man = new Man();
+        Human woman = new Woman();
+        sayHello(man);
+        sayHello(woman);
+    }
+}
+
+// 声明一个父类 两个子类
+class Human {
+}
+
+class Man extends Human {
+}
+
+class Woman extends Human {
+}

+ 27 - 0
spring-demo/src/main/java/com/sf/StaticInterview.java

@@ -0,0 +1,27 @@
+package com.sf;
+// 在有继承关系时 代码块、静态代码块和构造器的执行顺序  523164  524163  524613
+public class StaticInterview extends Base {
+    {
+        System.out.println("interview init 1");
+    }
+    static {
+        System.out.println("interview static 2");
+    }
+    public StaticInterview() {
+        System.out.println("interview constructor 3");
+    }
+    public static void main(String[] args) {
+        new StaticInterview();
+    }
+}
+class Base {
+    {
+        System.out.println("base init 4");
+    }
+    static {
+        System.out.println("base static 5");
+    }
+    public Base() {
+        System.out.println("base constructor 6");
+    }
+}

+ 36 - 0
spring-demo/src/main/java/com/sf/TestCollection.java

@@ -0,0 +1,36 @@
+package com.sf;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+public class TestCollection {
+
+    public static void main(String[] args) {
+        List<Integer> list = new ArrayList<>();
+        list.add(3);
+        list.add(1);
+        list.add(0);
+        list.add(2);
+        System.out.println(list);
+        Collections.sort(list, new Comparator<Integer>() {
+            @Override
+            public int compare(Integer o1, Integer o2) {
+                // 返回 -1 0 1 什么时候交换元素顺序  返回1交换
+                // 10 20  -10 -> -1  默认顺序
+                // 20 10  10  ->  1
+                // 10 10  0
+                if(o1 > o2){
+                    return -1;
+                }
+                //
+                if(o1 < o2){
+                    return 1;
+                }
+                return 0;
+            }
+        });
+        System.out.println(list);
+    }
+}

+ 9 - 0
spring-demo/src/main/java/com/sf/TestMath.java

@@ -0,0 +1,9 @@
+package com.sf;
+
+public class TestMath {
+
+    public static void main(String[] args) {
+        System.out.println(Math.log10(12321));
+        System.out.println(Math.pow(10, 4));
+    }
+}

+ 33 - 0
spring-demo/src/main/java/com/sf/TestString.java

@@ -0,0 +1,33 @@
+package com.sf;
+
+public class TestString {
+
+    public static void main(String[] args) {
+        // 创建了几个对象 0个
+        // 因为在jvm启动时已经创建了空串
+        String str0 = "";
+        // 创建了几个对象  1个
+        String str = "hello";
+        // 创建了几个对象  1个
+        String str1 = "he" + "llo";  // str1 = "hello"
+
+        // 创建了几个对象  2个
+        // 对象创建在哪里呢  常量池和堆中
+        String str2 = new String("hello");
+
+        // 创建了几个对象   A 2个  B 3个  C 4个  D 5个
+        String str3 = "hello";
+        String str4 = str3 + "world";
+        // 3个  变量和常量拼接的情况  StringBuilder来处理
+        new StringBuilder().append(str3).append("world").toString();
+        // 堆中的对象  hello world  常量池的对象 hello、world
+
+        // 创建了几个对象   A 3个  B 4个  C 5个  D 6个
+        String str5 = "hello";
+        String str6 = "hi" + str5 + "wor" + "ld";
+        // 5个
+        // 堆中的对象  hi hello world
+        // 常量池的对象  hello hi wor ld
+//        new StringBuilder().append("hi").append(str5).append("wor").append("ld");
+    }
+}

+ 29 - 0
spring-demo/src/main/java/com/sf/TestStringIntern.java

@@ -0,0 +1,29 @@
+package com.sf;
+
+public class TestStringIntern {
+
+    public static void main(String[] args) {
+        String str = new String("hello");
+        // 这个方法的作用,是先去常量池中查看是否有等于 "hello"的字符串  如果有 返回字符串
+        // 如果没有 会创建字符串  然后返回字符串的引用地址
+        str.intern();
+
+//        String str1 = new String("hello") + new String(" world");
+//        str1 = str1.intern();
+//        String str2 = "hello world";
+//        System.out.println(str1 == str2);
+        // str1和str2直接比较时  false不相等
+        // str1从常量池中取值重新赋值后  true相等
+        //    常量池中保存的是 堆中字符串的地址
+
+
+        // 相等 但是区别在 常量池中保存的是字符串"hello world"
+        String str1 = new String("hello") + new String(" world");
+        String str2 = "hello world";
+        str1 = str1.intern();
+        System.out.println(str1 == str2);
+
+        // String为什么是不可变的?
+
+    }
+}