Java学习笔记(1)Introduction to Computers, Programs, and Java

来源:互联网 发布:3dsmax2014软件许可证 编辑:程序博客网 时间:2024/05/01 07:33

学习教材为《Introducition to Java Programming》。

Chapter1 Introduction to Computers, Programs, and Java


1.2 What is Computer?

A computer consists of the following major hardware components (Figure 1.1):

A central processing unit (CPU)

Memory (main memory)

Storage devices (such as disks and CDs)

Input devices (such as the mouse and keyboard)

Output devices (such as monitors and printers)

Communication devices (such as modems and network interface cards)


1.2.1 Central Processing Unit

The central processing unit (CPU) is the computers brain. It retrieves instructions from memory and executes them. The CPU usually has two components: a control unit and an arithmetic/logic unit. The control unit controls and coordinates the actions of the other components. The arithmetic/logic unit performs numeric operations (addition, subtraction, multiplication, division) and logical operations (comparisons).


1.2.2 Bits and Bytes

The minimum storage unit in a computer is a byte. A byte is composed of eight bits. A small number such as 3 can be stored as a single byte. To store a number that cannot fit into a single byte, the computer uses several bytes.

A computers storage capacity is measured in bytes and multiples of the byte, as follows:

A kilobyte (KB) is about 1,000 bytes.

A megabyte (MB) is about 1 million bytes.

A gigabyte (GB) is about 1 billion bytes.

A terabyte (TB) is about 1 trillion bytes.


1.2.3Memory

1、主存用于在计算机运行时存储数据。任何数据在进入CPU运算之前,都必须存储在主存。

2、主存以字节(byte)为单位存储数据。不过表示存储容量的最小单位是位(bit),表示1个的1或者0。1字节=8位。

3、为了表示主存的每一个存储位置,主存必须按字节编址。

4、主存按照字节为单位,将存储空间从0开始编址。不使用位来编址,是因为这个单位实在太小,难以使用。例如,字符 ‘J’在计算机中用01001010这个二进制串表示,占1个字节大小。一个比较小的数,例如3,也只需要1个字节即可存储。更大的数需要多个字节才能存储,这时候计算机会连续分配几个字节给它。

5、字节是最小的存储单位。换句话说,计算机存储一个数,至少需要1字节。


1.2.4 Storage Devices

主存是易失性存储设备,掉电后所有信息就丢失了。为了长期保存数据,需要用外部存储设备,例如:磁盘(Disk drives),CDCD-RCD-RW),磁带,U盘等等。

磁盘(Magnetic disk drives)

1、磁盘以磁介质作为存储的媒介。容量从几个G到几个T都有。

2、典型的磁盘有硬盘(hard disks)以及软盘(floppy disks)。

CD和DVD

1、CD指的是光盘,有只读的CD-R和可读写的CD-RW,容量在700MB。

2、DVD也有两种,只读的DVD-R和可读写的DVD-RW,容量可以达到4.7GB。

U盘(USB Flash Drives) U盘是通过USB(Universal serial bus)线连接到计算机中的。容量可以达到256GB。


1.3 机器语言

机器语言->汇编语言->高级语言

1、机器语言是计算机能直接运行的唯一语言,它是二进制的指令形式。

2、机器语言的可读性显然是很差的,例如,把两个数相加,你可能要编写这么一段代码:1101101010011010

3、汇编语言是机器语言的符号版,由一组助记符组成,所以可读性大大提高。

不过显然计算机并不认识汇编语言,所以汇编语言需要借助汇编程序,将源代码翻译成机器语言之后才能够运行。仍然以加法为例,这时候你可以这样写代码了:ADD AX , BX

4、高级语言看上去已经和英文差不多了,所以容易学习和使用。例如计算半径为5的圆的面积,你可以直接这样写: area = 5 * 5 * 3.1415;


1.4 源代码的编译

高级语言需要转换为机器语言才能运行。

通常有两种做法:

1、解释执行:

利用解释器,将源代码逐行解释成机器语言,

边解释边运行,如图(a)所示。

2、编译执行:

利用编译器,将源代码一次性编译成机器语言然后再运行,如图(b)所示。这种方式运行速度比较快。


1.5 Java的特点

1Java Is Simple

2Java Is Object-Oriented

Java 是天生面向对象的语言。面向对象编程(Object-oriented programmingOOP)是区别于传统的面向过程编程的全新的思维方式。C语言是经典的面向过程的编程语言,以函数为单位来搭建程序。

面向对象编程是以对象为单位来搭建程序的,有助于代码的重用,并且大大提高了编程的效率。

面向对象编程有三个特性:封装性、继承性和多态性

3Java Is Distributed

4Java Is Interpreted

Java是解释型的语言,任何Java程序会被编译成字节码文件,这个文件需要安装Java虚拟机(Java Virtual MachineJVM)才能运行。

5Java Is Robust   6Java Is Secure

7Java Is Architecture-Neutral    8Java Is Portable

9Java's Performance    10Java Is Multithreaded      11Java Is Dynamic


1.6 JDK的版本

Java Standard Edition (J2SE)

J2SE用于开发客户端或单机版的应用程序,也可以用来写小应用程序(applet)

Java Enterprise Edition (J2EE)

J2EE用于开发服务端的大型应用程序,如Java servlets和Java Server Pages(JSP)

Java Micro Edition (J2ME).

J2ME用于开发移动设备的应用程序,如手机游戏。

本课程讲解J2SE版本。其实三个版本在语法和编程思路上完全没有区别,只是可使用的支持库有所不同。


1.7 源代码的编译

为了跨平台,Java只将源代码编译成字节码文件,然后由Java虚拟机(Java Virtual Machine,JVM)直接运行。

不同平台下的字节码文件,是完全相同的,所以你的Java程序完全无需考虑跨平台的问题。当然,为了运行字节码文件,不同平台需要安装对应的JVM。


1.8 Java环境变量的设置

为了方便手工编译运行Java程序,只需正确设置一个环境变量(使用IDE的话不设置也行,低版本的Java还要设置其它环境变量):

变量名:path(如已存在,请修改原值)

变量值:在原值的最前面加 C:\Program Files\Java\jdk1.8.0_73\bin;

注意:(1)上述路径其实是你机器上JDK的实际安装路径加上\bin,所以请根据实际情况修改;(2) 环境变量有两种:用户变量和系统变量。随便设置一个就行。如path不存在,自己新建。


1.9 Java支持三种注释

1、行注释:// 表示注释到行尾

2、段注释:/* 中间的所有文字都是注释,可以跨行,也可以跨段 */

3、文档注释:/** 中间的所有文字都是注释,可以跨行,也可以跨段,用于javadoc命令自动生成帮助文档。*/


1.10.1 关键字:

关键字又叫保留字,是Java留用的一些单词,有特定的含义,如class, public, static, void……


1.10.2 修饰符:

修饰符用来修饰变量、方法等,如本例的public和static,它们能够对变量起限制作用,如限定变量的作用范围。


1.10.3 语句:

语句表示一个具体的操作动作,Java的语句以分号;作为结束。


1.10.4 语句块:

夹在一对大括号{ }中的所有语句合起来叫做一个语句块。


1.10.5 类:

类是Java的核心,也是面向对象编程的最基本概念。关于类的介绍后面会详细展开,现在你只要知道:无论多小的Java程序,总是由一个或多个类组成。没有定义类的Java程序是不存在的。


1.10.6 方法:

System.out.println是什么?其实它是一个方法,或者说是一个功能模块。Java的方法和C语言的函数相比,概念上是完全一样的。只是Java非要叫method,不愿意叫function,所以你同样可以把它叫函数,不过这不是Java的术语。


1.10.7 main方法:

main方法是Java程序的运行入口,不过Java的main比起C要复杂那么一点点,它必须写成下面这个形式才可以:

public static void main(String[] args) {

  // 各种语句;

}

Java源文件命名:Java源文件不能随便取名,它一定要取这个文件中public class的那个class名,包括大小写也必须是一样的。例如上面的例子,源文件名一定要叫Welcome.java。

为什么有这么奇怪的规定?因为每一个class都可以有自己的main函数,而main函数就是程序的入口,因此JVM只会进入public class所拥有的那个main函数开始运行。

 

CHAPTER1 SUMMARY

1. A computer is an electronic device that stores and processes data.

2. A computer includes bothhardwareandsoftware.

3. Hardware is the physical aspect of the computer that can be touched.

4. Computer programs, known assoftware, are the invisible instructions that control the hardware and make it perform tasks.

5. Computer programmingis the writing of instructions (i.e., code) for computers to perform.

6. The central processing unit (CPU)is a computer’s brain. It retrieves instructions from memoryand executes them.

7. Computers use zeros and ones because digital devices have two stable states, referred to by convention as zero and one.

8. A bit is a binary digit 0 or 1.

9. A byte is a sequence of 8 bits.

10. A kilobyte is about 1,000 bytes, a megabyte about 1 million bytes, a gigabyte about 1 billion bytes, and a terabyte about 1,000 gigabytes.

11. Memory stores data and program instructions for the CPU to execute.

12. A memory unit is an ordered sequence of bytes.

13. Memory is volatile, because information is lost when the power is turned off.

14. Programs and data are permanently stored onstorage devicesand are moved to memory when the computer actually uses them.

15. The machine languageis a set of primitive instructions built into every computer.

16. Assembly languageis alow-level programming languagein which a mnemonic is used to represent each machine-language instruction.

17. High-level languagesare English-like and easy to learn and program.

18. A program written in a high-level language is called asource program.

19. A compiler is a software program that translates the source program into a machinelanguage program.

20. The operating system (OS)is a program that manages and controls a computer’s activities.

21. Java is platform independent, meaning that you can write a program once and run it on any computer.

22. Java programs can be embedded in HTML pages and downloaded by Web browsers to bring live animation and interaction to Web clients.

23. The Java source file name must match the public class name in the program. Java source code files must end with the.javaextension.

24. Every class is compiled into a separate bytecode file that has the same name as the class and ends with the.classextension.

25. To compile a Java source-code file from the command line, use thejavaccommand.

26. To run a Java class from the command line, use thejavacommand.

27. Every Java program is a set of class definitions. The keywordclassintroduces a class definition. The contents of the class are included in ablock.

28. A block begins with an opening brace ({) and ends with a closing brace (}).

29. Methods are contained in a class. To run a Java program, the program must have a mainmethod. Themainmethod is the entry point where the program starts when it is Executed.

30. Every statementin Java ends with a semicolon (;), known as thestatement terminator.

31. Reserved words,orkeywords,have a specific meaning to the compiler and cannot be used for other purposes in the program.

32. In Java, comments are preceded by two slashes (//) on a line, called aline comment,or enclosed between/*and*/on one or several lines, called ablock commentor paragraph comment. Comments are ignored by the compiler.

33. Java source programs are case sensitive.

34. Programming errors can be categorized into three types:syntax errors,runtime errors, andlogic errors.Errors reported by a compiler are called syntax errors orcompile errors.Runtime errors are errors that cause a program to terminate abnormally. Logic errors occur when a program does not perform the way it was intended to.


0 0