[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/buleli-AI/JavaNote/main/Java.md [Back]  [Original]

# SE ## ### #### | | | | | | :------: | :------------: | :------------------: | :-------------------------: | | | | | | | | | | | | | | | | | | | | JDK8 | | | | | | | | | | | https://www.bilibili.com/video/BV1TE41177mP *** #### ##### Java **byte** - byte 8 **8 ** - -128-2^7 1272^7-1 - `0` - byte byte int - `byte a = 100byte b = -50` **short** - short 16 - -32768-2^15 327672^15 - 1 - short byte short int - `0` - `short s = 1000short r = -20000` **int** - int 32 4 - -2,147,483,648-2^31 2,147,483,6472^31 - 1 - int - `0` - `int a = 100000, int b = -200000` **long** - long 64 8 - -9,223,372,036,854,775,808-2^63 9,223,372,036,854,775,8072^63 -1 - - ` 0L` - `long a = 100000LLong b = -200000L`L I 1 **float** - float 32 IEEE 754 - float - `0.0f` - - `float f1 = 234.5F` **double** - double 64 IEEE 754 - double - double - `0.0d` - `double d1 = 123.4` **boolean** - boolean - true false - JVM boolean int boolean byte boolean 4 1 - `false` - `boolean one = true` **char** - char 16 **** Unicode - `\u0000` 0 - `\uffff` 65535 - char **** - `char c = 'A'``char c = ''` **** ##### * float double Java **** ```java //1.1double1.1 float float f = 1.1;// //1.1f float float f = 1.1f; ``` ```java float f1 = 1.234f; double d1 = f1; double d2 = 1.23; float f2 = (float) d2;// ``` ```java int i1 = 1245; long l1 = i1; long l2 = 1234; int i2 = (int) l2; ``` * 1 int short int short += ++ ```java short s1 = 1; s1 += 1; //s1++; // s1 + 1 s1 = (short) (s1 + 1); ``` *** ##### * Object * ** null ** ```java byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean ``` Java * 1. toString() 2. Integer.toString() 3. + * **** 1. Xxx.parseXxx("") `Integer.parseInt(numStr)` 2. Xxx.valueOf("") `Integer.valueOf(numStr)` ```java public class PackageClass02 { public static void main(String[] args) { // 1. Integer it = 100 ; // a.toString() String itStr = it.toString(); System.out.println(itStr+1);//1001 // b.Integer.toString() String itStr1 = Integer.toString(it); System.out.println(itStr1+1);//1001 // c.+ String itStr2 = it + ""; System.out.println(itStr2+1);// 1001 // 2. String numStr = "23"; int numInt = Integer.valueOf(numStr); System.out.println(numInt+1);//24 String doubleStr = "99.9"; double doubleDb = Double.valueOf(doubleStr); System.out.println(doubleDb+0.1);//100.0 } } ``` *** ##### * > Integer.parseInt(String) String Integer > > Java * > Java * Java > Object * == > == > == *** #### **** **** ```java public class PackegeClass { public static void main(String[] args) { int a = 12 ; Integer a1 = 12 ; // Integer a2 = a ; // Integer a3 = null; // null Integer c = 100 ; int c1 = c ; // Integer it = Integer.valueOf(12); // // Integer it1 = new Integer(12); // Integer it2 = 12; Integer it3 = 111 ; int it33 = it3.intValue(); // } } ``` **** `Integer.valueOf()` ```java public static Integer valueOf(int i) { if (i >= IntegerCache.low && i API ```java public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String msg = sc.nextLine(); } } ``` **** ### #### **** * [] `int[] arr` * []`int arr[]` * [] = new []{1,2,...}`int[] arr = new int[]{11,22,33}` * [] = {1,2,...}`int[] arr = {44,55,66}` * [] = new []`int[] arr = new int[3]` #### * **** **0** index * ****[]`arr[0]` * ****`arr[0] = 10` *** #### Java | | | | ---------- | ---------------------------------------------------------- | | | CPU | | | JVM | | | class | | | new | | | main | **Java ** * ![](https://seazean.oss-cn-beijing.aliyuncs.com/img/Java/-.png) * ![](https://seazean.oss-cn-beijing.aliyuncs.com/img/Java/-.png) * ![](https://seazean.oss-cn-beijing.aliyuncs.com/img/Java/-.png) *** #### * ArrayIndexOutOfBoundsException * NullPointerException ```java public class ArrayDemo { public static void main(String[] args) { int[] arr = new int[3]; //null arr = null; System.out.println(arr[0]); } } ``` arr = null arr *** #### * [][] = new [m] [n]`int[][] arr = new int[3][3]` * m * n * * [][] = new [][]{{1, 2...} , {1, 2...} * [][] = {{1, 2...}, {1, 2...}...} * `int[][] arr = {{11,22,33}, {44,55,66}}` ```java public class Test1 { /* : 1. 2. */ public static void main(String[] args) { int[][] arr = {{11, 22, 33}, {33, 44, 55}}; // 1. for (int i = 0; i < arr.length; i++) { //System.out.println(arr[i]); // 2. //int[] temp = arr[i]; for (int j = 0; j < arr[i].length; j++) { System.out.println(arr[i][j]); } } } } ``` **** ### * i++ ++i i++ i 1++i i 1 * || |&& & **& | && || ** && false false true|| true & | ** && || ** * ^ 1 0 * 1 0 * switch Java 7 switch String ```java String s = "a"; switch (s) { case "a": System.out.println("aaa"); break; case "b": System.out.println("bbb"); break; default: break; } ``` switch longfloatdoubleswitch if * break * **** 0 1 * 0 ```java 100: 00000000 00000000 00000000 01100100 ``` * 1 1 +1 ```java -100 : 10000000 00000000 00000000 01100100 //32 -100 : 11111111 11111111 11111111 10011011 -100 : 11111111 11111111 11111111 10011100 ``` 1 * `>>` 2 * `>` 0 * 0 * 0 1 0 1 * 0 **** ### #### * * #### **** ... * * **** ```java public static void main(String[] args) { sum(); // sum(10); // sum(10,20,30); // sum(new int[]{10,30,50,70,90}); // } public static void sum(int... nums){ int sum = 0; for(int i : a) { sum += i; } return sum; } ``` *** ### #### method static protectedprivatepublic JDK8 ** static ** *** #### ```java public static () { //; return ; } ``` ```java = () ; ``` * * * * return * void return * void *** #### * ```java public class MethodDemo { public static void main(String[] args) { } public static void methodOne() { public static void methodTwo() { // !!! } } } ``` * void return return ```java public static void methodTwo() { //return 100; return; //System.out.println(100); return } ``` *** #### ##### 1. **** 2. **** 3. **** **** JVM ```java public class MethodDemo { public static void fn(int a) { // } public static int fn(int a) { /**/ // } public static void fn(int a, int b) {/**/ // } } ``` *** ##### Java * (auto-boxingauto-unboxing) * * Java **** ```java public class MethodDemo { void invoke(Object obj, Object... args) { ... } void invoke(String s, Object obj, Object... args) { ... } invoke(null, 1); // invoke invoke(null, 1, 2); // invokeStringObject invoke(null, new Object[]{1}); // invoke // JVM->-> } ``` *** ##### **** * * **** *** #### Java **** **** * ```java public class ArgsDemo01 { public static void main(String[] args) { int number = 100; System.out.println("change" + number);//100 change(number); System.out.println("change" + number);//100 } public static void change(int number) { number = 200; } } ``` * **** ```java public class PassByValueExample { public static void main(String[] args) { Dog dog = new Dog("A"); func(dog); System.out.println(dog.getName()); // B } private static void func(Dog dog) { dog.setName("B"); } } class Dog { String name;//..... } ``` *** ### Java ```java enum { } ``` * final * java.lang.Enum * * * | | | | ------------------------------------------------- | ------------------------------------ | | String name() | | | int ordinal() | | | int compareTo(E o) | | | String toString() | | | static T valueOf(Class type,String name) | | | values() | | * ```java enum Season { SPRING , SUMMER , AUTUMN , WINTER; } // public final class Season extends java.lang.Enum { public static final Season SPRING = new Season(); public static final Season SUMMER = new Season(); public static final Season AUTUMN = new Season(); public static final Season WINTER = new Season(); public static Season[] values(); public static Season valueOf(java.lang.String); } ``` * API ```java public class EnumDemo { public static void main(String[] args){ // Season s = Season.SPRING; System.out.println(s); //SPRING System.out.println(s.ordinal()); // 0summer 1 s.s.doSomething(); // Season[] ss = Season.values(); for(int i = 0; i < ss.length; i++){ System.out.println(ss[i]); } int result = Season.SPRING.compareTo(Season.WINTER); System.out.println(result);//-3 } } enum Season { SPRING , SUMMER , AUTUMN , WINTER; public void doSomething() { System.out.println("hello "); } } ``` *** ### Debug Debug Debug Debugger Console ![](https://seazean.oss-cn-beijing.aliyuncs.com/img/Java/Debug.png) Debug [Debug] *** ## ### Java **** * * == **** * *** ### #### ```java class { } ``` 1. StudentNameCode 2. Java Java 3. Java public **public Java ** ```java : class { 1.(Field): 2.(Method): 3.(Constructor): 4. 5. } public class ClassDemo { System.out.println(1);// } ``` *** #### ```java (){ } ``` **** = new * * ------ ### `package ` `import .` *** ### 1. ** private ** 2. ** getter setter ** private *** ### this this * this * this ** this ** * this * this ------ ### static #### Java static static * * static **** * static **** * * static * static **** #### static * **** * . * . * * . * static * . * . JVM * static * . ```java public class Student { // 1.static public static void inAddr(){ } // 2.static public void eat(){} public static void main(String[] args) { // a.. Student.inAddr(); inAddr(); // b.. // Student.eat(); // Student sea = new Student(); sea.eat(); } } ``` *** #### * main * * class jdk8 * * * ? * * **** * * ? * ------ ### #### Java * / * * **** * * 1. 2. **** 3. 4. 5. Object Object **Object Java ** ```java extends { } ``` * * * **** ```java public class ExtendsDemo { public static void main(String[] args) { Cat c = new Cat(); // c.run(); Cat.test(); System.out.println(Cat.schoolName); } } class Cat extends Animal{ } class Animal{ public static String schoolName ="seazean"; public static void test(){} private void run(){} } ``` *** #### **** super.super ```java public class ExtendsDemo { public static void wmain(String[] args) { Wolf w = new Wolf();w w.showName(); } } class Wolf extends Animal{ private String name = ""; public void showName(){ String name = ""; System.out.println(name); // name System.out.println(this.name); // name System.out.println(super.name); // System.out.println(name1); // //System.out.println(name2); // } } class Animal{ public String name = ""; public String name1 = ""; } ``` *** #### **** **** @Override * * @Override **** - - - - - **** ```java public class ExtendsDemo { public static void main(String[] args) { Wolf w = new Wolf(); w.run(); } } class Wolf extends Animal{ @Override public void run(){}// } class Animal{ public void run(){} } ``` *** #### * 1. super() 2. 3. JVM ```java class Animal { public Animal() { System.out.println("==Animal=="); } } class Tiger extends Animal { public Tiger() { super(); // System.out.println("==Tiger=="); } public Tiger(String name) { //super(); System.out.println("==Tiger=="); } } ``` * ** Java ** Java ```java class A{ public void test(){ System.out.println("A"); } } class B{ public void test(){ System.out.println("B"); } } class C extends A , B { public static void main(String[] args){ C c = new C(); c.test(); // Java } } ``` ------ ### super super * this this.this.**this(...)** * super super.super.super(...) * this(...) super(...) * this(...) super(...) * this(...) super(...) **** ```java public class ThisDemo { public static void main(String[] args) { // Student s1 = new Student("", 1000 ); Student s2 = new Student("", 2000, "" ); } } class Study extends Student { public Study(String name, int age, String schoolName) { super(name , age , schoolName) ; // } } class Student{ private String name ; private int age ; private String schoolName ; public Student() { } public Student(String name , int age){ // this(name , age , ""); } public Student(String name, int age, String schoolName) { this.name = name; this.age = age; this.schoolName = schoolName; } // .......get + set } ``` *** ### final #### final * final * final * final final abstract **** *** #### ##### final public static final final 1. 2. ```java public class FinalDemo { //public static final public static final String SCHOOL_NAME = "" ; public static final String SCHOOL_NAME1; static{ //SCHOOL_NAME = "java";// SCHOOL_NAME1 = "1"; } } ``` *** ##### final final 1 1. 2. 3. ```java public class FinalDemo { private final String name = "" ; private final String name1; private final String name2; { // name1 = "1"; } // public FinalDemo(){ name2 = "2"; } public FinalDemo(String a){ name2 = "2"; } public static void main(String[] args) { FinalDemo f1 = new FinalDemo(); //f1.name = "1"; // } } ``` *** ### #### > abstract abstract **** **** ```java public class AbstractDemo { public static void main(String[] args) { Dog d = new Dog(); d.run(); } } class Dog extends Animal{ @Override public void run() { System.out.println(""); } } abstract class Animal{ public abstract void run(); } ``` *** #### ? * * **** > ```java public class AbstractDemo { public static void main(String[] args) { //Animal a = new Animal(); //a.run(); // } } abstract class Animal{ private String name; public static String schoolName = ""; public Animal(){ } public abstract void run(); // public void go(){ } } ``` static abstract static *** #### **** ""**** ```java // public class ExtendsDemo { public static void main(String[] args) { Student xiaoMa = new Student(); xiaoMa.write(); } } class Student extends Template{ @Override public String writeText() {return "\t"} } // 1. abstract class Template{ private String title = "\t\t\t\t\t"; private String start = "\t"; private String last = "\t"; public void write(){ System.out.println(title+"\n"+start); System.out.println(writeText()); System.out.println(last); } // public abstract String writeText(); } ``` *** ### #### Java ```java interface { // // // // } ``` * public abstract * * public static final public static final ```java public interface InterfaceDemo{ //public static final String SCHOOL_NAME = ""; String SCHOOL_NAME = ""; //public abstract void run(); void run();// } ``` *** #### **** * * * **** ```java class implements 1,2,3,....{ } interface extend 1,2,3,....{ } ``` 1. 2. 3. **** 4. **** ```java public class InterfaceDemo { public static void main(String[] args) { Student s = new Student(); s.run(); s.rule(); } } class Student implements Food, Person{ @Override public void eat() {} @Override public void run() {} } interface Food{ void eat(); } interface Person{ void run(); } // interface Person extend Food, // class Student implements Person ``` *** #### jdk1.8 * * default public * * * * public * * ClassName.method() * * JDK 1.9 **** ```java public class InterfaceDemo { public static void main(String[] args) { // 1. Man m = new Man(); m.run(); m.work(); // 2. InterfaceJDK8.inAddr(); } } class Man implements InterfaceJDK8 { @Override public void work() { System.out.println(""); } } interface InterfaceJDK8 { // void work(); // a. // default void run() { go(); System.out.println(""); } // b. // static void inAddr() { System.out.println(""); } // c.: JDK 1.9 // private void go() { System.out.println(""); } } ``` *** #### | **** | **** | **** | | ------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | | | | jdk8 | | | **extends** | **implements** | | | | | | Java | Java | | | | **public****protected** **default** | **public** | | main | main | jdk8 main jdk8 default static main | | | | | | | | | | | | | ------ ### #### * > ```java = new ; = new ; ``` * **** * **** * * * * **** * **** * ```java public class PolymorphicDemo { public static void main(String[] args) { Animal c = new Cat(); c.run(); //c.eat();// go(c); go(new Dog); } // DogCat Anima public static void go(Animal d){} } class Dog extends Animal{} class Cat extends Animal{ public void eat(); @Override public void run(){} } class Animal{ public void run(){} } ``` *** #### > > >1. >2. **** **** - ** (upcasting)** - ** (downcasting)** ```java public class PolymorphicDemo { public static void main(String[] args){ Animal a = new Cat(); // Cat c = (Cat)a; // } } class Animal{} class Cat extends Animal{} ``` *** #### instanceof instanceof * * ** = ()()** * / ClassCastException ```java public class Demo{ public static void main(String[] args){ Aniaml a = new Dog(); //Dog d = (Dog)a; //Cat c = (Cat)a; ClassCastException if(a instanceof Cat){ Cat c = (Cat)a; } else if(a instanceof Dog) { Dog d = (Dog)a; } } } class Dog extends Animal{} class Cat extends Animal{} class Animal{} ``` *** ### #### **** **** *** #### static * * * . . = new . * ? * ? ```java public class Demo{ public static void main(String[] args){ Outter.Inner in = new Outter.Inner(); } } static class Outter{ public static int age; private double salary; public static class Inner{ // System.out.println(age); //System.out.println(salary); } } ``` *** #### static . . = new .new * `Outter.Inner in = new Outter().new Inner()` **** * * *** #### for ```java public class InnerClass{ public static void main(String[] args){ String name; class{} } public static void test(){ class Animal{} class Cat extends Animal{} } } ``` *** #### ```java new ||(){ // } ``` * * * ** new ** * ****JVM ```java public class Anonymity { public static void main(String[] args) { Animal a = new Animal(){ @Override public void run() { System.out.println("~~"); //System.out.println(n); } }; a.run(); a.go(); } } abstract class Animal{ public abstract void run(); public void go(){ System.out.println("go~~~"); } } ``` *** ### **private -> -> protected - > public ** | | private | | protected | public | | ------------------ | :-----: | :--: | :-------: | :----: | | | | | | | | | X | | | | | | X | | | | | | X | X | | | | | X | X | X | | protected * protected * protected protected *** ### #### ```java static { } ``` * * static * * * * ** main ** ```java public class CodeDemo { public static String schoolName ; public static ArrayList lists = new ArrayList(); // ,! static { System.out.println("~~~~~~~"); // schoolName = ""; lists.add("3"); lists.add("4"); lists.add("5"); } public static void main(String[] args) { System.out.println("main"); System.out.println(schoolName); System.out.println(lists); } } /*~~~~~~~ main [3, 4, 5] */ ``` *** #### ```java { } ``` * * static * * * ```java public class CodeDemo { private String name; private ArrayList lists = new ArrayList(); { name = ""; lists.add("java"); System.out.println("~~~~~~~~"); } public CodeDemo02(){ }// public CodeDemo02(String name){} public static void main(String[] args) { CodeDemo c = new CodeDemo();// System.out.println(c.name); System.out.println(c.lists); new CodeDemo02();// } } ``` *** ## API ### Object #### Object Java Object Object Object Object * `public String toString()`@Student@735b478 * toString() toString() * toString() toString * `public boolean equals(Object o)` * `protected Object clone()` ```java public boolean equals(Object o) { // 1.true if (this == o) return true; // 2.null , if (o == null || this.getClass() != o.getClass()) return false; // 3.o Student student = (Student) o; return age == student.age && sex == student.sex && Objects.equals(name, student.name); } ``` ****== equals * == **** * Object **** equals **** java.lang.Object hashCode * hashCode HashtableHashMap * equals(java.lang.Object) hashCode * *** #### Object clone() protected clone() clone() * (shallowCopy)**** **Java **Object.clone()System.arraycopy()Arrays.copyOf() * (deepCopy) Cloneable clone clone() Cloneable * Clone & Copy`Student s = new Student` `Student s1 = s` copy references s1 Object `Student s2 = s.clone()` Student s * Shallow Clone & Deep Clone Object clone() copy * StringInteger **** * ![](https://seazean.oss-cn-beijing.aliyuncs.com/img/Java/Object.jpg) ```java public class Student implements Cloneable{ private String name; private Integer age; private Date date; @Override protected Object clone() throws CloneNotSupportedException { Student s = (Student) super.clone(); s.date = (Date) date.clone(); return s; } //..... } ``` SDP *** ### Objects Objects Object Objects * `public static boolean equals(Object a, Object b)` ```java public static boolean equals(Object a, Object b) { // return a == b || a != null && a.equals(b); } ``` * `public static boolean isNull(Object obj)` null null true * `public static String toString()` * `public static String toString(, )` ```java public class ObjectsDemo { public static void main(String[] args) { Student s1 = null; Student s2 = new Student(); System.out.println(Objects.equals(s1 , s2));// // System.out.println(s1.equals(s2)); // System.out.println(Objects.isNull(s1)); System.out.println(s1 == null);// } } public class Student { } ``` *** ### String #### String final **Integer ** ```java public final class String implements java.io.Serializable, Comparable, CharSequence { /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0 } ``` Java 9 String byte `coder` value final value String value ** String ** **** ```java String s = "abc"; s = s + "cd"; //s = abccd ``` **** #### API * `public boolean equals(String s)` * `public boolean equalsIgnoreCase(String anotherString)` * `public int length()` * `public String trim()` * `public String[] split(String regex)` * `public char charAt(int index)` * `public char[] toCharArray()` * `public boolean startsWith(String prefix)` * `public int indexOf(String str)` -1 * `public int lastIndexOf(String str)` str -1 * `public String substring(int beginIndex)` * `public String substring(int i, int j)` j - 1 j - i * `public String toLowerCase()` String * `public String toUpperCase()` String * `public String replace(CharSequence target, CharSequence replacement)` ```java String s = 123-78; s.replace("-","");//12378 ``` *** #### * `public String()` * `public String(char[] chs)` * `public String(String original)` `String s = "abc"` abc - new new **** - `" "` JVM ** String Pool ** `String str = new String("abc")` * abc * abc equals() [] `new String("a") + new String("b")` * 1new StringBuilder() * 2new String("a") 3 a * 4new String("b") 5 b [] * StringBuilder toString() ```java @Override public String toString() { return new String(value, 0, count); } ``` * 6new String("ab") * StringBuilder toString() ** ab**new String("ab") ab String *** #### String Pool ##### String Pool / StringTable / literal strings Java **** * StringTable HashTable `-XX:StringTableSize` JDK 1.8 60013 * * **** StringBuilder#appendappend JDK 1.8 * **** * String intern() String Pool *** ##### intern() JDK 1.8 intern() String Pool * String Pool * **** Pool JDK 1.6 ```java public class Demo { // a b abjava // ldc #2 a "a" ldc: // ldc #3 b "b" // ldc #4 ab "ab" public static void main(String[] args) { String s1 = "a"; // String s2 = "b"; String s3 = "ab"; // String s4 = s1 + s2; // // new StringBuilder().append("a").append("b").toString() new String("ab") String s5 = "a" + "b"; // javac ab System.out.println(s3 == s4); // false System.out.println(s3 == s5); // true String x2 = new String("c") + new String("d"); // new String("cd") // new cd toString() x2.intern(); String x1 = "cd"; System.out.println(x1 == x2); //true } } ``` - == - == ```java String s1 = "ab"; // String s2 = new String("a") + new String("b"); // // String s = new String("ab"); ``` **** ##### ```java public static void main(String[] args) { String s = new String("a") + new String("b");//new String("ab") //"ab" String s2 = s.intern(); //jdk6"ab" //jdk8"ab", new String("ab") System.out.println(s2 == "ab");//jdk6:true jdk8:true System.out.println(s == "ab");//jdk6:false jdk8:true } ``` ```java public static void main(String[] args) { String str1 = new StringBuilder("58").append("tongcheng").toString(); System.out.println(str1 == str1.intern());//true String str2 = new StringBuilder("ja").append("va").toString(); System.out.println(str2 == str2.intern());//false } ``` * System Version Version ```java private static void initializeSystemClass() { sun.misc.Version.init(); } ``` * Version launcher_name `"java"` ```java package sun.misc; public class Version { private static final String launcher_name = "java"; private static final String java_version = "1.8.0_221"; private static final String java_runtime_name = "Java(TM) SE Runtime Environment"; private static final String java_profile_name = ""; private static final String java_runtime_version = "1.8.0_221-b11"; //... } ``` *** ##### Java 7 String Pool Java 7 String Pool OutOfMemoryError StringTable * `-Xmx10m` 10m * JDK8 `-Xmx10m -XX:-UseGCOverheadLimit` Run Configurations VM options * JDK6 `-XX:MaxPermSize=10m` ```java public static void main(String[] args) throws InterruptedException { List list = new ArrayList(); int i = 0; try { for (int j = 0; j < 260000; j++) { list.add(String.valueOf(j).intern()); i++; } } catch (Throwable e) { e.printStackTrace(); } finally { System.out.println(i); } } ``` ![](https://seazean.oss-cn-beijing.aliyuncs.com/img/Java/JVM-.png) *** #### * -XX:StringTableSize= * intern ```java /** * intern * -XX:StringTableSize=200000 -XX:+PrintStringTableStatistics * -Xsx500m -Xmx500m -XX:+PrintStringTableStatistics -XX:StringTableSize=200000 */ public class Demo1_25 { public static void main(String[] args) throws IOException { List address = new ArrayList(); System.in.read(); for (int i = 0; i < 10; i++) { // try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("linux.words"), "utf-8"))) { String line = null; long start = System.nanoTime(); while (true) { line = reader.readLine(); if(line == null) { break; } address.add(line.intern()); } System.out.println("cost:" +(System.nanoTime()-start)/1000000); } } System.in.read(); } } ``` *** #### * hash String HashMap key hash * String Pool String String Pool String String Pool * String String String String String * String * String API *** ### StringBuilder String StringBuffer StringBuilder * String : **** * StringBuffer : **** synchronized * StringBuilder : ****JDK5.0 char[] * `public StringBuilder()` * `public StringBuilder(String str)` API : * `public StringBuilder append()` * `public StringBuilder reverse()` * `public String toString()` toString() StringBuilder String ```java String str = "abc"; char data[] = {'a', 'b', 'c'}; StringBuffer sb1 = new StringBuffer();//new byte[16] sb1.append('a'); //value[0] = 'a'; ``` append ```java public AbstractStringBuilder append(String str) { if (str == null) return appendNull(); int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this; } private void ensureCapacityInternal(int minimumCapacity) { // char if (minimumCapacity - value.length > 0) { //int newCapacity = (value.length yyyyMMdd HH:mm:ss EEE a" ```java public static void main(String[] args){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss); String time = sdf.format(date); System.out.println(time);//2020-10-18 19:58:34 //121s long time = date.getTime(); time+=121; System.out.println(sdf.formate(time)); String d = "2020-10-18 20:20:20";// Date newDate = sdf.parse(d); System.out.println(sdf.format(newDate)); // } ``` **** ### Calendar Calendar Calendar `Calendar rightNow = Calendar.getInstance()`**** Calendar * `public static Calendar getInstance()` * `public int get(int field)` * `public void set(int field,int value)` * `public void add(int field,int amount)`/ * `public final Date getTime()` * `public long getTimeInMillis()` ```java public static void main(String[] args){ Calendar rightNow = Calendar.getInsance(); int year = rightNow.get(Calendar.YEAR);// int month = rightNow.get(Calendar.MONTH) + 1;//+1 int days = rightNow.get(Calendar.DAY_OF_YEAR); rightNow.set(Calendar.YEAR , 2099);// rightNow.add(Calendar.HOUR , 15);//15 -1515 Date date = rightNow.getTime();// long time = rightNow.getTimeInMillis();// //700 rightNow.add(Calendar.DAY_OF_YEAR , 701); Date date d = rightNow.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(d));//700 } ``` *** ### LocalDateTime JDK1.8 + LocalDate + LocalTime + LocalDateTime + * public static LocalDateTime now() * public static LocalDateTime of(, , , , , ) API | | | | --------------------------------------------------------- | ------------------------------------------------------------ | | public int getYear() | | | public int getMonthValue() | 1-12 | | public int getDayOfMonth() | 1-31 | | public int getDayOfYear() | 1-366 | | public DayOfWeek getDayOfWeek() | | | public int getMinute() | | | public int getHour() | | | public LocalDate toLocalDate() | LocalDate | | public LocalTime toLocalTime() | LocalTime | | public String format() | LocalDateTime | | public LocalDateTime parse(, ) | LocalDateTime | | public static DateTimeFormatter ofPattern(String pattern) | DateTimeFormatter | ```java public class JDK8DateDemo2 { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println(now); LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11); System.out.println(localDateTime); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"); String s = localDateTime.format(pattern); LocalDateTime parse = LocalDateTime.parse(s, pattern); } } ``` | | | | ------------------------------------------- | -------------- | | public LocalDateTime plusYears (long years) | | | public LocalDateTime withYear(int year) | | **** Duration API | | | | ------------------------------------------------ | -------------------- | | public static Period between(,) | " | | public int getYears() | | | public int getMonths() | | | public int getDays() | | | public long toTotalMonths() | | | public static Durationbetween(,) | " | | public long toSeconds() | | | public long toMillis() | | | public long toNanos() | | ```java public class JDK8DateDemo9 { public static void main(String[] args) { LocalDate localDate1 = LocalDate.of(2020, 1, 1); LocalDate localDate2 = LocalDate.of(2048, 12, 12); Period period = Period.between(localDate1, localDate2); System.out.println(period);//P28Y11M11D Duration duration = Duration.between(localDateTime1, localDateTime2); System.out.println(duration);//PT21H57M58S } } ``` *** ### Math Math Math | | | | -------------------------------------------- | --------------------------------- | | public static int abs(int a) | a | | public static double ceil(double a) | | | public static double floor(double a) | | | public static double pow(double a, double b) | a b | | public static long round(double a) | | | public static int max(int a,int b) | | | public static int min(int a,int b) | | | public static double random() | double [0.0,1.0) | ```java public class MathDemo { public static void main(String[] args) { // 1.: System.out.println(Math.abs(10)); System.out.println(Math.abs(-10.3)); // 2.: 5 System.out.println(Math.ceil(4.00000001)); // 5.0 System.out.println(Math.ceil(-4.00000001));//4.0 // 3.4 System.out.println(Math.floor(4.99999999)); // 4.0 System.out.println(Math.floor(-4.99999999)); // 5.0 // 4. System.out.println(Math.pow(2 , 3)); // 2^3 = 8.0 // 5. 10 System.out.println(Math.round(4.49999)); // 4 System.out.println(Math.round(4.500001)); // 5 System.out.println(Math.round(5.5));//6 } } ``` **** ### DecimalFormat ```java public static void main(String[]args){ double pi = 3.1415927;// // System.out.println(new DecimalFormat("0").format(pi));//3 // System.out.println(new DecimalFormat("0.00").format(pi));//3.14 //0 System.out.println(new DecimalFormat("00.000").format(pi));// 03.142 // System.out.println(new DecimalFormat("#").format(pi));//3 // System.out.println(new DecimalFormat("#.##%").format(pi));//314.16% long c =299792458;// // System.out.println(new DecimalFormat("#.#####E0").format(c));//2.99792E8 // System.out.println(new DecimalFormat("00.####E0").format(c));//29.9792E7 // System.out.println(new DecimalFormat(",###").format(c));//299,792,458 // System.out.println(new DecimalFormat(",###").format(c)); } ``` *** ### BigDecimal Java java.math API 16 * `public static BigDecimal valueOf(double val)` * `public BigDecimal(double val)` * `public BigDecimal(String val)` API * `public BigDecimal add(BigDecimal value)` * `public BigDecimal subtract(BigDecimal value)` * `public BigDecimal multiply(BigDecimal value)` * `public BigDecimal divide(BigDecimal value)` * `public double doubleValue()` BigDecimal double * `public int intValue()` int * `public BigDecimal divide (BigDecimal value)` ```java public class BigDecimalDemo { public static void main(String[] args) { // + - * / System.out.println(0.1 + 0.2); System.out.println(1.301 / 100); double a = 0.1 ; double b = 0.2 ; double c = a + b ; System.out.println(c);//0.30000000000000004 // 1. BigDecimal a1 = BigDecimal.valueOf(a); BigDecimal b1 = BigDecimal.valueOf(b); BigDecimal c1 = a1.add(b1);//a1.divide(b1); System.out.println(c1); // BigDecimaldouble double d = c1.doubleValue(); } } ``` 1. BigDecimal 2. BigDecimal 3. divide ```java BigDecimal divide = bd1.divide(,,); //1BigDecimal //2 //3 // BigDecimal.ROUND_UP // BigDecimal.ROUND_FLOOR // BigDecimal.ROUND_HALF_UP ``` *** ### Regex #### qq ```java public static boolean checkQQRegex(String qq){ return qq!=null && qq.matches("\\d{4,}");// 4 }// \\d \ \t \n ``` java.util.regex - Pattern Pattern Pattern Pattern Pattern - Matcher Matcher Pattern Matcher Pattern matcher Matcher - PatternSyntaxException PatternSyntaxException *** #### ##### **** *** ##### \r\n Windows Unix/Linux \n | | | | ------ | ------------------------------------------------------------ | | \ | | | \f | | | \n | | | \r | | | \t | | | \\ | \ | | () | () | *** ##### **** | | | | ------ | ------------------------------------------------------------ | | . | \n [\s\S] | | \d | 0~9 [0-9] | | \D | [ ^0-9] | | \w | [a-zA-Z_0-9_] | | \W | \w[ ^\w] | | \s | [\f\n\r\t\v] | | \S | \s | \x \0 \xA 10 ASCII \n *** ##### [ ] **** | | | | ------------ | ----------------------------------------- | | [ab5@] | "a" "b" "5" "@" | | [^abc] | "a","b","c" | | [f-k] | "f"~"k" | | [^A-F0-3] | "A","F","0"~"3" | | [a-d[m-p]] | a d m p[a-dm-p] | | [a-z&&[m-p]] | a z m p[a-dm-p] | | [^] | | * ^,- \ * [\d. \ -+] +- *** ##### * ( )\* + * ? | | | | ------ | --------------------------------- | | X? | X {0,1} | | X* | X {0,} | | X+ | X {1,} | | X{n} | X n | | {n,} | X n | | {n,m} | X n m | *** #### ##### | | | | ------ | ------------------------------------------------------------ | | ^ | | | $ | | | \b | | *** ##### `((A)(B(C)))`((A)(B(C)))(A)(B(C))(C) group(1)... * matcher groupCount int matcher * group(0)group() groupCount | | | | ------------------------- | ------------------------------------------------------------ | | \| () | "" | | () () | (1)
(2)
(3) ,() 1 | | (?:Expression) | ( ) () ( ) | *** ##### \number * () () 1 * * **** * 1 ```java String regex = "((\d)3)\1[0-9](\w)\2{2}"; ``` * ((\d)3) \1 ((\d)3) \2 (\d) {2} \2 * 23238n22 2 2 * 43438n44 * 2 ```java String regex = "\w*?"; ``` ```java

x

//

x

//

x

// ``` *** ##### * * **** * **** | | | | -------- | --------------------------------------- | | (?=exp) | exp | | (?() List() / \ / \ HashSet() TreeSet() ArrayList() LinekdList() / LinkedHashSet() ``` **** * Set * HashSet * LinkedHashSet * TreeSet * List * ArrayList * LinekdList *** #### API Collection Collection * `public ArrayList(Collection
Web Proxy Viewer  |  New URL  |  Original Page