如果你已经有了C语言或任何一门编程语言的基础(面向对象的更好),那么学习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版)》——清华大学出版社