/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

学习笔记(一)--->《Java 8编程官方参考教程(第9版).pdf》:第一章到六章学习笔记


注:本文声明事项。

本博文整理者:刘军

本博文出自于: 《Java8 编程官方参考教程》一书

声明:1:转载请标注出处。本文不得作为商业活动。违者本人不负法律责任。违法者自负一切法律责任。

          2: 本书对应的jdk为 jdk8版本

          3:因为内容容量太大,编辑器无法承受于是给拆分了以下版本:

          《Java 8编程官方参考教程(第9版).pdf》学习笔记(一)--->第一章到六章学习笔记:讲:java的历史和演变、Java概述、数据类型、变量和数组、运算符、控制语句、类 等内容

         《Java 8编程官方参考教程(第9版).pdf》学习笔记(二)--->第七章到九章学习笔记:讲:java 方法、继承详解、包和接口







________________________________________________________________________________________________________________________________________________________________________________________________________________________

《Java 8编程官方参考教程(第9版)》pdf 下载地址:http://pan.baidu.com/s/1o7Zp3Mq 密码:     33cf     

____________________________________________________________________________________________________________

封面:

                        image

 

 

 

 

 

第1章 Java的历史和演变

1:计算机语言的创新和发展取决于以下2个条件:

  •   适应环境和用途的变化
  •   实现编程艺术的完善和提高

1.1 Java的家世


1.1.1 现代编程语言的诞生:C语言

   设计一门程序语言,需要权衡以下几点条件:

  •      易用性与功能
  •      安全性与效率
  •      稳定性与可扩展性


1.1.2 C++:下一个阶段


1.1.3 Java出现的时机已经成熟

   万维网的迅猛发展,促使了java告诉的发展。

1.2 Java的诞生

    在c、c++种种局限性的前提下,因一个项目的开发偶然机会造就了java语言的诞生,在sun 公司的大力人力的投入,及其的开源特性,吸引了居多的程序员参与其中,并为其发展的提高了更高的速度。因其本身的一些良好特性,得到广大开发者的爱好。

使其得到广泛的应用。

1.3 Java改变Internet的方式


1.3.1 Java

image


1.3.2 安全性

image


1.3.3 可移植性

image

image

1.4 Java的魔力:字节码

image

image


1.5 servlet:服务器端的

image



1.6 Java的关键特性

       java关键特性有如下这些:

       image

    

1.6.1 简单性

Java  设计的目标之一是让专业程序员能够高效地学习和使用。

1.6.2 面向对象

任何事物都是对象

1.6.3 健壮性

1:开发阶段对代码的正确性进行了检查,如果有错误则提醒程序员处理掉不必要的 编译时异常。

2:成功规避了“内存管理错误和未处理异常(即:运行时异常)”


1.6.4 多线程

Java设计目标之一:满足对创建交互式、网络化程序的实现需求。对于此要求,java支出多线程,允许编写同时执行许多工作的程序。java运行时系统为多线程同步提供了优美且完善的解决方案,能够创造出运行平稳的交互式系统。

1.6.5 体系结构中立

对于开发人员来讲:核心问题就是:代码的持久性和可移植性。因此,对于java语言的设计目标:编写一次,无论何时,何地都能永运运行。

1.6.6 解释执行和高性能

image

1.6.7 分布式

java 是针对internet的分布式环境而设计的。因为java能处理TCP/IP协议。实际:使用URL访问资源与访问文件并没有多大区别。

1.6.8 动态性

image

1.7 Java的演变历程

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

  • 主要是Java2原始版本第一次重要升级,主要更新java的现有功能以及限制开发环境。

5:JDK1.4(2002年2月6日):代号Merlin

  •    添加新关键字 assert、链式异常 、基于通道I/O子系统。
  •   修改了集合框架和联网类(networking class).

6:JDK5.0(2004年9月30日):代号Tiger

  • 泛型
  • 注解
  • 自动装箱和自动拆箱
  • 枚举
  • 增强的for-each 风格的for循环
  • 可变长度参数
  • 静态导入
  • 格式化的I/O

7: JDKSE6(2006年12月11日):代号Mustang

  • 增强了api库并改进了运行时。

8:JDKSE7(2011年7月7日):代号Dolphin

  • 升级了Java语言重要特性和API库(最重要的2个方面是对NIO框架(表示新I/O)进行了增强并且增加了Fork/Join框架(该框架对并行编程提供了重要支持))
  • 对java运行时系统进行升级,包括对非java语言的支持。
  • 新增语言特性作为Project Coin 的一部分开发的。

  新增语言特性的列表:

  • String 现在能够控制switch语句
  • 二进制整形字面值
  • 数值字面值中的下划线
  • 扩展的try语句,称为带资源的try(try-with-resources)语句,这种try语句支持自动资源管理,当流(stream)不再需要时,现在能够自动关闭它们。
  • 构造泛型实例时的类型推断(借助菱形运算符<>)
  • 对异常处理进行了增强,单个catch子句能够捕获2个或者更多个异常(multi-catch),并且对重新抛出的异常提供了更好的类型检查。
  • 对与某些方法(参数的长度可变)类型关联的编译器警告进行了改进,监管语法没有发生变化,并且对i警告具有粳稻的控制权

image






1.8 Java SE8

   java最新发布的版本是Java SE8.对应的Java开发工具包称为JDK8。内部版本号为1.8.

  主要升级内容有:

  •     lambda表达式。
  •     新增 最新的 流API。
  •    新增java.util.function 。


1.9 文化革新



第2章 Java综述

image

2.1 面向对象编程: oop  (Object Oriented Programming) 在Java中处于核心地位。

2.1.1 两种范式

1:计算机程序包含2大元素:代码和数据

  • 面向过程模型:  程序围绕代码中“正在发生什么”进行编写的。
  • 面向对象编程:  程序围绕数据(对象)进行“将要影响谁”进行编写的。其特点为:数据控制对代码的访问。

2.1.2 抽象

  • 面向对象编程的本质元素之一:抽象。通过抽象管理复杂性。
  • 使用层次化分类是管理抽象的一种强有力方式。其允许对复杂系统的语义进行分层,将他们分解为更多个更容易管理的部分。
  • 复杂系统的层次化抽象可以应用于计算机程序。
  • 面向对象编程是一种强大且自然的范式。其生命周期为:概念提出、成长、衰老。

2.1.3 OOP三原则

面向对象编程语言都提供了用于帮助实现面向对象模型的机制:封装(encapsulation)、继承(inheritance)、多态(polymorphism).

  • 封装
  1.    封装是将代码及其操作的数据绑定到一起的机制,并且保证代码和数据既不会受到外部干扰,也不会被误用。外部的调用则需要通过其接口方法进行操作即可,不必关注其方法内部实现的逻辑。
  2.    在java中,封装的基础是类。类定义了一组对象共享的结构和行为(数据)。给定类的每个对象都包含该类定义的结构和行为。就好像他们从同一个类的模子中出来的。
  3.    类是一种逻辑结构,而对象是物理实体。有时将对象称作类的实例。
  4.    当创建类的时候,需要指定构成类的代码和数据。即:其元素(类的成员)和 类定义的数据(类的成员变量或实例变量)。其操作数据的代码称为成员方法(即:方法)。方法定义了使用成员变量的方式。   即:类的行为和接口是由实例数据的方法定义的。
  5.   封装的目的是简化类的复杂性。类中的每个方法或变量可以被标识为私有的或公有的。 类的公有接口(public)表示类的外部用户需要知道或可以知道的所有内容。私有(private)方法和数据只能有类的成员代码访问,所有不是类成员的其他代码都不能访问私有的方法或变量。
  • 继承
  1.      继承是一个对象获得另外一个对象的属性的过程。此特性支持层次分类的概念。通过层次划分类(即从上到下),每个对象只需要定义自己在所属的类中独有的那些属性,可以从父类继承通用的属性。即:继承机制使得对象成为更一般情况的特殊实例称为可能。
  • 多态
  1.  多态是允许将一个接口用于一类通用动作的特性。具体使用哪个动作与应用场合有关。得需要考虑堆栈(一种后进先出的数据结构)可能有一个程序需要 整数值、浮点值、字符等类型的堆栈。尽管储存的数据不同,而实现堆栈的算法是相同的情况。 
  2. 一般情况是:动态指“一个接口,多种方法”。
  • 多态、封装、继承协同工作


2.2第一个简单程序


  1 /*
  2    This is a simple Java program.
  3    Call this file "Example.java".
  4 */
  5 class Example {
  6   // Your program begins with a call to main().
  7   public static void main(String args[]) {
  8     System.out.println("This is a simple Java program.");
  9   }
 10 }
 11 

2.2.1 输入程序

image

image

2.2.2 编译程序

编译example.java 程序,需要执行编译器javac,在命令行上指定源文件的名称。

  1 javac example.java

image

image

image

image




2.2.3 深入分析第一个示例程序

image

image

image

image

image

image

image

image


2.3第二个简短程序

image

  1 /*
  2    Here is another short example.
  3    Call this file "Example2.java".
  4 */
  5 class Example2 {
  6   public static void main(String args[]) {
  7     int num; // this declares a variable called num
  8 
  9     num = 100; // this assigns num the value 100
 10 
 11     System.out.println("This is num: " + num);
 12 
 13     num = num * 2;
 14 
 15     System.out.print("The value of num * 2 is ");
 16     System.out.println(num);
 17   }
 18 }

image

image

image




2.4 两种控制语句

2.4.1 if语句

image

image

  1 /*
  2   Demonstrate the if.
  3 
  4   Call this file "IfSample.java".
  5 */
  6 class IfSample {
  7   public static void main(String args[]) {
  8     int x, y;
  9 
 10     x = 10;
 11     y = 20;
 12 
 13     if(x < y) System.out.println("x is less than y");
 14 
 15     x = x * 2;
 16     if(x == y) System.out.println("x now equal to y");
 17 
 18     x = x * 2;
 19     if(x > y) System.out.println("x now greater than y");
 20 
 21     // this won't display anything
 22     if(x == y) System.out.println("you won't see this");
 23   }
 24 }
 25 

image


2.4.2 for循环

image

  1 /*
  2   Demonstrate the for loop.
  3 
  4   Call this file "ForTest.java".
  5 */
  6 class ForTest {
  7   public static void main(String args[]) {
  8     int x;
  9 
 10     for(x = 0; x<10; x = x+1)
 11       System.out.println("This is x: " + x);
 12   }
 13 }
 14 

image

  image

image


2.5 使用代码块

image

  1 /*
  2   Demonstrate a block of code.
  3 
  4   Call this file "BlockTest.java"
  5 */
  6 class BlockTest {
  7   public static void main(String args[]) {
  8     int x, y;
  9 
 10     y = 20;
 11 
 12     // the target of this loop is a block
 13     for(x = 0; x<10; x++) {
 14       System.out.println("This is x: " + x);
 15       System.out.println("This is y: " + y);
 16       y = y - 2;
 17     }
 18   }
 19 }

image

  1 This is x: 0
  2 This is y: 20
  3 This is x: 1
  4 This is y: 18
  5 This is x: 2
  6 This is y: 16
  7 This is x: 3
  8 This is y: 14
  9 This is x: 4
 10 This is y: 12
 11 This is x: 5
 12 This is y: 10
 13 This is x: 6
 14 This is y: 8
 15 This is x: 7
 16 This is y: 6
 17 This is x: 8
 18 This is y: 4
 19 This is x: 9
 20 This is y: 2
 21 

image

2.6 词汇问题

2.6.1 空白符

image

2.6.2 标识符

image

2.6.3 字面值

image

2.6.4 注释

image

2.6.5 分隔符

image

image

2.6.6 Java关键字

image

image

image

image

2.7 Java类库

image


第3章 数据类型、变量和数组

3.1 Java是强类型化的语言

imageimage

3.2 基本类型

image

3.3 整型

image

image

image

3.3.1 : byte

image


3.3.2  :  short

image

3.3.  int

image

3.3.4  : long

image

  1 package Chap3;
  2 
  3 //Compute distance light travels using long variables.
  4 class Light {
  5 	public static void main(String args[]) {
  6 		int lightspeed;
  7 		long days;
  8 		long seconds;
  9 		long distance;
 10 
 11 		// approximate speed of light in miles per second
 12 		lightspeed = 186000;
 13 
 14 		days = 1000; // specify number of days here
 15 
 16 		seconds = days * 24 * 60 * 60; // convert to seconds
 17 
 18 		distance = lightspeed * seconds; // compute distance
 19 
 20 		System.out.print("In " + days);
 21 		System.out.print(" days light will travel about ");
 22 		System.out.println(distance + " miles.");
 23 	}
 24 }
 25 
  1 //这个程序产生的输出如下所示:
  2 In 1000 days light will travel about 16070400000000 miles.
  3 
  4 //显然,int 变量无法保存这么大的结果


3.4 浮点型

image

image


3.4.1  float

image

image



3.4.2

image

  1 package Chap3;
  2 
  3 //Compute the area of a circle.
  4 class Area {
  5 	public static void main(String args[]) {
  6 		double pi, r, a;
  7 
  8 		r = 10.8; // radius of circle
  9 		pi = 3.1416; // pi, approximately
 10 		a = pi * r * r; // compute area
 11 
 12 		System.out.println("Area of circle is " + a);
 13 	}
 14 }
 15 
 16 
 17 //运行结果如下:
 18 Area of circle is 366.436224
 19 


3.5 字符型

在java 中,用于存储字符的数据类型时char.java使用 unicode表示字符。因此,java中的char时16位类型。char 的范围为0~~65 536 ;char 没有负值;

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: CharDemo
  6  * @Description: Demonstrate char data type.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午9:20:35
  9  *
 10  */
 11 class CharDemo {
 12 	public static void main(String args[]) {
 13 		char ch1, ch2;
 14 
 15 		ch1 = 88; // code for X
 16 		ch2 = 'Y';
 17 
 18 		System.out.print("ch1 and ch2: ");
 19 		System.out.println(ch1 + "" + ch2);
 20 	}
 21 }
 22 
 23 
 24 //其运行结果为:
 25 ch1 and ch2: X Y
 26 

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5 * @ClassName: CharDemo2
  6 * @Description: char variables behave like integers.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月12日 下午9:23:44
  9 *
 10  */
 11 class CharDemo2 {
 12 	public static void main(String args[]) {
 13 		char ch1;
 14 
 15 		ch1 = 'X';
 16 		System.out.println("ch1 contains " + ch1);
 17 
 18 		ch1++; // increment ch1
 19 		System.out.println("ch1 is now " + ch1);
 20 	}
 21 }
 22 
 23 //其运行结果为:
 24 
ch1 contains X
 25 
ch1 is now Y
 26 

image

3.6 布尔型

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: BoolTest
  6  * @Description: Demonstrate boolean values.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午9:26:44
  9  *
 10  */
 11 class BoolTest {
 12 	public static void main(String args[]) {
 13 		boolean b;
 14 
 15 		b = false;
 16 		System.out.println("b is " + b);
 17 		b = true;
 18 		System.out.println("b is " + b);
 19 
 20 		// a boolean value can control the if statement
 21 		if (b){
 22 			System.out.println("This is executed.");
 23 		}
 24 		b = false;
 25 		if (b){
 26 			System.out.println("This is not executed.");
 27 		}
 28 		// outcome of a relational operator is a boolean value
 29 		System.out.println("10 > 9 is " + (10 > 9));
 30 	}
 31 }
 32 
 33 //其运行结果如下:
 34 b is false
 35 b is true
 36 This is executed.
 37 10 > 9 is true
 38 

image

3.7 深入分析字面值

3.7.1 整型字面值

image

image

image



3.7.2 浮点型字面值

image

image

3.7.3 布尔型字面值

image

3.7.4 字符型字面值

image

image

image

3.7.5 字符串字面值

image



3.8 变量

image

3.8.1 变量的声明

  1 //在Java,所有变量在使用之前必须声明。声明变量的基本形式如下所示:
  2 type identifier [ = valuie ] [, identifier [ = value] ...] ;

image

3.8.2 动态初始化

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: DynInit
  6  * @Description: Demonstrate dynamic initialization.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午9:41:29
  9  *
 10  */
 11 class DynInit {
 12 	public static void main(String args[]) {
 13 		double a = 3.0, b = 4.0;
 14 
 15 		// c is dynamically initialized
 16 		double c = Math.sqrt(a * a + b * b);
 17 
 18 		System.out.println("Hypotenuse is " + c);
 19 	}
 20 }
 21 
 22 //其运行结果如下
 23 Hypotenuse is 5.0
 24 

image

image

3.8.3 变量的作用域和生存期

image

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: Scope
  6  * @Description: Demonstrate block scope.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午9:46:26
  9  *
 10  */
 11 class Scope {
 12 	public static void main(String args[]) {
 13 		int x; // known to all code within main
 14 
 15 		x = 10;
 16 		if (x == 10) { // start new scope
 17 			int y = 20; // known only to this block
 18 
 19 			// x and y both known here.
 20 			System.out.println("x and y: " + x + "" + y);
 21 			x = y * 2;
 22 		}
 23 		// y = 100; // Error! y not known here
 24 
 25 		// x is still known here.
 26 		System.out.println("x is " + x);
 27 	}
 28 }
 29 
 30 //其运行结果为:
 31 x and y: 10 20
 32 x is 40
 33 
 34 

image

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: LifeTime
  6  * @Description: Demonstrate lifetime of a variable.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午9:49:17
  9  *
 10  */
 11 class LifeTime {
 12 	public static void main(String args[]) {
 13 		int x;
 14 
 15 		for (x = 0; x < 3; x++) {
 16 			int y = -1; // y is initialized each time block is entered
 17 			System.out.println("y iz: " + y); // this always prints -1
 18 			y = 100;
 19 			System.out.println("y is now: " + y);
 20 		}
 21 	}
 22 }
 23 
 24 //其运行结果为:
 25 y iz: -1
 26 y is now: 100
 27 y iz: -1
 28 y is now: 100
 29 y iz: -1
 30 y is now: 100
 31 

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: ScopeErr
  6  * @Description:This program will not compile
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午9:51:10
  9  *
 10  */
 11 class ScopeErr {
 12 	public static void main(String args[]) {
 13 		int bar = 1;
 14 		{ // creates a new scope
 15 			int bar = 2; // Compile time error -- bar already defined!
 16 		}
 17 	}
 18 }

image

3.9 类型转换和强制类型转换

image

3.9.1 Java的自动类型转换

image

3.9.2 强制转换不兼容的类型

image

image

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5 * @ClassName: Conversion
  6 * @Description: Demonstrate casts.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月12日 下午9:57:42
  9 *
 10  */
 11 class Conversion {
 12 	public static void main(String args[]) {
 13 		byte b;
 14 		int i = 257;
 15 		double d = 323.142;
 16 
 17 		System.out.println("\nConversion of int to byte.");
 18 		b = (byte) i;
 19 		System.out.println("i and b " + i + "" + b);
 20 
 21 		System.out.println("\nConversion of double to int.");
 22 		i = (int) d;
 23 		System.out.println("d and i " + d + "" + i);
 24 
 25 		System.out.println("\nConversion of double to byte.");
 26 		b = (byte) d;
 27 		System.out.println("d and b " + d + "" + b);
 28 	}
 29 }
 30 
 31 
 32 //其结果为:
 33 
 34 
 35 Conversion of int to byte.
 36 i and b 257 1
 37 
 38 Conversion of double to int.
 39 d and i 323.142 323
 40 
 41 Conversion of double to byte.
 42 d and b 323.142 67
 43 
 44 

image

3.10 表达式中的自动类型提升

image

image

类型提升规则:

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: Promote
  6  * @Description: 类型提升规则
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午10:09:00
  9  *
 10  */
 11 class Promote {
 12 	public static void main(String args[]) {
 13 		byte b = 42;
 14 		char c = 'a';
 15 		short s = 1024;
 16 		int i = 50000;
 17 		float f = 5.67f;
 18 		double d = .1234;
 19 		double result = (f * b) + (i / c) - (d * s);
 20 		System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
 21 		System.out.println("result = " + result);
 22 	}
 23 }
 24 //其运行结果为:
 25 238.14 + 515 - 126.3616
 26 result = 626.7784146484375

image


3.11 数组

image

3.11.1 一维数组

image

image

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5 * @ClassName: Array
  6 * @Description: /Demonstrate a one-dimensional array.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月12日 下午11:37:38
  9 *
 10  */
 11 class Array {
 12 	public static void main(String args[]) {
 13 		int month_days[];
 14 		month_days = new int[12];
 15 		month_days[0] = 31;
 16 		month_days[1] = 28;
 17 		month_days[2] = 31;
 18 		month_days[3] = 30;
 19 		month_days[4] = 31;
 20 		month_days[5] = 30;
 21 		month_days[6] = 31;
 22 		month_days[7] = 31;
 23 		month_days[8] = 30;
 24 		month_days[9] = 31;
 25 		month_days[10] = 30;
 26 		month_days[11] = 31;
 27 		System.out.println("April has " + month_days[3] + " days.");
 28 	}
 29 }
 30 //其运行结果如下:
 31 April has 30 days.
 32 

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: AutoArray
  6  * @Description: An improvied version of the previous program.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午11:39:33
  9  *
 10  */
 11 class AutoArray {
 12 	public static void main(String args[]) {
 13 		int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 14 		System.out.println("April has " + month_days[3] + " days.");
 15 	}
 16 }
 17 
 18 //其运行结果为:
 19 April has 30 days.
 20 

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: Average
  6  * @Description: Average an array of values.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午11:41:28
  9  *
 10  */
 11 class Average {
 12 	public static void main(String args[]) {
 13 		double nums[] = { 10.1, 11.2, 12.3, 13.4, 14.5 };
 14 		double result = 0;
 15 		int i;
 16 
 17 		for (i = 0; i < 5; i++)
 18 			result = result + nums[i];
 19 
 20 		System.out.println("Average is " + result / 5);
 21 	}
 22 }
 23 
 24 //其 运行结果为:
 25 Average is 12.299999999999999
 26 


3.11.2 多维数组

image

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: TwoDArray
  6  * @Description: Demonstrate a two-dimensional array.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午11:45:02
  9  *
 10  */
 11 class TwoDArray {
 12 	public static void main(String args[]) {
 13 		int twoD[][] = new int[4][5];
 14 		int i, j, k = 0;
 15 
 16 		for (i = 0; i < 4; i++) {
 17 			for (j = 0; j < 5; j++) {
 18 				twoD[i][j] = k;
 19 				k++;
 20 			}
 21 		}
 22 		for (i = 0; i < 4; i++) {
 23 			for (j = 0; j < 5; j++)
 24 				System.out.print(twoD[i][j] + "");
 25 			System.out.println();
 26 		}
 27 	}
 28 }
 29 
 30 
 31 //其运行结果为:
 32 
 33 0 1 2 3 4
 34 5 6 7 8 9
 35 10 11 12 13 14
 36 15 16 17 18 19
 37 

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: TwoDAgain
  6  * @Description: Manually allocate differing size second dimensions.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午11:48:43
  9  *
 10  */
 11 class TwoDAgain {
 12 	public static void main(String args[]) {
 13 		int twoD[][] = new int[4][];
 14 		twoD[0] = new int[1];
 15 		twoD[1] = new int[2];
 16 		twoD[2] = new int[3];
 17 		twoD[3] = new int[4];
 18 
 19 		int i, j, k = 0;
 20 
 21 		for (i = 0; i < 4; i++){
 22 			for (j = 0; j < i + 1; j++) {
 23 				twoD[i][j] = k;
 24 				k++;
 25 			}
 26 		}
 27 		for (i = 0; i < 4; i++) {
 28 			for (j = 0; j < i + 1; j++)
 29 				System.out.print(twoD[i][j] + "");
 30 			System.out.println();
 31 		}
 32 	}
 33 }
 34 
 35 //其运行结果为:
 36 0
 37 1 2
 38 3 4 5
 39 6 7 8 9
 40 

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: Matrix
  6  * @Description: Initialize a two-dimensional array.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午11:52:27
  9  *
 10  */
 11 class Matrix {
 12 	public static void main(String args[]) {
 13 		double m[][] =
 14 			{ 	{ 0 * 0, 1 * 0, 2 * 0, 3 * 0 },
 15 				{ 0 * 1, 1 * 1, 2 * 1, 3 * 1 },
 16 				{ 0 * 2, 1 * 2, 2 * 2, 3 * 2 },
 17 				{ 0 * 3, 1 * 3, 2 * 3, 3 * 3 }
 18 				};
 19 		int i, j;
 20 
 21 		for (i = 0; i < 4; i++) {
 22 			for (j = 0; j < 4; j++)
 23 				System.out.print(m[i][j] + "");
 24 			System.out.println();
 25 		}
 26 	}
 27 }
 28 
 29 //其运行结果为:
 30 0.0 0.0 0.0 0.0
 31 0.0 1.0 2.0 3.0
 32 0.0 2.0 4.0 6.0
 33 0.0 3.0 6.0 9.0
 34 

image

  1 package Chap3;
  2 
  3 /**
  4  *
  5  * @ClassName: ThreeDMatrix
  6  * @Description: Demonstrate a three-dimensional array.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月12日 下午11:54:52
  9  *
 10  */
 11 class ThreeDMatrix {
 12 	public static void main(String args[]) {
 13 		int threeD[][][] = new int[3][4][5];
 14 		int i, j, k;
 15 
 16 		for (i = 0; i < 3; i++){
 17 			for (j = 0; j < 4; j++)
 18 				for (k = 0; k < 5; k++)
 19 					threeD[i][j][k] = i * j * k;
 20 		}
 21 
 22 		for (i = 0; i < 3; i++) {
 23 			for (j = 0; j < 4; j++) {
 24 				for (k = 0; k < 5; k++)
 25 					System.out.print(threeD[i][j][k] + "");
 26 				System.out.println();
 27 			}
 28 			System.out.println();
 29 		}
 30 	}
 31 }
 32 //其运行结果为:
 33 0 0 0 0 0
 34 0 0 0 0 0
 35 0 0 0 0 0
 36 0 0 0 0 0
 37 
 38 0 0 0 0 0
 39 0 1 2 3 4
 40 0 2 4 6 8
 41 0 3 6 9 12
 42 
 43 0 0 0 0 0
 44 0 2 4 6 8
 45 0 4 8 12 16
 46 0 6 12 18 24
 47 
 48 


3.11.3 另一种数组声明语法

image


3.12 关于字符串的一些说明

image

image


3.13 向C/C++程序员提供指针方面的说明

image


第4章 运算符

image

4.1 算术运算符

  image

image

image

4.1.1 基本算术运算符

image

  1 package Chap4;
  2 
  3 /**
  4  *
  5 * @ClassName: BasicMath
  6 * @Description: Demonstrate the basic arithmetic operators.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月13日 上午12:04:44
  9 *
 10  */
 11 class BasicMath {
 12 	public static void main(String args[]) {
 13 		// arithmetic using integers
 14 		System.out.println("Integer Arithmetic");
 15 		int a = 1 + 1;
 16 		int b = a * 3;
 17 		int c = b / 4;
 18 		int d = c - a;
 19 		int e = -d;
 20 		System.out.println("a = " + a);
 21 		System.out.println("b = " + b);
 22 		System.out.println("c = " + c);
 23 		System.out.println("d = " + d);
 24 		System.out.println("e = " + e);
 25 
 26 		// arithmetic using doubles
 27 		System.out.println("\nFloating Point Arithmetic");
 28 		double da = 1 + 1;
 29 		double db = da * 3;
 30 		double dc = db / 4;
 31 		double dd = dc - a;
 32 		double de = -dd;
 33 		System.out.println("da = " + da);
 34 		System.out.println("db = " + db);
 35 		System.out.println("dc = " + dc);
 36 		System.out.println("dd = " + dd);
 37 		System.out.println("de = " + de);
 38 	}
 39 }
 40 //其运行结果为:
 41 Integer Arithmetic
 42 a = 2
 43 b = 6
 44 c = 1
 45 d = -1
 46 e = 1
 47 
 48 Floating Point Arithmetic
 49 da = 2.0
 50 db = 6.0
 51 dc = 1.5
 52 dd = -0.5
 53 de = 0.5
 54 


4.1.2 求模运算符

image

  1 package Chap4;
  2 
  3 /**
  4  *
  5  * @ClassName: Modulus
  6  * @Description: Demonstrate the % operator.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 上午12:07:05
  9  *
 10  */
 11 class Modulus {
 12 	public static void main(String args[]) {
 13 		int x = 42;
 14 		double y = 42.25;
 15 
 16 		System.out.println("x mod 10 = " + x % 10);
 17 		System.out.println("y mod 10 = " + y % 10);
 18 	}
 19 }
 20 
 21 //其运行结果为:’
 22 x mod 10 = 2
 23 y mod 10 = 2.25
 24 

4.1.3 算术与赋值复合运算符

image

image

  1 package Chap4;
  2 
  3 /**
  4  *
  5  * @ClassName: OpEquals
  6  * @Description: Demonstrate several assignment operators.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 上午12:10:14
  9  *
 10  */
 11 class OpEquals {
 12 	public static void main(String args[]) {
 13 		int a = 1;
 14 		int b = 2;
 15 		int c = 3;
 16 
 17 		a += 5;
 18 		b *= 4;
 19 		c += a * b;
 20 		c %= 6;
 21 		System.out.println("a = " + a);
 22 		System.out.println("b = " + b);
 23 		System.out.println("c = " + c);
 24 	}
 25 }
 26 
 27 //其运行结果为:
 28 a = 6
 29 b = 8
 30 c = 3
 31 


4.1.4 自增与自减运算符

image

image

image

  1 package Chap4;
  2 
  3 /**
  4  *
  5  * @ClassName: IncDec
  6  * @Description: Demonstrate ++ and --.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 上午12:13:06
  9  *
 10  */
 11 class IncDec {
 12 	public static void main(String args[]) {
 13 		int a = 1;
 14 		int b = 2;
 15 		int c;
 16 		int d;
 17 
 18 		c = ++b;
 19 		d = a++;
 20 		c++;
 21 		System.out.println("a = " + a);
 22 		System.out.println("b = " + b);
 23 		System.out.println("c = " + c);
 24 		System.out.println("d = " + d);
 25 	}
 26 }
 27 
 28 
 29 //其运行结果为:
 30 a = 2
 31 b = 3
 32 c = 4
 33 d = 1
 34 


4.2 位运算符

image

image

image

image

image

4.2.1 位逻辑运算符

image

image

image

image

image

  1 package Chap4;
  2 
  3 /**
  4  *
  5  * @ClassName: BitLogic
  6  * @Description: Demonstrate the bitwise logical operators.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 上午12:23:31
  9  *
 10  */
 11 class BitLogic {
 12 	public static void main(String args[]) {
 13 		String binary[] =
 14 			{   "0000", "0001", "0010", "0011", "0100", "0101",
 15 			    "0110", "0111", "1000", "1001", "1010",
 16 				"1011", "1100", "1101", "1110", "1111" };
 17 		int a = 3; // 0 + 2 + 1 or 0011 in binary
 18 		int b = 6; // 4 + 2 + 0 or 0110 in binary
 19 		int c = a | b;
 20 		int d = a & b;
 21 		int e = a ^ b;
 22 		int f = (~a & b) | (a & ~b);
 23 		int g = ~a & 0x0f;
 24 
 25 		System.out.println("        a = " + binary[a]);
 26 		System.out.println("        b = " + binary[b]);
 27 		System.out.println("      a|b = " + binary[c]);
 28 		System.out.println("      a&b = " + binary[d]);
 29 		System.out.println("      a^b = " + binary[e]);
 30 		System.out.println("~a&b|a&~b = " + binary[f]);
 31 		System.out.println("       ~a = " + binary[g]);
 32 	}
 33 }
 34 
 35 //其运行结果为:
 36         a = 0011
 37         b = 0110
 38       a|b = 0111
 39       a&b = 0010
 40       a^b = 0101
 41 ~a&b|a&~b = 0101
 42        ~a = 1100
 43 

image


4.2.2 左移

image

  1 package Chap4;
  2 
  3 //Left shifting a byte value.
  4 class ByteShift {
  5 	public static void main(String args[]) {
  6 		byte a = 64, b;
  7 		int i;
  8 
  9 		i = a << 2;
 10 		b = (byte) (a << 2);
 11 
 12 		System.out.println("Original value of a: " + a);
 13 		System.out.println("i and b: " + i + "" + b);
 14 	}
 15 }
 16 
 17 //其运行结果为:
 18 Original value of a: 64
 19 i and b: 256 0
 20 

image

  1 package Chap4;
  2 
  3 /**
  4  *
  5  * @ClassName: MultByTwo
  6  * @Description: Left shifting as a quick way to multiply by 2.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 上午12:29:49
  9  *
 10  */
 11 class MultByTwo {
 12 	public static void main(String args[]) {
 13 		int i;
 14 		int num = 0xFFFFFFE;
 15 
 16 		for (i = 0; i < 4; i++) {
 17 			num = num << 1;
 18 			System.out.println(num);
 19 		}
 20 	}
 21 }
 22 
 23 //其运行结果为:
 24 536870908
 25 1073741816
 26 2147483632
 27 -32
 28 

image

4.2.3 右移

image

image

image

  1 package Chap4;
  2 
  3 //Masking sign extension.
  4 class HexByte {
  5 	static public void main(String args[]) {
  6 		char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  7 		byte b = (byte) 0xf1;
  8 
  9 		System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
 10 	}
 11 }
 12 //其运行结果为:
 13 b = 0xf1
 14 

4.2.4 无符号右移

image

image

  1 package Chap4;
  2 
  3 //Unsigned shifting a byte value.
  4 class ByteUShift {
  5 	static public void main(String args[]) {
  6 		char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  7 		byte b = (byte) 0xf1;
  8 		byte c = (byte) (b >> 4);
  9 		byte d = (byte) (b >>> 4);
 10 		byte e = (byte) ((b & 0xff) >> 4);
 11 
 12 		System.out.println("              b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
 13 		System.out.println("         b >> 4 = 0x" + hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
 14 		System.out.println("        b >>> 4 = 0x" + hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
 15 		System.out.println("(b & 0xff) >> 4 = 0x" + hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
 16 	}
 17 }
 18 //其运行结果为:
 19               b = 0xf1
 20          b >> 4 = 0xff
 21         b >>> 4 = 0xff
 22 (b & 0xff) >> 4 = 0x0f
 23 

image

4.2.5 位运算符与赋值的组合

image

  1 package Chap4;
  2 /**
  3  *
  4 * @ClassName: OpBitEquals
  5 * @Description: 位运算符与赋值的组合
  6 * @author  刘军/shall_liu (1136808529@qq.com)
  7 * @date 2017年9月13日 上午1:31:37
  8 *
  9  */
 10 class OpBitEquals {
 11 	public static void main(String args[]) {
 12 		int a = 1;
 13 		int b = 2;
 14 		int c = 3;
 15 
 16 		a |= 4;
 17 		b >>= 1;
 18 		c <<= 1;
 19 		a ^= c;
 20 		System.out.println("a = " + a);
 21 		System.out.println("b = " + b);
 22 		System.out.println("c = " + c);
 23 	}
 24 }
 25 //其结果为:
 26 a = 3
 27 b = 1
 28 c = 6
 29 


4.3 关系运算符

image

image

image



4.4 布尔逻辑运算符

image

image

image

  1 package Chap4;
  2 
  3 //Demonstrate the boolean logical operators.
  4 /**
  5  *
  6 * @ClassName: BoolLogic
  7 * @Description: 布尔逻辑运算符
  8 * @author  刘军/shall_liu (1136808529@qq.com)
  9 * @date 2017年9月13日 上午1:38:24
 10 *
 11  */
 12 class BoolLogic {
 13 	public static void main(String args[]) {
 14 		boolean a = true;
 15 		boolean b = false;
 16 		boolean c = a | b;
 17 		boolean d = a & b;
 18 		boolean e = a ^ b;
 19 		boolean f = (!a & b) | (a & !b);
 20 		boolean g = !a;
 21 
 22 		System.out.println("        a = " + a);
 23 		System.out.println("        b = " + b);
 24 		System.out.println("      a|b = " + c);
 25 		System.out.println("      a&b = " + d);
 26 		System.out.println("      a^b = " + e);
 27 		System.out.println("!a&b|a&!b = " + f);
 28 		System.out.println("       !a = " + g);
 29 	}
 30 }
 31 //其运行结果如下;
 32         a = true
 33         b = false
 34       a|b = true
 35       a&b = false
 36       a^b = true
 37 !a&b|a&!b = true
 38        !a = false
 39 
 40 
 41 

image

image

4.5 赋值运算符

image

image


4.6 “?”运算符

image

  1 package Chap4;
  2 
  3 //Demonstrate ?. 
  4 class Ternary {
  5 	public static void main(String args[]) {
  6 		int i, k;
  7 
  8 		i = 10;
  9 		k = i < 0 ? -i : i; // get absolute value of i
 10 		System.out.print("Absolute value of ");
 11 		System.out.println(i + " is " + k);
 12 
 13 		i = -10;
 14 		k = i < 0 ? -i : i; // get absolute value of i
 15 		System.out.print("Absolute value of ");
 16 		System.out.println(i + " is " + k);
 17 	}
 18 }
 19 
 20 //其運行結果為:
 21 Absolute value of 10 is 10
 22 Absolute value of -10 is 10
 23 

4.7 运算符的优先级

image

image

4.8 使用圆括号

image

image

第5章 控制语句

image

5.1 Java的选择语句

imageimage

5.1.1 if语句

image

image

image


image

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: IfElse
  6  * @Description: Demonstrate if-else-if statements.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午7:54:10
  9  *
 10  */
 11 class IfElse {
 12 	public static void main(String args[]) {
 13 		int month = 4; // April
 14 		String season;
 15 
 16 		if (month == 12 || month == 1 || month == 2)
 17 			season = "Winter";
 18 		else if (month == 3 || month == 4 || month == 5)
 19 			season = "Spring";
 20 		else if (month == 6 || month == 7 || month == 8)
 21 			season = "Summer";
 22 		else if (month == 9 || month == 10 || month == 11)
 23 			season = "Autumn";
 24 		else
 25 			season = "Bogus Month";
 26 
 27 		System.out.println("April is in the " + season + ".");
 28 	}
 29 }
 30 
 31 //其運行結果為:
 32 April is in the Spring.
 33 


5.1.2 switch语句

image

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5 * @ClassName: SampleSwitch
  6 * @Description: A simple example of the switch.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月13日 下午7:57:52
  9 *
 10  */
 11 class SampleSwitch {
 12 public static void main(String args[]) {
 13  for(int i=0; i<6; i++)
 14    switch(i) {
 15      case 0:
 16        System.out.println("i is zero.");
 17        break;
 18      case 1:
 19        System.out.println("i is one.");
 20        break;
 21      case 2:
 22        System.out.println("i is two.");
 23        break;
 24      case 3:
 25        System.out.println("i is three.");
 26        break;
 27      default:
 28        System.out.println("i is greater than 3.");
 29    }
 30  }
 31 }
 32 
 33 //其運行結果為:
 34 i is zero.
 35 i is one.
 36 i is two.
 37 i is three.
 38 i is greater than 3.
 39 i is greater than 3.
 40 
 41 

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5 * @ClassName: MissingBreak
  6 * @Description: In a switch, break statements are optional.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月13日 下午8:00:18
  9 *
 10  */
 11 class MissingBreak {
 12 	public static void main(String args[]) {
 13 		for (int i = 0; i < 12; i++){
 14 			switch (i) {
 15 				case 0:
 16 				case 1:
 17 				case 2:
 18 				case 3:
 19 				case 4:
 20 					System.out.println("i is less than 5");
 21 					break;
 22 				case 5:
 23 				case 6:
 24 				case 7:
 25 				case 8:
 26 				case 9:
 27 					System.out.println("i is less than 10");
 28 					break;
 29 				default:
 30 					System.out.println("i is 10 or more.");
 31 			}
 32 		}
 33 	}
 34 }
 35 
 36 //其運行結果為;
 37 i is less than 5
 38 i is less than 5
 39 i is less than 5
 40 i is less than 5
 41 i is less than 5
 42 i is less than 10
 43 i is less than 10
 44 i is less than 10
 45 i is less than 10
 46 i is less than 10
 47 i is 10 or more.
 48 i is 10 or more.
 49 

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: Switch
  6  * @Description: An improved version of the season program.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午8:05:43
  9  *
 10  */
 11 class Switch {
 12 	public static void main(String args[]) {
 13 		int month = 4;
 14 		String season;
 15 		for (month = 0; month < 13; month++) {
 16 			switch (month) {
 17 			case 12:
 18 			case 1:
 19 			case 2:
 20 				season = "Winter";
 21 				break;
 22 			case 3:
 23 			case 4:
 24 			case 5:
 25 				season = "Spring";
 26 				break;
 27 			case 6:
 28 			case 7:
 29 			case 8:
 30 				season = "Summer";
 31 				break;
 32 			case 9:
 33 			case 10:
 34 			case 11:
 35 				season = "Autumn";
 36 				break;
 37 			default:
 38 				season = "Bogus Month";
 39 			}
 40 			System.out.println("April is in the " + season + ".");
 41 		}
 42 	}
 43 }
 44 //其運行結果為:
 45 April is in the Bogus Month.
 46 April is in the Winter.
 47 April is in the Winter.
 48 April is in the Spring.
 49 April is in the Spring.
 50 April is in the Spring.
 51 April is in the Summer.
 52 April is in the Summer.
 53 April is in the Summer.
 54 April is in the Autumn.
 55 April is in the Autumn.
 56 April is in the Autumn.
 57 April is in the Winter.
 58 
 59 

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5 * @ClassName: StringSwitch
  6 * @Description: Use a string to control a switch statement.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月13日 下午8:09:43
  9 *
 10  */
 11 
 12 class StringSwitch {
 13 	public static void main(String args[]) {
 14 
 15 		String str = "two";
 16 
 17 		switch (str) {
 18 		case "one":
 19 			System.out.println("one");
 20 			break;
 21 		case "two":
 22 			System.out.println("two");
 23 			break;
 24 		case "three":
 25 			System.out.println("three");
 26 			break;
 27 		default:
 28 			System.out.println("no match");
 29 			break;
 30 		}
 31 	}
 32 }
 33 
 34 //其運行結果為:
 35 two
 36 

image

image

image

image



5.2 迭代语句

image

5.2.1 while语句

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: While
  6  * @Description: Demonstrate the while loop.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午8:20:22
  9  *
 10  */
 11 class While {
 12 	public static void main(String args[]) {
 13 		int n = 10;
 14 
 15 		while (n > 0) {
 16 			System.out.println("tick " + n);
 17 			n--;
 18 		}
 19 	}
 20 }
 21 
 22 //其運行結果為:
 23 tick 10
 24 tick 9
 25 tick 8
 26 tick 7
 27 tick 6
 28 tick 5
 29 tick 4
 30 tick 3
 31 tick 2
 32 tick 1
 33 

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: NoBody
  6  * @Description: The target of a loop can be empty.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午8:22:46
  9  *
 10  */
 11 class NoBody {
 12 	public static void main(String args[]) {
 13 		int i, j;
 14 
 15 		i = 100;
 16 		j = 200;
 17 
 18 		// find midpoint between i and j
 19 		while (++i < --j) {
 20 			// no body in this loop
 21 		}
 22 		System.out.println("Midpoint is " + i);
 23 	}
 24 }
 25 //其運行結果為:
 26 Midpoint is 150
 27 

image

5.2.2 do-while语句

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: DoWhile
  6  * @Description: Demonstrate the do-while loop.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午8:26:48
  9  *
 10  */
 11 class DoWhile {
 12 	public static void main(String args[]) {
 13 		int n = 0;
 14 		System.out.println(n);
 15 		do {
 16 			System.out.println("tick " + n);
 17 			n--;
 18 		} while (n > 0);
 19 
 20 		System.out.println(n);
 21 		n = 10;
 22 		System.out.println(n);
 23 		do {
 24 			System.out.println("tick " + n);
 25 			n--;
 26 		} while (n > 0);
 27 	}
 28 }
 29 //其運行結果為:
 30 0
 31 tick 0
 32 -1
 33 10
 34 tick 10
 35 tick 9
 36 tick 8
 37 tick 7
 38 tick 6
 39 tick 5
 40 tick 4
 41 tick 3
 42 tick 2
 43 tick 1
 44 

image

image

  1 package Chap5;
  2 
  3 //Using a do-while to process a menu selection -- a simple help system.
  4 class Menu {
  5 	public static void main(String args[]) throws java.io.IOException {
  6 		int choice=1;
  7 
  8 		do {
  9 			System.out.println("Help on:");
 10 			System.out.println("  1. if");
 11 			System.out.println("  2. switch");
 12 			System.out.println("  3. while");
 13 			System.out.println("  4. do-while");
 14 			System.out.println("  5. for\n");
 15 			System.out.println("Choose one:");
 16 
 17 		} while (choice <1 && choice >5);
 18 
 19 		System.out.println("choice:"+choice+"_____ \n");
 20 
 21 		switch (choice) {
 22 		case 1:
 23 			System.out.println("The if:\n");
 24 			System.out.println("if(condition) statement;");
 25 			System.out.println("else statement;");
 26 			break;
 27 		case 2:
 28 			System.out.println("The switch:\n");
 29 			System.out.println("switch(expression) {");
 30 			System.out.println("  case constant:");
 31 			System.out.println("    statement sequence");
 32 			System.out.println("  break;");
 33 			System.out.println("  // ...");
 34 			System.out.println("}");
 35 			break;
 36 		case 3:
 37 			System.out.println("The while:\n");
 38 			System.out.println("while(condition) statement;");
 39 			break;
 40 		case 4:
 41 			System.out.println("The do-while:\n");
 42 			System.out.println("do {");
 43 			System.out.println("  statement;");
 44 			System.out.println("} while (condition);");
 45 			break;
 46 		case 5:
 47 			System.out.println("The for:\n");
 48 			System.out.print("for(init; condition; iteration)");
 49 			System.out.println(" statement;");
 50 			break;
 51 		}
 52 	}
 53 }
 54 //其運行結果為:
 55 Help on:
 56   1. if
 57   2. switch
 58   3. while
 59   4. do-while
 60   5. for
 61 
 62 Choose one:
 63 choice:1_____
 64 
 65 The if:
 66 
 67 if(condition) statement;
 68 else statement;
 69 
View Code

image

5.2.3 for语句

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: ForTick
  6  * @Description: Demonstrate the for loop.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午8:47:34
  9  *
 10  */
 11 class ForTick {
 12 	public static void main(String args[]) {
 13 		int n;
 14 
 15 		for (n = 10; n > 0; n--)
 16 			System.out.println("tick " + n);
 17 	}
 18 }
 19 //其运行结果为:
 20 tick 10
 21 tick 9
 22 tick 8
 23 tick 7
 24 tick 6
 25 tick 5
 26 tick 4
 27 tick 3
 28 tick 2
 29 tick 1
 30 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: ForTick
  6  * @Description: Declare a loop control variable inside the for.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午8:48:55
  9  *
 10  */
 11 class ForTick {
 12 	public static void main(String args[]) {
 13 
 14 		// here, n is declared inside of the for loop
 15 		for (int n = 10; n > 0; n--)
 16 			System.out.println("tick " + n);
 17 	}
 18 }
 19 //其運行結果為;
 20 tick 10
 21 tick 9
 22 tick 8
 23 tick 7
 24 tick 6
 25 tick 5
 26 tick 4
 27 tick 3
 28 tick 2
 29 tick 1
 30 
View Code

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: FindPrime
  6  * @Description: Test for primes.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午8:59:08
  9  *
 10  */
 11 class FindPrime {
 12 	public static void main(String args[]) {
 13 		int num;
 14 		boolean isPrime;
 15 
 16 		num = 14;
 17 
 18 		if (num < 2)
 19 			isPrime = false;
 20 		else
 21 			isPrime = true;
 22 
 23 		for (int i = 2; i <= num / i; i++) {
 24 			if ((num % i) == 0) {
 25 				isPrime = false;
 26 				break;
 27 			}
 28 		}
 29 
 30 		if (isPrime)
 31 			System.out.println("Prime");
 32 		else
 33 			System.out.println("Not Prime");
 34 	}
 35 }
 36 
 37 //其 运行结果为:
 38 Not Prime
 39 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: Sample
  6  * @Description:
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:00:52
  9  *
 10  */
 11 
 12 class Sample {
 13 	public static void main(String args[]) {
 14 		int a, b;
 15 
 16 		b = 4;
 17 		for (a = 1; a < b; a++) {
 18 			System.out.println("a = " + a);
 19 			System.out.println("b = " + b);
 20 			b--;
 21 		}
 22 	}
 23 }
 24 //其运行结果为:
 25 a = 1
 26 b = 4
 27 a = 2
 28 b = 3
 29 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: Comma
  6  * @Description:Using the comma.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:13:45
  9  *
 10  */
 11 class Comma {
 12 	public static void main(String args[]) {
 13 		int a, b;
 14 
 15 		for (a = 1, b = 4; a < b; a++, b--) {
 16 			System.out.println("a = " + a);
 17 			System.out.println("b = " + b);
 18 		}
 19 	}
 20 }
 21 //其运行结果为:
 22 a = 1
 23 b = 4
 24 a = 2
 25 b = 3
 26 
View Code


5.2.4 for循环的for-each版本

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: ForVar
  6  * @Description: Parts of the for loop can be empty.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:16:02
  9  *
 10  */
 11 class ForVar {
 12 	public static void main(String args[]) {
 13 		int i;
 14 		boolean done = false;
 15 
 16 		i = 0;
 17 		for (; !done;) {
 18 			System.out.println("i is " + i);
 19 			if (i == 10){
 20 				done = true;
 21 			}
 22 			i++;
 23 		}
 24 	}
 25 }
 26 
 27 //其运行结果为:
 28 i is 0
 29 i is 1
 30 i is 2
 31 i is 3
 32 i is 4
 33 i is 5
 34 i is 6
 35 i is 7
 36 i is 8
 37 i is 9
 38 i is 10
 39 
View Code

image

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: ForEach
  6  * @Description: Use a for-each style for loop.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:21:15
  9  *
 10  */
 11 
 12 class ForEach {
 13 	public static void main(String args[]) {
 14 		int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 15 		int sum = 0;
 16 
 17 		// use for-each style for to display and sum the values
 18 		for (int x : nums) {
 19 			System.out.println("Value is: " + x);
 20 			sum += x;
 21 		}
 22 
 23 		System.out.println("Summation: " + sum);
 24 	}
 25 }
 26 //其运行结果为:
 27 Value is: 1
 28 Value is: 2
 29 Value is: 3
 30 Value is: 4
 31 Value is: 5
 32 Value is: 6
 33 Value is: 7
 34 Value is: 8
 35 Value is: 9
 36 Value is: 10
 37 Summation: 55
 38 
 39 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: ForEach2
  6  * @Description: Use break with a for-each style for.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:23:43
  9  *
 10  */
 11 class ForEach2 {
 12 	public static void main(String args[]) {
 13 		int sum = 0;
 14 		int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 15 
 16 		// use for to display and sum the values
 17 		for (int x : nums) {
 18 			System.out.println("Value is: " + x);
 19 			sum += x;
 20 			if (x == 5){
 21 				break; // stop the loop when 5 is obtained
 22 			}
 23 		}
 24 		System.out.println("Summation of first 5 elements: " + sum);
 25 	}
 26 }
 27 
 28 //其运行结果为
 29 Value is: 1
 30 Value is: 2
 31 Value is: 3
 32 Value is: 4
 33 Value is: 5
 34 Summation of first 5 elements: 15
 35 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: NoChange
  6  * @Description: The for-each loop is essentially read-only.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:25:22
  9  *
 10  */
 11 class NoChange {
 12 	public static void main(String args[]) {
 13 		int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 14 
 15 		for (int x : nums) {
 16 			System.out.print(x + "");
 17 			x = x * 10; // no effect on nums
 18 			System.out.print ("_"+x + "; ");
 19 		}
 20 
 21 		System.out.println();
 22 
 23 		for (int x : nums)
 24 			System.out.print (x + "");
 25 
 26 		System.out.println();
 27 	}
 28 }
 29 
 30 //其运行结果为
 31 1 _10; 2 _20; 3 _30; 4 _40; 5 _50; 6 _60; 7 _70; 8 _80; 9 _90; 10 _100;
 32 1 2 3 4 5 6 7 8 9 10
 33 
 34 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: ForEach3
  6  * @Description: Use for-each style for on a two-dimensional array.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:30:04
  9  *
 10  */
 11 class ForEach3 {
 12 	public static void main(String args[]) {
 13 		int sum = 0;
 14 		int nums[][] = new int[3][5];
 15 
 16 		// give nums some values
 17 		for (int i = 0; i < 3; i++)
 18 			for (int j = 0; j < 5; j++){
 19 				nums[i][j] = (i + 1) * (j + 1);
 20 			}
 21 		// use for-each for to display and sum the values
 22 		for (int x[] : nums) {
 23 			for (int y : x) {
 24 				System.out.println("Value is: " + y);
 25 				sum += y;
 26 			}
 27 		}
 28 		System.out.println("Summation: " + sum);
 29 	}
 30 }
 31 //其运行结果为
 32 Value is: 1
 33 Value is: 2
 34 Value is: 3
 35 Value is: 4
 36 Value is: 5
 37 Value is: 2
 38 Value is: 4
 39 Value is: 6
 40 Value is: 8
 41 Value is: 10
 42 Value is: 3
 43 Value is: 6
 44 Value is: 9
 45 Value is: 12
 46 Value is: 15
 47 Summation: 90
 48 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: Search
  6  * @Description: Search an array using for-each style for.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:34:16
  9  *
 10  */
 11 class Search {
 12 	public static void main(String args[]) {
 13 		int nums[] = { 6, 8, 3, 7, 5, 6, 1, 4 };
 14 		int val = 5;
 15 		boolean found = false;
 16 
 17 		// use for-each style for to search nums for val
 18 		for (int x : nums) {
 19 			if (x == val) {
 20 				found = true;
 21 				break;
 22 			}
 23 		}
 24 
 25 		if (found)
 26 			System.out.println("Value found!");
 27 	}
 28 }
 29 //其运行结果为
 30 Value found!
 31 
View Code

image


5.2.5 嵌套的循环

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: Nested
  6  * @Description: Loops may be nested.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午9:37:26
  9  *
 10  */
 11 class Nested {
 12 	public static void main(String args[]) {
 13 		int i, j;
 14 
 15 		for (i = 0; i < 10; i++) {
 16 			for (j = i; j < 10; j++)
 17 				System.out.print(".");
 18 			System.out.println();
 19 		}
 20 	}
 21 }
 22 
 23 //其运行结果为;
 24 ..........
 25 .........
 26 ........
 27 .......
 28 ......
 29 .....
 30 ....
 31 ...
 32 ..
 33 .
 34 
 35 
View Code


5.3 跳转语句


image

5.3.1 使用break语句

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5 * @ClassName: BreakLoop
  6 * @Description: Using break to exit a loop.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月13日 下午9:40:51
  9 *
 10  */
 11 class BreakLoop {
 12 	public static void main(String args[]) {
 13 		for (int i = 0; i < 100; i++) {
 14 			if (i == 10){
 15 				break; // terminate loop if i is 10
 16 			}
 17 			System.out.println("i: " + i);
 18 		}
 19 		System.out.println("Loop complete.");
 20 	}
 21 }
 22 //其运行结果为:
 23 i: 0
 24 i: 1
 25 i: 2
 26 i: 3
 27 i: 4
 28 i: 5
 29 i: 6
 30 i: 7
 31 i: 8
 32 i: 9
 33 Loop complete.
 34 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  * Using break to exit a while loop.
  5  *
  6  * @ClassName: BreakLoop2
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午9:42:56
 10  *
 11  */
 12 class BreakLoop2 {
 13 	public static void main(String args[]) {
 14 		int i = 0;
 15 
 16 		while (i < 100) {
 17 			if (i == 10)
 18 				break; // terminate loop if i is 10
 19 			System.out.println("i: " + i);
 20 			i++;
 21 		}
 22 		System.out.println("Loop complete.");
 23 	}
 24 }
 25 
 26 //其运行结果为:
 27 i: 0
 28 i: 1
 29 i: 2
 30 i: 3
 31 i: 4
 32 i: 5
 33 i: 6
 34 i: 7
 35 i: 8
 36 i: 9
 37 Loop complete.
 38 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  * Using break with nested loops.
  5  *
  6  * @ClassName: BreakLoop3
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午9:45:02
 10  *
 11  */
 12 class BreakLoop3 {
 13 	public static void main(String args[]) {
 14 		for (int i = 0; i < 3; i++) {
 15 			System.out.print("Pass " + i + ": ");
 16 			for (int j = 0; j < 100; j++) {
 17 				if (j == 10)
 18 					break; // terminate loop if j is 10
 19 				System.out.print(j + "");
 20 			}
 21 			System.out.println();
 22 		}
 23 		System.out.println("Loops complete.");
 24 	}
 25 }
 26 //其运行结果为:
 27 Pass 0: 0 1 2 3 4 5 6 7 8 9
 28 Pass 1: 0 1 2 3 4 5 6 7 8 9
 29 Pass 2: 0 1 2 3 4 5 6 7 8 9
 30 Loops complete.
 31 
View Code

image

image

image

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5 * @ClassName: Break
  6 * @Description: Using break as a civilized form of goto.
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月13日 下午9:49:04
  9 *
 10  */
 11 
 12 class Break {
 13 	public static void main(String args[]) {
 14 		boolean t = true;
 15 
 16 		first: {
 17 			second: {
 18 				third: {
 19 					System.out.println("Before the break.");
 20 					if (t)
 21 						break second; // break out of second block
 22 					System.out.println("This won't execute");
 23 				}
 24 				System.out.println("This won't execute");
 25 			}
 26 			System.out.println("This is after second block.");
 27 		}
 28 	}
 29 }
 30 
 31 //其运行结果为:
 32 Before the break.
 33 This is after second block.
 34 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  * Using break to exit from nested loops
  5  *
  6  * @ClassName: BreakLoop4
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午10:24:37
 10  *
 11  */
 12 class BreakLoop4 {
 13 	public static void main(String args[]) {
 14 		outer: for (int i = 0; i < 3; i++) {
 15 			System.out.print("Pass " + i + ": ");
 16 			for (int j = 0; j < 100; j++) {
 17 				if (j == 10)
 18 					break outer; // exit both loops
 19 				System.out.print(j + "");
 20 			}
 21 			System.out.println("This will not print");
 22 		}
 23 		System.out.println("Loops complete.");
 24 	}
 25 }
 26 //其运行结果为:
 27 Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops complete.
 28 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  *
  5  * @ClassName: BreakErr
  6  * @Description: This program contains an error.
  7  * @author 刘军/shall_liu (1136808529@qq.com)
  8  * @date 2017年9月13日 下午10:26:39
  9  *
 10  */
 11 class BreakErr {
 12 	public static void main(String args[]) {
 13 
 14 		one: for (int i = 0; i < 3; i++) {
 15 			System.out.print("Pass " + i + ": ");
 16 		}
 17 
 18 		for (int j = 0; j < 100; j++) {
 19 			if (j == 10)
 20 				break one; // WRONG
 21 			System.out.print(j + "");
 22 		}
 23 	}
 24 }
View Code

image


image

5.3.2 使用continue语句

image

  1 package Chap5;
  2 
  3 /**
  4  * Demonstrate continue.
  5  *
  6  * @ClassName: Continue
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午10:29:14
 10  *
 11  */
 12 class Continue {
 13 	public static void main(String args[]) {
 14 		for (int i = 0; i < 10; i++) {
 15 			System.out.print(i + "");
 16 			if (i % 2 == 0){
 17 				continue;
 18 			}
 19 			System.out.println("");
 20 		}
 21 	}
 22 }
 23 //其运行结果为:
 24 0 1
 25 2 3
 26 4 5
 27 6 7
 28 8 9
 29 
View Code

image

  1 package Chap5;
  2 
  3 /**
  4  * Using continue with a label.
  5 * @ClassName: ContinueLabel
  6 * @Description:
  7 * @author  刘军/shall_liu (1136808529@qq.com)
  8 * @date 2017年9月13日 下午10:30:57
  9 *
 10  */
 11 class ContinueLabel {
 12 	public static void main(String args[]) {
 13 		outer: for (int i = 0; i < 10; i++) {
 14 			for (int j = 0; j < 10; j++) {
 15 				if (j > i) {
 16 					System.out.println();
 17 					continue outer;
 18 				}
 19 				System.out.print("" + (i * j));
 20 			}
 21 		}
 22 		System.out.println();
 23 	}
 24 }
 25 //其运行结果为:
 26  0
 27  0 1
 28  0 2 4
 29  0 3 6 9
 30  0 4 8 12 16
 31  0 5 10 15 20 25
 32  0 6 12 18 24 30 36
 33  0 7 14 21 28 35 42 49
 34  0 8 16 24 32 40 48 56 64
 35  0 9 18 27 36 45 54 63 72 81
 36 
View Code

image

5.3.3 return语句

image

  1 package Chap5;
  2 
  3 /**
  4  * Demonstrate return.
  5  *
  6  * @ClassName: Return
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午10:33:52
 10  *
 11  */
 12 class Return {
 13 	public static void main(String args[]) {
 14 		boolean t = true;
 15 
 16 		System.out.println("Before the return.");
 17 
 18 		if (t){
 19 			return; // return to caller
 20 		}
 21 		System.out.println("This won't execute.");
 22 	}
 23 }
 24 //其运行结果为:
 25 Before the return.
 26 
View Code

image


第6章 类

image


6.1 类的基础知识


image

6.1.1 类的一般形式

image

image

image

image

6.1.2 一个简单的类

image

image

  1 package Chap6;
  2 
  3 /**
  4  *
  5  * @ClassName: Box
  6  * @Description: A program that uses the Box class.
  7  *  Call this file BoxDemo.java
  8  *
  9  * @author 刘军/shall_liu (1136808529@qq.com)
 10  * @date 2017年9月13日 下午10:45:28
 11  *
 12  */
 13 class Box {
 14 	double width;
 15 	double height;
 16 	double depth;
 17 }
View Code
  1 package Chap6;
  2 
  3 /**
  4  * This class declares an object of type Box.
  5  *
  6  * @ClassName: BoxDemo
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午10:47:41
 10  *
 11  */
 12 class BoxDemo {
 13 	public static void main(String args[]) {
 14 		Box mybox = new Box();
 15 		double vol;
 16 
 17 		// assign values to mybox's instance variables
 18 		mybox.width = 10;
 19 		mybox.height = 20;
 20 		mybox.depth = 15;
 21 
 22 		// compute volume of box
 23 		vol = mybox.width * mybox.height * mybox.depth;
 24 
 25 		System.out.println("Volume is " + vol);
 26 	}
 27 }
 28 //其运行结果为:
 29 Volume is 3000.0
 30 
View Code

image

  1 package Chap6;
  2 
  3 /**
  4  *
  5  * @ClassName: Box
  6  * @Description: A program that uses the Box class.
  7  *  Call this file BoxDemo.java
  8  *
  9  * @author 刘军/shall_liu (1136808529@qq.com)
 10  * @date 2017年9月13日 下午10:45:28
 11  *
 12  */
 13 class Box {
 14 	double width;
 15 	double height;
 16 	double depth;
 17 }
 18 
 19 //--------------------------------------------------------------------
 20 
 21 
 22 package Chap6;
 23 
 24 /**
 25  *
 26  * @ClassName: BoxDemo2
 27  * @Description:
 28  * @author 刘军/shall_liu (1136808529@qq.com)
 29  * @date 2017年9月13日 下午10:49:47
 30  *
 31  */
 32 
 33 class BoxDemo2 {
 34 	public static void main(String args[]) {
 35 		Box mybox1 = new Box();
 36 		Box mybox2 = new Box();
 37 		double vol;
 38 
 39 		// assign values to mybox1's instance variables
 40 		mybox1.width = 10;
 41 		mybox1.height = 20;
 42 		mybox1.depth = 15;
 43 
 44 		/*
 45 		 * assign different values to mybox2's instance variables
 46 		 */
 47 		mybox2.width = 3;
 48 		mybox2.height = 6;
 49 		mybox2.depth = 9;
 50 
 51 		// compute volume of first box
 52 		vol = mybox1.width * mybox1.height * mybox1.depth;
 53 		System.out.println("Volume is " + vol);
 54 
 55 		// compute volume of second box
 56 		vol = mybox2.width * mybox2.height * mybox2.depth;
 57 		System.out.println("Volume is " + vol);
 58 	}
 59 }
 60 
 61 //其运行结果为:
 62 Volume is 3000.0
 63 Volume is 162.0
 64 
 65 
 66 
 67 
View Code

image



6.2 声明对象

image

image

image

image


6.3 为对象引用变量赋值

image

image


6.4 方法

image

image

  1 package Chap6;
  2 
  3 /**
  4  * This program includes a method inside the box class.
  5  *
  6  * @ClassName: Box
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午11:01:47
 10  *
 11  */
 12 
 13 class Box {
 14 	double width;
 15 	double height;
 16 	double depth;
 17 
 18 	// display volume of a box
 19 	void volume() {
 20 		System.out.print("Volume is ");
 21 		System.out.println(width * height * depth);
 22 	}
 23 }
 24 
 25 
 26 
 27 package Chap6;
 28 
 29 /**
 30  *
 31  * @ClassName: BoxDemo3
 32  * @Description:
 33  * @author 刘军/shall_liu (1136808529@qq.com)
 34  * @date 2017年9月13日 下午11:03:08
 35  *
 36  */
 37 
 38 class BoxDemo3 {
 39 	public static void main(String args[]) {
 40 		Box mybox1 = new Box();
 41 		Box mybox2 = new Box();
 42 
 43 		// assign values to mybox1's instance variables
 44 		mybox1.width = 10;
 45 		mybox1.height = 20;
 46 		mybox1.depth = 15;
 47 
 48 		/*
 49 		 * assign different values to mybox2's instance variables
 50 		 */
 51 		mybox2.width = 3;
 52 		mybox2.height = 6;
 53 		mybox2.depth = 9;
 54 
 55 		// display volume of first box
 56 		mybox1.volume();
 57 
 58 		// display volume of second box
 59 		mybox2.volume();
 60 	}
 61 }
 62 //其运行结果为:
 63 Volume is 3000.0
 64 Volume is 162.0
 65 
 66 
View Code

image

image



6.4.2 返回值

image


  1 package Chap6;
  2 
  3 /**
  4  * This program includes a method inside the box class.
  5  *
  6  * @ClassName: Box
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午11:01:47
 10  *
 11  */
 12 
 13 class Box {
 14 	double width;
 15 	double height;
 16 	double depth;
 17 
 18 	// display volume of a box
 19 	void volume() {
 20 		System.out.print("Volume is ");
 21 		System.out.println(width * height * depth);
 22 	}
 23 
 24 	// compute and return volume
 25 	double volume1() {
 26 		return width * height * depth;
 27 	}
 28 }
 29 
 30 
 31 
 32 package Chap6;
 33 
 34 /**
 35  *
 36  * @ClassName: BoxDemo4
 37  * @Description:
 38  * @author 刘军/shall_liu (1136808529@qq.com)
 39  * @date 2017年9月13日 下午11:09:23
 40  *
 41  */
 42 class BoxDemo4 {
 43 	public static void main(String args[]) {
 44 		Box mybox1 = new Box();
 45 		Box mybox2 = new Box();
 46 		double vol;
 47 
 48 		// assign values to mybox1's instance variables
 49 		mybox1.width = 10;
 50 		mybox1.height = 20;
 51 		mybox1.depth = 15;
 52 
 53 		/*
 54 		 * assign different values to mybox2's instance variables
 55 		 */
 56 		mybox2.width = 3;
 57 		mybox2.height = 6;
 58 		mybox2.depth = 9;
 59 
 60 		// get volume of first box
 61 		vol = mybox1.volume1();
 62 		System.out.println("Volume is " + vol);
 63 
 64 		// get volume of second box
 65 		vol = mybox2.volume1();
 66 		System.out.println("Volume is " + vol);
 67 	}
 68 }
 69 //其运行结果为:
 70 Volume is 3000.0
 71 Volume is 162.0
 72 
 73 
View Code

image

6.4.3 添加带参数的方法

image

image

image

image

  1 package Chap6;
  2 
  3 /**
  4  * This program includes a method inside the box class.
  5  *
  6  * @ClassName: Box
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午11:01:47
 10  *
 11  */
 12 
 13 class Box {
 14 	double width;
 15 	double height;
 16 	double depth;
 17 
 18 	// display volume of a box
 19 	void volume() {
 20 		System.out.print("Volume is ");
 21 		System.out.println(width * height * depth);
 22 	}
 23 
 24 	// compute and return volume
 25 	double volume1() {
 26 		return width * height * depth;
 27 	}
 28 
 29 	// sets dimensions of box
 30 	void setDim(double w, double h, double d) {
 31 		width = w;
 32 		height = h;
 33 		depth = d;
 34 	}
 35 }
 36 
 37 
 38 package Chap6;
 39 
 40 /**
 41  *
 42  * @ClassName: BoxDemo5
 43  * @Description:
 44  * @author 刘军/shall_liu (1136808529@qq.com)
 45  * @date 2017年9月13日 下午11:15:15
 46  *
 47  */
 48 class BoxDemo5 {
 49 	public static void main(String args[]) {
 50 		Box mybox1 = new Box();
 51 		Box mybox2 = new Box();
 52 		double vol;
 53 
 54 		// initialize each box
 55 		mybox1.setDim(10, 20, 15);
 56 		mybox2.setDim(3, 6, 9);
 57 
 58 		// get volume of first box
 59 		vol = mybox1.volume1();
 60 		System.out.println("Volume is " + vol);
 61 
 62 		// get volume of second box
 63 		vol = mybox2.volume1();
 64 		System.out.println("Volume is " + vol);
 65 	}
 66 }
 67 
 68 //其运行结果为:
 69 Volume is 3000.0
 70 Volume is 162.0
 71 
View Code

image



6.5 构造函数

image

  1 package Chap6;
  2 
  3 /**
  4  * This program includes a method inside the box class.
  5  *
  6  * @ClassName: Box
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午11:01:47
 10  *
 11  */
 12 
 13 class Box {
 14 
 15 	double width;
 16 	double height;
 17 	double depth;
 18 
 19 	// This is the constructor for Box.
 20 	Box() {
 21 		System.out.println("Constructing Box");
 22 		width = 10;
 23 		height = 10;
 24 		depth = 10;
 25 	}
 26 
 27 	// display volume of a box
 28 	void volume() {
 29 		System.out.print("Volume is ");
 30 		System.out.println(width * height * depth);
 31 	}
 32 
 33 	// compute and return volume
 34 	double volume1() {
 35 		return width * height * depth;
 36 	}
 37 
 38 	// sets dimensions of box
 39 	void setDim(double w, double h, double d) {
 40 		width = w;
 41 		height = h;
 42 		depth = d;
 43 	}
 44 }
 45 
 46 
 47 //----------------------
 48 package Chap6;
 49 
 50 /**
 51  *
 52  * @ClassName: BoxDemo6
 53  * @Description:
 54  * @author 刘军/shall_liu (1136808529@qq.com)
 55  * @date 2017年9月13日 下午11:23:56
 56  *
 57  */
 58 class BoxDemo6 {
 59 	public static void main(String args[]) {
 60 		// declare, allocate, and initialize Box objects
 61 		Box mybox1 = new Box();
 62 		Box mybox2 = new Box();
 63 
 64 		double vol;
 65 
 66 		// get volume of first box
 67 		vol = mybox1.volume1();
 68 		System.out.println("Volume is " + vol);
 69 
 70 		// get volume of second box
 71 		vol = mybox2.volume1();
 72 		System.out.println("Volume is " + vol);
 73 	}
 74 }
 75 //其运行结果为:
 76 Constructing Box
 77 Constructing Box
 78 Volume is 1000.0
 79 Volume is 1000.0
 80 
 81 
View Code

image

image

image

  1 package Chap6;
  2 
  3 /**
  4  * This program includes a method inside the box class.
  5  *
  6  * @ClassName: Box
  7  * @Description:
  8  * @author 刘军/shall_liu (1136808529@qq.com)
  9  * @date 2017年9月13日 下午11:01:47
 10  *
 11  */
 12 
 13 class Box {
 14 	double width;
 15 	double height;
 16 	double depth;
 17 
 18 
 19 	// This is the constructor for Box.
 20 	Box() {
 21 		System.out.println("Constructing Box");
 22 		width = 10;
 23 		height = 10;
 24 		depth = 10;
 25 	}
 26 	// This is the constructor for Box.
 27 		Box(double w, double h, double d) {
 28 			width = w;
 29 			height = h;
 30 			depth = d;
 31 		}
 32 	// display volume of a box
 33 	void volume() {
 34 		System.out.print("Volume is ");
 35 		System.out.println(width * height * depth);
 36 	}
 37 
 38 	// compute and return volume
 39 	double volume1() {
 40 		return width * height * depth;
 41 	}
 42 
 43 	// sets dimensions of box
 44 	void setDim(double w, double h, double d) {
 45 		width = w;
 46 		height = h;
 47 		depth = d;
 48 	}
 49 }
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 package Chap6;
 58 
 59 /**
 60  *
 61  * @ClassName: BoxDemo7
 62  * @Description:
 63  * @author 刘军/shall_liu (1136808529@qq.com)
 64  * @date 2017年9月13日 下午11:29:32
 65  *
 66  */
 67 class BoxDemo7 {
 68 	public static void main(String args[]) {
 69 		// declare, allocate, and initialize Box objects
 70 		Box mybox1 = new Box(10, 20, 15);
 71 		Box mybox2 = new Box(3, 6, 9);
 72 
 73 		double vol;
 74 
 75 		// get volume of first box
 76 		vol = mybox1.volume1();
 77 		System.out.println("Volume is " + vol);
 78 
 79 		// get volume of second box
 80 		vol = mybox2.volume1();
 81 		System.out.println("Volume is " + vol);
 82 	}
 83 }
 84 
 85 
 86 //其运行结果为:
 87 Volume is 3000.0
 88 Volume is 162.0
 89 
 90 
View Code

image


6.6 this关键字

image


image

image



6.7 垃圾回收

image

6.8 finalize()方法

image

6.9 堆栈类

imageimage

  1 package Chap6;
  2 
  3 class Stack {
  4 	int stck[] = new int[10];
  5 	int tos;
  6 
  7 	// Initialize top-of-stack
  8 	Stack() {
  9 		tos = -1;
 10 	}
 11 
 12 	// Push an item onto the stack
 13 	void push(int item) {
 14 		if (tos == 9)
 15 			System.out.println("Stack is full.");
 16 		else
 17 			stck[++tos] = item;
 18 	}
 19 
 20 	// Pop an item from the stack
 21 	int pop() {
 22 		if (tos < 0) {
 23 			System.out.println("Stack underflow.");
 24 			return 0;
 25 		} else
 26 			return stck[tos--];
 27 	}
 28 }
View Code

image

  1 package Chap6;
  2 
  3 class TestStack {
  4 	public static void main(String args[]) {
  5 		Stack mystack1 = new Stack();
  6 		Stack mystack2 = new Stack();
  7 
  8 		// push some numbers onto the stack
  9 		for (int i = 0; i < 10; i++)
 10 			mystack1.push(i);
 11 		for (int i = 10; i < 20; i++)
 12 			mystack2.push(i);
 13 
 14 		// pop those numbers off the stack
 15 		System.out.println("Stack in mystack1:");
 16 		for (int i = 0; i < 10; i++)
 17 			System.out.println(mystack1.pop());
 18 
 19 		System.out.println("Stack in mystack2:");
 20 		for (int i = 0; i < 10; i++)
 21 			System.out.println(mystack2.pop());
 22 	}
 23 }
View Code
  1 Stack in mystack1:
  2 9
  3 8
  4 7
  5 6
  6 5
  7 4
  8 3
  9 2
 10 1
 11 0
 12 Stack in mystack2:
 13 19
 14 18
 15 17
 16 16
 17 15
 18 14
 19 13
 20 12
 21 11
 22 10
 23 
View Code

image


 

 

posted @ 2017-09-11 20:57  一品堂.技术学习笔记  阅读(1316)  评论(0编辑  收藏  举报