【筆記】Java從入門到精通 | 基礎篇

若是你已經有了C語言或任何一門編程語言的基礎(面向對象的更好),那麼學習Java語言不難,特別是本文中的基礎內容。找本書花一兩天看看這部份內容,過一遍代碼,其實就掌握了Java語言的基礎,以後能夠進一步學習Java面向對象的精髓,而且修煉Java編程思想,才能算是入了門道。java

基礎篇分爲五個部分,第一部分「語言基礎」和第二部分「流程控制」在全部編程語言中都差很少,這裏再也不贅述,只是關於常量和變量(包括static/final)的一些概念,以及循環控制中的foreach用法,簡單提一下。正則表達式

package com.kplayer /*常量與變量*/
public class KPValue { static final double PI = 3.14; //靜態常量,必須且只能一次賦值,可跨類調用
    final double PI2 = 3.14; //實例常量,必須建立實例對象,分配空間後使用
    
    static int a = 10;  //靜態變量(類變量),屬於類,加載後便可使用
    int b = 20; //實例變量,必須建立實例對象,分配空間後使用
    
    public static void main(String[] args) { int c = 30; //局部變量,且不能聲明爲static //PI = 3.1415; //The final field KPValue.PI cannot be assigned //PI2 = 3.1415; //Cannot make a static reference to the non-static field PI2
        a = 100;  //靜態變量重賦值 //b = 200; //必須建立實例對象,分配空間後使用
        c = 300; System.out.println(a + "," + c); } }
/*Foreach語句*/
public class KPForeach { public static void main(String args[]) { int arr[] = { 1, 3, 5 }; for (int x : arr) { System.out.println(x); } } }

第三部分「字符串」和第四部分「數組」是Java中重要的兩個數據類型,須要掌握如何建立/初始化以及經常使用基本操做。數據庫

package com.kplayer

import java.util.Arrays;
import java.util.Date;

/*
字符串*/ public class KPString { public static void main(String args[]) { String s1 = new String("hello"); //構造方法建立 String s2 = "world "; //字符串常量引用賦值建立 String s3 = new String("a,b,c,d"); int year = 2018; //鏈接字符串 String conc = s1 + " " + s2 + year; //hello world 2018 System.out.println(conc); //獲取字符串信息 int size = s1.length(); //5 int indx = s1.indexOf("el"); //1 int indxlast = s1.lastIndexOf("l"); //3 char chr = s1.charAt(4); //o System.out.println(size + " " + indx + " " + indxlast + " " + chr); //字符串操做 String subs = s1.substring(0,2); //he String subs2 = s1.substring(2); //llo String trm = s2.trim(); //world String repl = s1.replace("h", "H"); //Hello boolean sta = s1.startsWith("he"); //true boolean end = s1.endsWith("O"); //false boolean equ = s1.equals(repl); //false boolean equ2 = s1.equalsIgnoreCase(repl); //true int com = s1.compareTo(s2); //-15 String low = s1.toLowerCase(); //hello String up = s1.toUpperCase(); //HELLO System.out.println(subs + " " + subs2 + " " + trm + " " + repl + " " + sta + " " + end + " " + equ + " " + equ2 + " " + com + " " + low + " " + up); String[] spl = s3.split(",",3); //a b c,d for(int i = 0; i < spl.length; i++){ System.out.println(spl[i]); } //格式化 Date date = new Date(); String dat = String.format("%tF", date); //2018-07-21 String time = String.format("%tc", date); //星期六 七月 21 09:42:44 CST 2018 System.out.println(dat + ", " + time); String formt = String.format("%d", 400 / 2); //200 String formb = String.format("%b", 3 > 5); //false String formx = String.format("%x", 200); //c8 System.out.println(formt + " " + formb + " " + formx); //正則表達式 String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}"; String str1 = "aaa@"; // 定義要進行驗證的字符串 String str2 = "aaaaa"; String str3 = "1111@111ffyu.dfg.com"; boolean match1 = str1.matches(regex); //false boolean match2 = str2.matches(regex); //false boolean match3 = str3.matches(regex); //true System.out.println(match1 + " " + match2 + " " + match3); //字符串生成器 StringBuilder builder = new StringBuilder(""); builder.append("Hello "); //Hello builder.insert(6, "world Kplayer"); //Hello world Kplayer builder.delete(11, 19); //Hello world System.out.println(builder); } }
package com.kplayer import java.util.Arrays; import java.util.Date; /*數組*/
public class KPArray { public static void main(String args[]) { //一維數組
        int arr[]; //聲明
        arr = new int[5]; //分配內存
        int arr2[] = new int[5]; //聲明時分配內存 
        int arr3[]=new int[]{1,2,3,4,5}; //初始化
        for (int i = 0; i < arr3.length; i++) { System.out.println(arr3[i]); } //二維數組
        int darr[][]; //聲明
        darr = new int[2][4]; //分配內存 //darr = new int[2][]; darr[0] = new int[2]; darr[1] = new int[3];
        int darr2[][] = new int[2][4]; //聲明時分配內存 
        int darr3[][] = {{11,22,33,44},{21,22,23,24}};  //初始化 //遍歷
        for (int i = 0; i < darr3.length; i++) { for (int j = 0; j < darr3[i].length; j++) { System.out.print(darr3[i][j] + " "); } System.out.println(); } for (int x[] : darr3) { for (int y : x) { System.out.print(y + " "); } System.out.println(); } //填充
        int arrf[] = new int[]{1,2,3,4,5}; Arrays.fill(arrf, 8); //8 8 8 8 8
        for (int i = 0; i < arrf.length; i++) { System.out.println(arrf[i]); } Arrays.fill(arrf, 1, 3, 7); //8 7 7 8 8
        for (int i = 0; i < arrf.length; i++) { System.out.println(arrf[i]); } //排序
        int arrs[] = new int[]{20,18,7,21}; Arrays.sort(arrs); //7 18 20 21
        for (int i = 0; i < arrs.length; i++) { System.out.println(arrs[i]); } //複製
        int arrc[] = new int[]{20,18,7,21}; int newarrc[] = Arrays.copyOf(arrc,5); //20 18 7 21 0
        for (int i = 0; i < newarrc.length; i++) { System.out.println(newarrc[i]); } int newarrc2[] = Arrays.copyOfRange(arrc,0,2); //20 18
        for (int i = 0; i < newarrc2.length; i++) { System.out.println(newarrc2[i]); } //查詢
        int arrb[] = new int[]{20,18,7,21}; Arrays.sort(arrb); //必須先排序
        int index = Arrays.binarySearch(arrb, 20); System.out.println(index); int index2 = Arrays.binarySearch(arrb, 1, 3, 20); System.out.println(index + " " + index2); } }

第五部分「類和對象」是Java區別於C等面向過程語言的重要部分,要理解「類」和「對象」的含義,在基礎篇中學會如何定義類和建立使用對象便可,繼承,多態,接口那些更精華的東西咱們放在「核心篇」中概括。編程

package com.kplayer import java.util.Arrays; import java.util.Date; /*類和對象*/
public class KPStudent { //類權限約束成員權限,Default爲包存取
    /*成員變量*/
    public String id;         //本類/同包類或子類/其它包類或子類
    protected String name;  //本類/同包類或子類
    private String sex;     //本類
    
    /*成員方法*/
    public String getId(){ return this.id; //this表明本類對象的引用
 } private void setId(String id){ this.id = id; //this代表第一個id是成員變量,區別於第二個id是形參
 } public KPStudent getStudent(){ return this; } /*靜態static *提供共享數據庫或方法*/
    static final String SCHOOL = "LYEZ"; //靜態常量
    static int age; //靜態變量
    public static int checkAge(int age){ //靜態方法,不可用this,不可直接調用非靜態方法
        if(age > 30 || age < 10){ System.out.println("Too Old/Young"); return -1; } return 0; } static{ System.out.println("Initialize..."); //類的初始化動做
 } /*構造方法 *實例化對象時自動調用 *未定義時自動建立一個無參構造方法 *若定義的構造方法均有參,則不自動建立無參構造方法*/
    public KPStudent(){ System.out.println("More Info is Better!"); } public KPStudent(String id, String name){ this.id = id; this.name = name; System.out.println("Create Stu Success!"); } /*主方法*/
    public static void main(String[] args){ KPStudent KPlayer = new KPStudent("1","KPlayer"); //建立對象 
        KPlayer.sex = "M"; //類成員變量賦值
        System.out.println(KPlayer.id + " " + KPlayer.name + " " + KPlayer.sex); KPStudent KPlayer2 = new KPStudent(); //建立對象
        KPlayer2.setId("2"); //調用方法
        KPlayer2.name = "KPlayer2"; KPlayer2.sex = "M"; KPlayer2.age = 45; //static連帶修改KPlayer對象的age,用KPStudent.age更恰當
        System.out.println(KPlayer2.age + " " + KPlayer.age); //45 45 //對象比較
        String s1 = new String("abc"); String s2 = new String("abc"); String s3 = s1; System.out.println(s2 == s3); //false
        System.out.println(s2.equals(s3)); //true
 } }

 

注:部分代碼來源於《Java從入門到精通(第3版)》——清華大學出版社數組

相關文章
相關標籤/搜索