前言 以前我学java都是看看菜鸟教程就算了 那时候急功近利 而且因为用的少 每次学完都无一例外会忘记得一干二净 但是明年去华为肯定是需要使用java了 所以现在重新认真学习和使用java 下面是一些基础知识 比较浅显
因为每个小的知识点 我都是会在自己的示例代码里写很多注释 所以就不拓展来讲了
函数参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 package basics.parameters;public class immutable { public static void main (String[] args) { StringBuilder str = new StringBuilder("old" ); change(str, str); System.out.println(str); String string = new String("lala" ); changeStr(string); System.out.println("after changestr " + string); int num = 100 ; changeInt(num); System.out.println("after changeInt " + num); } public static void change (StringBuilder str0, StringBuilder str1) { str0.append("append0" ); str1.append("append1" ); str0 = new StringBuilder("new string builder" ); str0.append("new append" ); System.out.println("inside func " + str0); } public static void changeStr (String str) { str = "change str " ; System.out.println("str inside func " + str); } public static void changeInt (int num) { num = 10 ; System.out.println("inside func" + num); } }
输出
静态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package basics.staticBlock;class Parent { static { System.out.println("这是父类的静态代码块" ); } public Parent () { System.out.println("这是父类的构造函数" ); } } class Son extends Parent { static { System.out.println("这是子类的静态代码块" ); } public Son () { System.out.println("这是子类的构造函数" ); } public static void TestStaticFunc () { System.out.println("这是静态方法" ); } } public class staticBlock { public static void main (String[] args) { new Son(); new Son(); Son.TestStaticFunc(); } }
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package basics.staticBlock;public class StaticCode { static String prior = "done" ; static String last = f() ? g() : prior; public static boolean f () { System.out.println("这是f函数" ); return true ; } public static String g () { System.out.println("这是g函数" ); return "lala" ; } static { System.out.println("这是静态代码块" ); System.out.println(last); g(); } public static void main (String[] args) { } }
输出
重载 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package basics.overload;public class OverloadMethods { public void overloadTest () { System.out.println("这是测试函数" ); } public void overloadTest (int num) { System.out.println("这是重载的函数内部 传进来的参数为" + num); } public static void main (String[] args) { OverloadMethods overloadMethods = new OverloadMethods(); overloadMethods.overloadTest(); overloadMethods.overloadTest(12 ); } }
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package basics.overload;public class Priority { public void OverloadMethod (int num) { System.out.println("参数为int的方法" ); } public void OverloadMethod (Integer num) { System.out.println("参数为Integer的方法" ); } public static void main (String[] args) { Priority priority = new Priority(); priority.OverloadMethod(7 ); } }
输出
覆写 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package basics.override;class Father { protected void DoSomething () { System.out.println("这是父类函数" ); } } public class Son extends Father { @Override public void DoSomething () { System.out.println("这是子类覆写的函数" ); } public static void main (String[] args) { Father father = new Son(); father.DoSomething(); } }
输出
泛型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package basics.template;class Meat { @Override public String toString () { return "Meat" ; } } class Soup { @Override public String toString () { return "Soup" ; } } public class Stove { public static <T> void heat (T food) { System.out.println(food + " is done" ); } public static void main (String[] args) { Meat meat = new Meat(); Soup soup = new Soup(); Stove.heat(meat); Stove.heat(soup); } }
输出
包装数据类型的缓存 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package basics.boxing;public class cache { public static void main (String[] args) { Integer integer = new Integer(11 ); System.out.println("新创建的Integer类型" + integer); Integer integer1 = Integer.valueOf(11 ); System.out.println("新建的Integer类型" + integer1); } }
输出