Chapter II: Identifiers, Keywords and Types

A: method call definition and a method of
definition of the method: the return value of the method name (parameter list) {method modifier
method body
}
If no return value will be written as: void
list of parameters: Parameter Name Parameter Type
called method: method name (parameter values)

The next day:
Objectives: 1. identifiers, keywords and the type of presentation;
2. how to build class?
--------------------------- ---------------------------------
Chapter II: Identifiers, Keywords and Types
objectives: 1 comment effect: the programmer-visible part only,
not the compiler compiler, the virtual machine is executed; increase the readability of the program
positions: front longitudinal class declaration, after the method declaration attribute declarations, the method body.
Almost, but not a key word in a source file at any location
is inserted in the Notes.
Type: 1) single-line comments: // text-- from "//" to the end of all the characters of the Bank as a comment and are ignored by the compiler
2) Multi-line comments: / * text * / - From "/ *" to "* /" between all the characters will be ignored by the compiler
3) documentation comments: / ** text * / - from "/ *" to "* /" between all the characters will be ignored by the compiler.
When such comments appear in any statement (such as the class declaration, declaration or class member variables of the class
declaration member methods) before when, as the content will JavaDoc documentation;
example: 1) // Package Penalty for declaration
Package Penalty for CH01; / / allowed;
2) Package / * * Declaration Package / CH01;


4) System.out./*out content to console*/println("Hello Java"); //允许;
5) System.out.print/*out content to console*/ln("Hello Java"); //不允许;


javadoc -d doc -author -version *.java

2. 分号、块和空格
1) 每个语句短语以;结束 ;
2) 代码段以{}结束;
3) 空白处(空格、tab键、新行和回车(几个语句短语连接在一起))是无关紧要的。

3. Identifier: class name, methods and variables
1) java identifier letters, _ $ beginning and can not begin with a number, followed by the letters, numbers, "_" and "$" combination;
2) case-sensitive
3) there is no length limit.
4) You can not use java keywords
, for example: legal identifier illegal identifier
the try the try #
GROUP_7 7GROUP
Opendoor Open-Door
boolean1 boolean
4. keyword
keyword Java language program code special characters. Comprising: a
. --Class class and interface declarations, the extends, the implements, interface
. Packages and package declaration introduced --import, Package
. Datatypes --boolean, byte, char, double, float, int, long, short
one. Alternatively such data type value --false, to true, null
. flow control --break, Case, Continue, default, do, the else, for, IF, return, Switch, the while
. exception handling --catch, finally, throw , throws, the try
. modifier --abstract, final, native, private, protected, public, static, synchronized, transient, volatile
. 操作符——instanceof
. 创建对象——new
. 引用——this, super
. 方法返回类型——void
Java语言的保留字是指预留的关键字,它们虽然现在没有作为关键字,但在以后的升级版本中有可能作为关键字。
. 所有关键字都是小写;
. friendly, sizeof不是Java语言的关键字,这有别于C++;
. 程序中标识符不能以关键字命名;

5. 基本类型:程序的基本功能是处理数据,程序用变量来表示数据;
程序中必须先定义变量才能使用;
定义变量是指设定变量的数据类型和变量的名字,
定义变量的基本语法为:
数据类型 变量名;

Java语言把数据类型分为基本类型和引用类型。
基本数据类型
数值类型
整数类型:byte,short,int,long
浮点数类型:float,double
布尔类型:boolean
字符类型:char
引用数据类型
类类型:比如String
接口类型
数组类型

接下来,我们主要关注基本数据类型,关注各种基本数据类型
有什么样的取值范围?占用多少内存空间?

6. boolean类型

位置 boolean类型变量取值
------------------------------------------------------------
Java源程序 只能是true或false
class文件 用int或byte表示boolean
虚拟机中 用整数0来表示false, 有任意一个非零整数表示true

强调,在Java源程序中不允许把整数或null赋给boolean类型的变量,
这是有别于其它语言(如c语言)的地方. 例:

boolean isMarried = 0; //编译出错,提示类型不匹配
boolean isMarried = null; //编译出错,提示类型不匹配

7. 文本数据类型——char和String

1) 字符编码:Java语言对文本字符
采用Unicode字符编码。
由于计算机内存只能存取二进制数据,
因此必须为各个字符进行编码。
所谓字符编码,是指用一串二进制数据来表示特定的字符。
常见的字符编码包括:
a. ASCII字符编码
ASCII--
Amecian Standard Code for Information Interchange(
美国信息交换标准代码).
主用于表达现代英语
和其他西欧语言中的字符。
它是现今最通用的单字节编码系统,
它只用一个字节的7位,
一共表示128个字符。

b. ISO-8859-1字符编码,又称为Latin-1, 是国际标准化组织(ISO)为西欧语言中的字符制定的编码,
用一个字节(8位)来为字符编码,与ASCII字符编码兼容。所谓兼容,是指对于相同的字符,它的ASCII字符编码
和ISO-8859-1字符编码相同。

c. GB2312字符编码
它包括对简体中文字符的编码,
一共收录了7445个字符(6763个汉字+682个其他字符. 它与ASCII字符编码兼容。

d. GBK字符编码
对GB2312字符编码的扩展,
收录了21886个字符(21003个字符+其它字符), 它与GB2312字符编码兼容。

e. Unicode字符编码:
由国际Unicode协会编制,
收录了全世界所有语言文字中的字符,是一种跨平台的字符编码。
UCS(Universal Character Set)是指采用Unicode字符编码的通用字符集。
Unicode具有两种编码方案:
. 用2个字节(16位)编码,被称为UCS-2, Java语言采用;
. 用4个字节(32位)编码,被称为UCS-4;

f. UTF字符编码
有些操作系统不完全支持16位或32位的Unicode字符编码,UTF(UCS Transformation Format)字符编码能够把
Unicode字符编码转换为操作系统支持的编码,常见的UTF字符编码包括UTF-8, UTF-7和UTF-16.

2) char的几种可能取值
Java语言采用UCS-2字符编码,字符占2个字节。
字符a的二进制数据形式为 0000 0000 0110 0001
十六进制数据形式为 0x0061
十进制数据形式为 97

以下4种赋值方式是等价的:

char c = 'a';
char c = '\u0061'; //设定"a"的十六进制数据的Unicode字符编码
char c = 0x0061; //设定"a"的十六进制数据的Unicode字符编码
char c = 97; //设定"a"的十进制数据的Unicode字符编码

3) 转义字符

Java编程人员在给字符变量赋值时,通常直接从键盘输入特定的字符,
而不会使用Unicode字符编码,因为很难记住各种字符的Unicode
字符编码值。对于有些特殊字符,比如单引号,如不知道它的
Unicode字符编码,直接从键盘输入编译错误:
char c = '''; //编码出错

为了解决这个问题,可采用转义字符来表示单引号和其他特殊字符:

char c = '\'';
char c = '\\';

转义字符以反斜杠开头,常用转义字符:

\n 换行符,将光标定位到下一行的开头;
\t 垂直制表符,将光标移到下一个制表符的位置;
\r 回车,将光标定位到当前行的开头,不会移到下一行;
\\ 反斜杠字符
\' 单引号字符

8. 整数类型
byte, short, int和long都是整数类型,并且都是有符号整数。
与有符号整数对应的是无符号整数,两者的区别在于
把二进制数转换为十进制整数的方式不一样。
. 有符号整数把二进制数的首位作为符号数,
当首位是0时,对应十进制的正整数,
当首位是1时,对应十进制的负整数。
对于一个字节的二进制数, 它对应的十进制数的取值范围是-128 - 127。
. 无符号整数把二进制数的所有位转换为正整数。
对于一个字节的二进制数, 它对应的十进制数的取值范围是0 - 255。

在Java语言中,为了区分不同进制的数据,八进制数以“0”开头,十六制以“0x”开头。举例:

一个字节的二进制数 八进制数 十六进制数 有符号十进制数 无符号十进制数
-------------------------------------------------------------------------------------------------
0000 0000 0000 0x00 0 0
1111 1111 0377 0xFF -1 255
0111 1111 0177 0x7F 127 127
1000 0000 0200 0x80 -128 128

如果一个整数值在某种整数类型的取值范围内,就可以把它直接赋给这种类型的变量,否则必须进行强制类型的转换。

byte = 13;

如129不在byte类型的取值范围(-128-127)范围内,则必须进行强制类型的转换。

byte b = (byte)129; //变量b的取值为-127.

如果一个整数后面加上后缀——大写"L"或小写"l", 就表示它是一个long类型整数。以下两种赋值是等价的:

long var = 100L; //整数100后面加上大写的后缀"L",表示long型整数;
long var = 100l; //整数100后面加上大写的后缀"l",表示long型整数;

Java语言允许把八进制数(以"0"开头), 十六进制数(以"0x"开头)和十进制数赋给整数类型变量,例如:

int a1 = 012; // 012 octal, decimal value of the variable a1 is 10
int a2 = 0x12; // hexadecimal number 0x12, a decimal value of the variable a2 18 is
int = 12 is A3; // 12 is a decimal number, the decimal value of the variable is 12 a3

int a4 = 0xF1; // 0xF1 is a hexadecimal number, the decimal value of the variable a4 is 241
byte b = (byte) 0xF1 0xF1 // hexadecimal number, the decimal value of the variable b is -15
Note:
Data type of conversion:
1) cast: high precision the low precision conversion
byte B = (byte) 129;
2) implicit type conversion: low accuracy to higher accuracy conversion
byte <short <int <long < float <double

9. floating-point type

Floating-point type represents fractional components. There are two types of floating point in Java:

. Float: 4 bytes, a total of 32 bits, called single precision floating point;
Double:. 8 bytes of 64 bit double precision floating point is called;

type float and double follow IEEE754 standard, which are 32-bit and 64-bit floating point binary data representing a predetermined form.

float = 1 (number sign) +8 (index, base-2) +23 (mantissa)
Double. 1 = (number sign) + 11 (index, base-2) +52 (mantissa)

By default, the decimal and decimal digital representation is a double scientific notation,
it can be directly assigned to a variable of type double.

D1 = 1000.1 Double;
Double D2 = + 1.0001E. 3; // use decimal numbers represented in scientific notation, d2 actual value of 1000.1
Double D3 = 0.0011;
Double D4 = 0.11E-2; // decimal scientific notation numerical, d4 actual value of 0.0011

If the double data type directly assigned to a variable of type float, it may cause loss of precision, it must be mandatory conversion, whether
it will result in a compilation error, such as:

float f1 = 1.0 // compile error, must be cast;
a float F2 = 1; // valid, the integer assigned to f2, f2 value of 1.0;
a float F3 = (a float) 1.0; // legal, f3 the value of 1.0;
float F4 = (float) 1.5E + 55; // legal, 1.5E + 55 beyond the range of the float type,
F4 values is positive infinity
System.out.println (f3); // Print 1.0;
System.out.println (F4); // Print Infinity

-------------------------------------------------- --------------------------------------------------
Float.NaN non-numeric
Float.POSITIVE_INFINITY infinity
Float.NEGATIVE_INFINITY negative infinity

float f1 = (float)(0.0/0.0); //f1的取值为Float.NaN
float f2 = (float)(1.0/0.0); //f2的取值为Float.POSITIVE_INFINITY
float f3 = (float)(-1.0/0.0); //f3的取值为Float.NEGATIVE_INFINITY
System.out.println(f1); //打印NaN;
System.out.println(f2); //打印Infinity
System.out.println(f3); //打印-Infinity

Java语言之所以提供以上特殊数字, 是为了提高Java程序的健壮性,并且简化编程。当数字运算出错时,可以用浮
点数取值范围内的特殊数字来表示所产生的结果。否则,如果Java程序在进行数学运算遇到错误时就抛出异常,会影
响程序的健壮性,而且程序中必须提供捕获数学运算异常的代码块,增加了编程工作量。

10. 变量的申明和赋值

程序的基本功能是处理数据
程序用变量来表示数据;
程序中必须先定义变量才能使用;
定义变量是指设定变量的数据类型和变量的名字,定义变量的基本语法为:

数据类型 变量名;

Java语言要求变量遵循先定义,再初始化,然后使用的规则。变量的初始化是指自从变量定义以后,首次给它赋初始
值的过程。例:

int a; // define variable A
A =. 1; // initialize variables A
A ++; // use the variable A
int b = A; // b defined variables, initialization variables b, using the variable A;
b ++; // use the variable b

11. The recommended naming convention

1) class names start with a capital letter;
2) interface names begin with a capital letter;
3) Method names start with a lowercase letter;
4) variable names start with a lowercase letter;
5) constant names all uppercase, a plurality of words with "_" connection;

12. object appreciated
understand what is the object-oriented programming
oriented software development system, the method of the object as a collection of objects,
the object is the smallest subsystem, a group of related objects can be combined into more complex
subsystems. Object-oriented software development method system as a collection of objects
close to the natural way of thinking.

Object is an abstract problem areas in the event. Object has the following characteristics:

1) 万物皆为对象。问题领域中的实体和概念都可以抽象为对象。
例如学生,成绩单、教师、课和教室。
2) 每个对象都是惟一的。正如世界上不存在一模一样的叶子。
3) 对象具有属性和行为。
例如小张,性别女,年龄22,身高1.6m, 体重40kg, 能够学习,唱歌。
小张的属性包括姓名、性别、年龄、身高和体重,行为包括学习、唱歌。
例如一部手机,牌子是诺基亚、价格是2000元,银白色,能够拍照、打电话和收发短信等。这部手机的属性包括
品牌类型type、价格price和颜色color,行为包括拍照takePhoto(),打电话call(),收发
短信receiveMessage()和发短信sendMessage().
4) 对象具有状态。状态是指某个瞬间对象的各个属性的取值。
对象的某些行为会改变对象自身的状态,即属性的取值。
例如小张本来体重为40kg,经为减肥后,体重减到45kg.

肥胖状态: 40kg
|
| 减肥行为
|
肥胖状态: 35kg

5) 每个对象都是某个类的实例。小张和小王都属于学生类、
中国和美国都属于国家类、中文和英文都属于语言类。
类是具有相同属性和行为的对象的集合。

All instances of the same class have the same attributes, but in fact the same property values differ,
but their status is not necessarily the same. For example, Zhang and Wang are considered
in the student category, have the name, sex, age, height and weight of these properties,
but they have different property values.

All instances of the same class have the same behavior, which means that they have some of the same functionality.

13. Create a class

category is a set of templates with the same attributes and behavior of objects.
The main task of object-oriented programming is that each class definition in the object model.

package sample;

public class Teacher {

/**attributes of a teacher*/
private String name;
private int age;
private double salary;

/** Creates a new instance of Teacher */
public Teacher(String name, int age, double salary) {
this.salary = salary;
this.age = age;
this.name = name;
}
/**operations on properties */
/** get the name of this teacher */
public String getName() { return name; }
/**get the salary of this teacher */
public double getSalary() { return salary; }
/**get the age of teacher teacher */
public int getAge() { return age; }
……
}

Code analysis:

1) package sample;
包声明语句,将Java类放到特定的包中,便于类的组织、权限访问和区分名字相同的类。
2) public class Teacher {...}
类的声明语句,类名为Teacher,
public修饰符意味着这个类可以被公开访问;

声明类的格式:

class 类名 {
类内容
}

3) private String name;
类的属性(也称为成员变量)的声明语句;
Teacher类有一个name属性,字符串类型,
private修饰符意味着这个属性不能被公开访问。
4) public String getName() { return name; }
方法的声明语句和方法体
方法名为getName,不带参数,
String表明返回类型为String。
public表明这个方法可以被公开访问。
getName后紧跟的大括号为方法体,代表getName的具体实现。

声明方法的格式:

返回值类型 方法名 (参数列表) {
方法体
}

返回值类型是方法的返回数据的类型, 如果返回值类型为void, 表示没有返回值。
方法名是任意合法的标识符;
参数列表可包含零个或多个参数,参数之间以逗号","分开。
方法体每个语句用";"结束;
方法体中使用return语句返回数据或结束本方法的执行;

注:不介绍构造方法。在面向对象章节会提到。

14. 创建实例

public static void main(String[] args) {
Teacher gzhu = new Teacher("George Zhu", 30, 10000);
System.out.println("Teacher: " + gzhu.getName());
System.out.println("\tAge: " + gzhu.getAge());
System.out.println("\tSalary: " + gzhu.getSalary());
}

main()方法是Java应用程序的入口点,
每个Java应用程序都是从main()方法开始运行的。
作为程序入口的main()方法必须同时符合以下几个条件:
. 用public static修饰;
. 返回类型为void;
. 方法名为main;
. 参数类型为String[];
包含main方法的类又叫主程序类。

类创建好之后,通过new关键字创建具体对象。它有以下作用:
. 为对象分配内存空间,将对象的实例变量自动初始化为其变量类型的默认值;
. 如实例变量显示初始化,将初始化值赋给实例变量;
. 调用构造方法;
. 返回对象的引用;

注:结合实例,并画内存分配图讲解。

15. 基本类型和引用类型的区别

1) 基本类型代表简单的数据类型,比如整数和字符。
引用类型代表复杂数据类型,引用类型所引用的
实例包括操纵这种数据类型的行为。

通过"."运算符,就能访问引用变量所引用的实例的方法.
2) 基本类型Java虚拟机会为其分配数据类型实际占用的内存空间;
引用类型仅是一个指向堆区中某个实例的指针。

例:public class Counter {
int count = 13;
}

Counter counter = new Counter();

counter引用变量-------------> Counter实例
count变量(占4个字节,值为13)

counter引用变量的取值为Counter实例的内存地址。
counter引用变量本身也占一定的内存空间,
到底占用多少内存空间取决于Java虚拟机的实现,这对Java程序是透明的。

注:counter引用变量到底位于Java虚拟机的运行时数据区的哪个区?
取决于counter变量的作用域,
如果是局部变量,则位于Java栈区;
如果是静态成员变量,则位于方法区;
如果是实例成员变量,则位于堆区;


面向对象编程的步骤:
1)从需求中寻找名词性短语,构建class,有相应的属性和行为
2)产生对象
3)访问属性,方法(.)

一.基本数据类型变量和引用数据类型变量:
1)使用基本数据类型定义的变量叫做基本数据类型变量
int a=10;
保存是数据本身的数值(二进制)
2)使用引用数据类型定义的变量叫做引用数据类型变量
Teacher t=new Teacher();
保存是堆区开辟的地址
new关键字:
a)在栈区开辟空间保存对象的引用
b)在堆区开辟空间保存对象本身
c)堆区的地址赋值给栈区的对象的引用
二:运行时数据区
一个完整的Java程序运行过程会涉及以下内存区域
方法区/代码区:存放类的信息
堆区:用来存放动态产生的数据,比如new出来的对象。
注意创建出来的对象只包含属于各自的成员变量,
并不包括成员方法。因为同一个类的对象拥有各自
的成员变量,存储在各自的堆中,但是他们共享该
类的方法,并不是每创建一个对象就把成员方法复制一次。
栈区:保存局部变量的值,包括:
1.用来保存基本数据类型的值;
2.保存类的实例,即堆区对象的引用(指针)。
也可以用来保存加载方法时的帧
常量池:JVM为每个已加载的类型维护一个常量池,
常量池就是这个类型用到的常量的一个有序集合。
常量池存在于堆中。
PC寄存器:指向下一条需要执行的指令,将该数据传递给CPU

Guess you like

Origin www.cnblogs.com/Diyo/p/11079045.html