注:本文有空会跟新;
讲述的是jdk7的内容;
注关于java 更详细的内容请进入:《Java 8编程官方参考教程(第9版).pdf》学习笔记 进行学习
第1章Java简介
1:为什么使用Java
1:java 诞生时C++太费电脑资源,不方便嵌入式移植
2:java 编写一次到处运行
2:什么是Java
java 是一门编程语言,移除了内存管理、安全性这类复杂的特性,转而将这些任务交给了虚拟机管理。
3:Java虚拟机
java编译器将java源程序编译成二进制“伪CPU指令构成”的字节码。Java虚拟机仿真了虚拟CPU,为java可执行文件提供了运行环境、字节码解释器、验证器。其中验证器用于内存
管理、线程管理等其他用途。JVM本质是能够运行Java可执行文件的机器。java编译文件以“.class”形式结尾。
4:Java特性
1:体积小
2:简单易学
3:面向对象
4:兼具编译与解释特性
5:与运行平台无关
6:执行安全
7:支出多线程
8:动态特性:内省(introspection) 反射(reflection)
5:ava的演变过程
1: 1996年1月23日):代号Oak
1: 提供了类库
2:提供applet互联网小程序
2:JDK1.1(1997年2月19日)
1:引入javaBean 组件。
2:引入 远程方法调用。让客户端调用运行在远程服务器的应用程序
3:jar文件格式
4:数字签名
5:AWT增强
6:其他改动
3:JDK1.2(1998年12月8日):代号Playground
1:引入Swing 图形编辑类库
2:提供 2D API
3: 实现拖拽功能
4:音频增强
5:引入 java平台上的ORB 对象请求代理实现类库
6:安全增强
7:其他增强
4:JDK1.3(2000年5月8日):代号Kestrel
5:JDK1.4(2002年2月6日):代号Merlin
6:JDK5.0(2004年9月30日):代号Tiger
7: JDKSE6(2006年12月11日):代号Mustang
8:JDKSE7(2011年7月7日):代号Dolphin
第2章:数组
、
1:数组介绍
1:作用:用来处理相同数据类型的变量集合
2:本质:相同数据类型的元素的集合,其中元算是数组的组成部分。数组中的每个元算可以使用唯一的
索引值(下标)
进行访问。
2:数组声明
1: 声明语法:
1 type arrayName();
或者是:
1 type[ ] arrayName ;
3:创建数组
1:示例:
1 int [ ] numberArray; //声明一个数组类型变量 numberArray ,其数组元素类型为int
2 numberArray= new int[20] ; //保存10个整数分配了连续内存,并将首个元算内存地址赋值给变量numberArray.数组初始化器为numberArray数组所有元算提供初始化值。
4:访问和修改数组元算
1 //访问数组元素:就是使用索引来访问数组元算。数组的每个元算都拥有唯一的索引值。数组元素可以使用数组名并跟上方括号内的索引值进行访问。
2
3 arrayName[indexValue];
4
5 //示例:
6 int[] num=new int[5];
7 int a1=num[0];//注意:下标是从0开始算数组总长度个数值的;如果下标不对则会出现 ArrayIndexOutOfBounds异常。
5:初始化数组
1:运行时初始化
1 //示例:
2 int[] num_Array=net int[10];
3 //赋值
4 num_Array[0]=0;
5 num_Array[1]=1;
6 num_Array[2]=2;
7 ...
8 num_Array[10]=10;
9 //如果值相同或则有规律可循。往往使用循环比较好用
10 for(int 1=0;i<10;i++){
11 num_Array[i]=i+1;
12 }
2: 使用数字字面量初始化
1 int[] num_Array={1,2,3,4,5,6,7,78} ;
1
2 import java.io.*;
3 /**
4 * 显示数组用法程序
5 *
6 **/
7 public class TestScoreAverage {
8
9 public static void main(String[] args) {
10 final int NUMBER_OF_STUDENTS = 5;
//final 关键字在程序中创建常量,变量被声明后值不可以再此被改变
11 int[] marks = new int[NUMBER_OF_STUDENTS];
12 try {
13 BufferedReader reader = new BufferedReader(new InputStreamReader( System.in));
15 for (int i = 0; i < NUMBER_OF_STUDENTS; i++) {
16 System.out.print("Enter marks for student #" + (i + 1) + ": ");
17 String str = reader.readLine();
18 marks[i] = Integer.parseInt(str);
19 }
20 } catch (Exception e) {
21 e.printStackTrace();
22 }
23 int total = 0;
24 for (int i = 0; i < NUMBER_OF_STUDENTS; i++) {
25 total += marks[i];
26 }
27 System.out.println("Average Marks " + (float) total
28 / NUMBER_OF_STUDENTS);
29 }
30 }
6:for_each 遍历循环数组 取值
1 //for_each循环可以在不使用元素索引值的情况下遍历整个数组
2
3 //这个是jdk5的新特性
4 for(type varableName :collection){
5 loopBody
6 }
7
具体示例如下:
1 int[] n ={1,2,3,4,5,6,7,7,7,67,6};
2 for(int m: n){
3 System.out.println(m);
4 }
5
7: 多维数组
即:指包含数组维数多于1维;可以创建二维、三维和n维数组(1<=n<=255)。
1:二维数组声明和初始化
1
2 //----------------------------------------------------------------------------------------
3 //二维数组:例如一个包含 行与列的表格。其中表格的每一个单元格代表一个数组元算。
4 //声明格式如下:
5 type arrayName[][];
6 type[] [] arrayname;
7
8 //具体示例如下:
9 final int n1=5; // 代表行
10 final int n2=10 ; //代表列
11
12 int[ ] [ ] m ;
13 m=new int[n1] [n2] ;
14
15 //其元素格值显示:
16 m[4][5]=5;
17
18 //-------初始化------------------------------------------------------------------------
19 //1:运行时初始化
20 arrayName[row] [col] =data;
21 //例如:
22 int [][] m =new int[5][10];'
23 //数组的单元格可以使用如下程序初始化
24 m[0][1]=1;
25 m[0][2]=2;
26 ....
27 m[5][10]=50
28
29
30 //2:循环初始化
31
32 final int r=5;c=10 ;
33 int [][] m=new int[r][c];
34 for(int i=o;i<r;i++){
35 for(int j=0;j<c;j++){
36 m[r][n]=i*j;
37 }
38 }
39
40 //使用数组字面量初始化
41 int [][] m={{1,1},{2,2},{3,3},{4,5}};
1
2
3 /**
4 * 展示使用二维数组的程序
5 *
6 **/
7 public class MultiDimArrayApp {
8
9 public static void main(String[] args) {
10 final int MAX_STUDENTS = 50, MAX_SUBJECTS = 3;
11 int[][] marks = new int[MAX_STUDENTS][MAX_SUBJECTS];
12 // Adding data to the array
13 for (int id = 0; id < MAX_STUDENTS; id++) {
14 for (int subject = 0; subject < MAX_SUBJECTS; subject++) {
15 marks[id][subject] = (int) (Math.random() * 100);
16 }
17 }
18 // Printing Array
19 System.out.print("Student\t");
20 for (int subject = 0; subject < MAX_SUBJECTS; subject++) {
21 System.out.print("\t" + "Subject " + subject + "\t");
22 }
23 System.out.println();
24 for (int id = 0; id < MAX_STUDENTS; id++) {
25 System.out.print("Student " + (id + 1) + '\t');
26 for (int subject = 0; subject < MAX_SUBJECTS; subject++) {
27 System.out.print("\t" + marks[id][subject] + "\t");
28 }
29 System.out.println();
30 }
31 }
32 }
2: 使用 for_each循环
1 int i=0;
2 for(int[] a:m){
3 System.out.println("a"+i+++'\t');
4 for(int v : a){
5 System.out.println("\t"+v+"\t");
6 }
7 System.out.println();
8 }
8:N维数组
1 //三维数组声明
2 type[][][] arrayName=new type[size][size][ size ] ;
3 //即:
4 int [][][]a=new int[1][2][3];
9:非矩形数组
10:确定长度
1 /**
2 * 确定数组长度
3 *
4 **/
5 public class ArrayLengthApp {
6
7 public static void main(String[] args) {
8 final int SIZE = 5;
9 int[] integerArray = new int[SIZE];
10 float[] floatArray = {5.0f, 3.0f, 2.0f, 1.5f};
11 String[] weekDays = {"Sunday", "Monday", "Tuesday",
12 "Wednesday", "Thursday", "Friday", "Saturday"};
13 int[][] jaggedArray = {
14 {5, 4},
15 {10, 15, 12, 15, 18},
16 {6, 9, 10},
17 {12, 5, 8, 11}
18 };
19 System.out.println("integerArray length: " + integerArray.length);
20 System.out.println("floatArray length: " + floatArray.length);
21 System.out.println("Number of days in a week: " + weekDays.length);
22 System.out.println("Length of jaggedArray: " + jaggedArray.length);
23 int row = 0;
24 for (int[] memberRow : jaggedArray) {
25 System.out.println("\tArray length for row "
26 + ++row + ": " + memberRow.length);
27 }
28 }
29 }
结果输出:
11: 数组复制
1
2 import java.util.Arrays;
3 /**
4 * 数组复制
5 *
6 **/
7 public class ArrayCopyApp {
8
9 public static void main(String[] args) {
10 float[] floatArray = {5.0f, 3.0f, 2.0f, 1.5f};
11 float[] floatArrayCopy =
floatArray.clone(); //此方法为数组的复制方法:把a数组的所有元算复制到b数组中
12 System.out.println(Arrays.toString(floatArray) + " - Original");
13 System.out.println(Arrays.toString(floatArrayCopy) + " - Copy");
14 System.out.println();
15 System.out.println("Modifying the second element of the original array");
16 floatArray[1] = 20;
17 System.out.println(Arrays.toString(floatArray)
18 + " - Original after modification");
19 System.out.println(Arrays.toString(floatArrayCopy) + " - Copy");
20 System.out.println();
21 System.out.println("Modifying the third element of the copy array");
22 floatArrayCopy[2] = 30;
23 System.out.println(Arrays.toString(floatArray) + " - Original");
24 System.out.println(Arrays.toString(floatArrayCopy)
25 + " - Copy array after modification");
26 }
27 }
执行后输出结果如下:
12:找出数组的类表示
1 /**
2 * 找出数组的类表示
3 *
4 **/
5 public class ArrayClassNameApp {
6
7 public static void main(String[] args) {
8 final int SIZE = 5;
9 int[] integerArray = new int[SIZE];
10 float[] floatArray = {5.0f, 3.0f, 2.0f, 1.5f};
11 String[] weekDays = {"Sunday", "Monday", "Tuesday",
12 "Wednesday", "Thursday", "Friday", "Saturday"};
13 int[][] jaggedArray = {
14 {5, 4},
15 {10, 15, 12, 15, 18},
16 {6, 9, 10},
17 {12, 5, 8, 11}
18 };
19 Class cls = integerArray.getClass();
20
21 System.out.println(
22 "The class name of integerArray: " +
cls.getName()
);
// arrayName.getClass()
23 cls = floatArray.getClass();
24
25 System.out.println(
26 "The class name of floatArray: " + cls.getName());
27 cls = weekDays.getClass();
28
29 System.out.println(
30 "The class name of weekDays: " + cls.getName());
31 cls = jaggedArray.getClass();
32
33 System.out.println(
34 "The class name of jaggedArray: " + cls.getName());
35 System.out.println();
36 cls = cls.getSuperclass();
37
38 System.out.println(
39 "The super class of an array object: "
40 + cls.getName());
41 }
42 }
第三章:类
1:面向对象概念
1:面向对象编程的特性
1:封装
2:继承
3:动态
2:面向对象编程的好处
2:类
1: 定义类
1 //定义类的一般形式如下:
2 Modifiers
可选
class ClassName{
3 classBody
可选
4 }
1 ClassModifieres
可选
class Identifier ClassBody
1 //class 关键字之前的修饰符用于定义类的可见性
2
3 class MyClass{
4 //attributes
5 // constructors
6 //methods
7
8 }
2: 定义Point 类
1 /*
2 *声明Point类
3 */
4
5 class Point{
6 int x;
7 int y;
8
9 }
10
11 /* 说明:
12 * 该类使用关键字class 并紧跟类名Point 定义而成。由于修饰符在类定义时是可以选的,因此并未
13 * 包含在上述的定义中。修饰符用于定义类的可见性。
14 * {} 中的内容表示类的主体。
15 */
3:使用类
1 Point p=new Point();
4: 访问/修改字段
5: 类的示例程序
1
2 class Point {
3
4 int x;
5 int y;
6 }
7
8 class TestPoint {
9
10 public static void main(String[] args) {
11 System.out.println("Creating a Point object ... ");
12 Point p = new Point();
13 System.out.println("Initializing data members ...");
14 p.x = 4;
15 p.y = 5;
16 System.out.println("Printing object");
17 System.out.println("Point p (" + p.x + ", " + p.y + ")");
18 }
19 }
6:方法声明
1
2 import java.util.*;
3
4 class Point {
5
6 int x;
7 int y;
8
9 double getDistance() { 10 return (Math.sqrt(x * x + y * y)); 11 }
12 }
13
14 class TestPoint {
15
16 public static void main(String[] args) {
17 System.out.println("Creating a Point object ... ");
18 Point p1 = new Point();
19 System.out.println("Initializing object ...");
20 p1.x = 3;
21 p1.y = 4;
22 double distance = p1.getDistance();
23 StringBuilder sb = new StringBuilder();
24 Formatter formatter = new Formatter(sb, Locale.US);
25 formatter.format("Distance of Point p1(" + p1.x + "," + p1.y
26 + ") from origin is %.02f", distance);
27 System.out.println(sb);
28 System.out.println();
29 sb.delete(0, sb.length());
30 System.out.println("Creating another Point object ... ");
31 Point p2 = new Point();
32 System.out.println("Initializing object ...");
33 p2.x = 8;
34 p2.y = 9;
35 distance = p2.getDistance();
36 formatter.format("Distance of Point p2(" + p2.x + ","
37 + p2.y + ") from origin is %.02f", distance);
38 System.out.println(sb);
39 }
40 }
当编译运行时结果如下:
7:对象内存表示
3:信息掩藏
1:展示信息掩藏概念的程序
1
2 class Wallet {
3
4 private float money;
5
6 public void setMoney(float money) {
7 this.money = money;
8 }
9
10 public boolean pullOutMoney(float amount) {
11
if (money >= amount) { 12 money -= amount; 13 return true; 14 } 15 return false;
16 }
17 }
18
19 class Person {
20
21 public static void main(String[] args) {
22 Wallet wallet = new Wallet();
23 System.out.println("Putting $500 in the wallet\n");
24 wallet.setMoney(500);
25 System.out.println("Pulling out $100 ...");
26 boolean isMoneyInWallet = wallet.pullOutMoney(100);
27 if (isMoneyInWallet) {
28 System.out.println("Got it!");
29 } else {
30 System.out.println("Nope, not enough money");
31 }
32 System.out.println("\nPulling out $300 ...");
33 isMoneyInWallet = wallet.pullOutMoney(300);
34 if (isMoneyInWallet) {
35 System.out.println("Got it!");
36 } else {
37 System.out.println("Nope, not enough money");
38 }
39 System.out.println("\nPulling out $200 ...");
40 isMoneyInWallet = wallet.pullOutMoney(200);
41 if (isMoneyInWallet) {
42 System.out.println("Got it!");
43 } else {
44 System.out.println("Nope, not enough money");
45 }
46 }
47 }
编译运行结果如下:
4:封装
1 class Date{
2
3
4 public int day;
5 public int mount;
6 public int year;
7
8 public void setMount(int month){
9 if(month >=1&& month <=12){
10 this.month=month;
11 }else{
12 System.out.println(month);
13 }
14
15 }
16
17 }