北大青鸟南昌德弘中心32班学员王昊在经过S1到Y2的学习,现在到 java 后期的学习了。总结出了java的基础知识总结:
1 、 Identifiers :标识符
①Names of class,method and variable:用于类名、方法名、变量名
②Begin with character,”_” or “$”:标识符不能以数字开头
③Case sensitive:大小写敏感(区分大小写)
④No length limitation:长度无限制
⑤标识符不能是Java关键字,汉字也可以做标识符,但是不建议使用(使用汉字涉及到编码问题,跨平台时回出现问题)。
⑥ String 是 Java 的一个类,类名是标识符,所以 String 可以做标识符。
⑦There is no sizeof operator. Java 中没有 sizeof 运算符 ,所以 sizeof 可以作为标识符
⑧关键字、保留字(const、 goto 、true、 false 、null)不能用作标识符。
1publicclass Identifiers {
2publicstaticvoid main(String[] args) {
3 String $abc=”abc1″;
4 String _abc=”abc2″;
5//编译错误,标识符不能以数字开头
6//String 8abc=”abc3″;
7 String 中国=”China”;
8 String String=”wanghao”;
9int Integer=22;
10//Java中没有sizeof运算符,所以sizeof可以作为标识符
11 String sizeof=”sizeof”;
12 System.out.println(String);//wanghao
13 System.out.println(Integer);//22
14 }
15 }
2 、 Keywords : 关键字
The goto and const keyword are not used in the Java programming.
const 保留关键字(留儿不用)。const是从C++继承而来的,但是Java并不使用const这个关键字,由于JVM是由C++编写的,因此,虽然Java并不使用const这个关键字,但却要保留下来。和const一样goto也是保留关键字!
关键字全部用小写表示(53个)
Strictly speaking,the literals true,false,and null are not keywords but literals;however,the distinction is academic.
true,false,and null是保留字但不是关键字,只是3个值,不能用来做关键字。
3 、 Primitive data types :基本数据类型
Java强数据类型只能先声明后使用。
boolean Boolean literals indicating true or false
char Stores one 16-bit unicode character
char 同时具备字符和整数的特征。
byte 8-bit integer
short 16-bit integer
int 32-bit integer
long 64-bit integer
Integer Data Types- byte ,short,int and long
float 32-bit floating-point number
double 64-bit floating-point number
在范围内的常数可以自动转换类型,但是变量就不一定了。
小————》大自动转换
大————》小强制类型转化
Byte相当于截取了最后8位,前面的全部舍去
1publicclass Demo {
2publicstaticvoid main(String[] args) {
3int i1 = 5; //语句1 Right
4
5byte b1=5; //语句2 Right
6byte b2=3; //语句3 Right
7//byte b3=128; //语句4 Wrong 128超出了byte的范围
8byte b3=(byte)128; //语句5 Right
9//byte的取值范围:(-128,127)
10byte b4=127;
11byte b5=-128;
12//byte b6=-129; //-129超出了byte的范围
13
14 long l1=5; //语句6 Right
15long l2=5L; //语句7 Right
16//b1=i1; //语句8 Wrong i1不能转换成b1
17 b1=(byte)i1; //语句9
18 l1=i1; //语句10 Right
19//i1=123L; //语句11 Wrong
20 i1=(int)123L; //语句12
21
22//b1=b1+b2; //语句13 Wrong b1+b2变成int
23 b1=(byte)(b1+b2); //语句14
24 }
25 }
程序分析:
① 整形数据默 ( byte,short,int and long ) 认类型是 int 型,因此语句 1 正确
② 占用位数小的类型(简称 “ 小类型 ” )向占用位数多的类型(简称 “ 大类型 ” )可以自动转换,相反,大类型到小类型则要通过 强制类型转换 。
如:语句 6 是由 int—->long( 自动转换 ) 。
语句 11 是由 long—->int( 不能进行转换,提示“可能损失精度”的错误 ) ,必须通过强制类型转换,改正后为语句 12 。
③ 语句 2 和语句 3 是由 int—->byte( 自动转换 ) ,这又是怎么回事呢?
int可自动转换为byte和short而无需强制类型转换,这是因为byte和short表示的范围较小,所有小于int的整型运算,先转换为int类型,再参与运算。例如: 15 和 20 都在 byte 范围内但是 15*20 却超出了 byte 的表示范围,则要存放在 int 类型变量里面。
④ 既然int可以自动转换为byte和short而不需强制类型转换,那么为什么语句8会出错,需要改成语句9这样的强制类型转换呢?
语句8表面上是int—->byte但是i1是一个int型的变量,它表示(-65535—-65535)范围的整数,不能确定它的值,因此需要强制类型转换。语句4中128超出了byte的表示范围,因此不能赋值给byte变量b3需要进行强制类型转换,如语句5。
⑤ 强制类型转换的实质是什么呢?
例如语句5执行的结果是:-128,这是为什么呢?
这正是强制类型转换后的结果。 int 类型是 32-bit , 128 的二进制表示形式为: 00000000,00000000,00000000,10000000 。 Byte 类型是 8-bit ,要把 int 类型强制转换成 byte 类型,则只截取其后 8-bit ,把前 24-bit 去除掉。因此 byte b3=(byte)128; 执行后的结果的二进制表示形式为: 10000000 ,对应的整数即为 -128 。
⑥ 所有小于 Int 的整型运算,先转成 int 再运算,因此语句 13 出现错误,需要进行强制类型转换,如语句 14 。
public class float {
public static void main(String[] args) {
double d1=123.0;
double d2=123.0F;
//编译错误 Default data type is double
//float f1=123.0;
//需要强制类型转换
float f1=(float)123.0;
//int和long可以自动转换为float和double
float f2=123;
float f3=123L;
double d3=123;
double d4=123L;
}
}
Float Point Data Type-float and double
① Default data type is double
② A float data is 32-bit long
③ A double data is 64-bit long
④ A float data ends with ‘f’ or ‘F’
⑤ A double data can end with ‘d’ or ‘D’
在计算机领域对于 float 和 double 只能表示他们的近似值。因为小数可以无限,但是计算机表示的精度是有限的。
⑥ int 和 long 可以自动转换为 float 和 double
Java 变量类型:
① 基本类型: boolean,byte,char,shot,int,long,float,double
② 引用类型:所有类、接口、数组、枚举、标注
Recommended Naming Rules
①Class names begin with uppercase character
②Interface names begin with uppercase character
③Method names begin with lowercase character
④Variable names begin with lowercase character
⑤Constant variable names are in uppercase character,不同单词间用“_”隔开,例如:USER_NAME。
⑥Package names are in lowercase character
规范我们可以违背,但是我们提倡遵守,养成一种良好的习惯!
4 、 Operators
Assignment operators
= += -= %= *= /= <<= >>= >>>= &= ^= |=
1publicclass ByteDemo {
2publicstaticvoid main(String[] args) {
3byte b1=2;
4byte b2=3;
5 b1=(byte)(b1+b2); // 语句1 加法,转int
6 b1+=b2; //语句2 赋值,不转int
7 }
8 }
常识
b1+=b2; 是否和语句 b1=b1+b2 完全等价?
答案是否定的,上面程序中的语句1和语句2给出了证明。语句1中byte类型参数与运算时要先转换为int型,因此要进行强制类型转换。而语句2中,自动进行类型转换。(我的理解:我们可以把“b11+=b2;”看做是对“b1=(byte)(b1+b2);”的优化!)
Comparison operators
> >= < <= instanceof
Equality operators
= = !=
Arithmetic operators
+ – * / %
Shift operators
>> << >>>
Bitwise operators
& | ^(按位异或) ~(按位取反)
Logic operators
&& (逻辑与) & || (逻辑或) | !(逻辑非)
&&和&都是逻辑与,它们之间的区别:&&是短路逻辑与,有短路的效果,效率更高。
||和|都是逻辑或,它们之间的区别:||是短路逻辑或,有短路的效果,效率更高。
Conditional operators
? :
Other operators
++ —
1publicclass Test5 {
2publicstaticvoid main(String [] args){
3int i=2;
4 System.out.println(i++); //2
5 System.out.println(i); //3
6int a=i++ + ++i; //3+5=8
7 System.out.println(a);
8 System.out.println(i); //5
9for(int j=0;j<1000;j++){
10 i=i++;
11 }
12 System.out.println(i); //5
13 }
14 }
程序分析:
为什么经过 1000 次循环后, i=i++; 的结果仍然是 5 ?
(老师讲解) ++ 的运算符优先级高于 =( 赋值 ) 运算符, i++ 的运算结果为 5 ,进入寄存器,这时寄存器中存放的值为 5 。然后 i 的值变为 6 ,继续执行赋值语句,赋值时,把寄存器中的值赋 5 值给 i ,因此 i 的值是 5 和 6 之间来回切换的!
(我的理解)++的运算符优先级高于=(赋值)运算符,因此这个表达式的执行顺序如下:①运算i++这个表达式的结果为5 。—-> ②i的值自加1(i++先参与运算,再自加1),即i的值变为6。—-> ③将i++这个表达式的值,即第①部的结果赋值给变量i。(“=”表达式是将右边表达式的值赋值与左边的变量,因此是将5赋于i,而不是将6赋于i)。整个循环过程i的值在5和6之间来回切换。bdqn0791