注:*表示0或多个;+表示1个或多个
object-oriented programming
1.constructor
没有返回值
如果只定义一个带参数的constructor,则lose缺省的无参数的constructor,new xx()会出错。
2.source file layout:
定义顺序:package ?c >import ?c> class declaration
一个文件至少应该有一个class,只能有一个public class,文件名必须要和public class的名称一致,如果没有public class,对文件名没有限制。
3.package:
如果文件中没声明package,则class属于缺省包,即没有名字的包
identifiers,keywords,and type
1.identifiers:
开头以unicode letter,”_”和”$”。后面可以跟数字;(中文变量,方法名居然都可以!!!牛)
大小写敏感;
没有长度限制。
warning:
类名必须是ascii的字母。因为很多文件系统不支持unicode.(不过我试了一下,类名是中文的,compile的时候是可以通过,runtime时throw java.lang.noclassdeffounderror)
2.keywords:
几个 很生僻的keywords:
transient,strictfp,volatile
没有goto和const;没有sizeof()。
4.basic java type:
共8种。
boolean和integer type之间不能转化。
5.integral:
byte:8bit -2的7次方~2的7次方-1
short:16bit
int:32bit
long:64bit
6.floating point
float:32bit
double:64bit
浮点形默认是double.
float a = 1.02 //compile error
float a = 1.02f or float a = 1.02f //correct
7.类型的取值范围
data typesize (bits)initial valuemin valuemax value
boolean1false falsetrue
byte80-128 (-27)127 (27 ?c 1)
short160-215 215 - 1
char16‘/u0000’‘/u0000’ (0)‘/uffff’ (216 ?c 1)
int320-231 231 - 1
long640l-263 263 - 1
float320.0f1.4e-453.4028235e38
double640.04.9e-3241.7976931348623157e308
8.assignment of reference type
基本类型的赋值是值的赋值;int x =6; int y = x;相当于复制x的内容到y上。
对象的赋值不会赋值内容,两个对象的指针都是指向同一个object..
9.pass by value
pass argument by value. 当方法的参数是对象的引用时,参数的值是对象的地址,对象是可以在参数调用时改变的。
public class test{
public static void changeobject(mydate ref){
ref = new mydate(1,2,2002);
}
public static void main(string[] arv){
mydate d = new mydate(3,3,1988);
changeobject(d);
}
}
结果是d还是为1988,3,3;因为对象的地址是不变的。
10.java coding convention(编码惯例)
package ?c 名词且小写
class--名词且第一个字母大写
interface―同class
methods-动词且第一个字母小写,分隔词第一个字母大写,不用”-“
variable―第一个字母小写,不用”_”,”$”(对于inner class有意义)
constants―大写并用”_”
expression and flow control
1.
local variables―variable defined in method
instance variables―variable defined outside method
instance variable initialize:
byte,short,int,long,float,double:0
boolean:false
char:’/u0000’
all reference type:null
2.bitwise logic operators
位逻辑运算符作用于整形。(byte,char,short,int,long)
3.>>
右移是把第一个操作数/2的第二个操作数次方
e.g
128>>4 returns 128/(2的4次方)
4.优先级
助记词 运算符类型 运算符
ulcerunary+ - ++ ?c [[ rest...]],()cast
addictsarithmetic (and shift)* / % + - << >>
reallyrelational> < >= <= == !=
likelogical (and bitwise)&& || & | ^
cconditional (ternary)a > b ? x : y
a lotassignment= (and compound assignment like *=)
note:
对于int,其实是右移右操作数的求32的模;对于long,其实是右移右操作数的求64的模
int y = x >> 32 ,y没有改变,而不是0.(我试了一下,byte和short分别右移8和16,结果都为0)
4.>>>
11000>>2 returns 11110
11000>>>2 returns 00110
note:
>>>只对int,long有效。如果对于byte和short,在>>>之前先promote成int,右移完再折回byte或short,这样,通常unsigned shift becomes signed shift
5.<<
128<<4 returns 128*(2的四次方)
6.+
short x =5;
short y=6;
short z=x+y;//compile error
因为+的结果最小起码是int
7.cast
7.if()要求的是一个boolean表达式
if(x) //int x cause error
use if(x!=0)
8.switch(exp1)
exp1必须是和int兼容,(byte,short,int,char)
float,long,和类指针(string)都不允许。
9.label:statement
statement必须是循环(break 还可以是switch)
array:
1.初始化
s = new char[5] //initialize for ‘/u0000’
2.多维数组的定义
int [][] a = new int [2][];
a[0] = new int[4];
a[1] = new int[6];
system.out.println(a.length);
system.out.println(a[0].length);
result:
2
4
3.数组的复制
int a[]={1,2,3};
int b[]={4,5,6,7,8,9};
system.arraycopy(a,0,b,0,a.length);
result:
b[]={1,2,3,7,8,9}
note:
system.arraycopy拷贝的是引用,而不是object.(我试了,如果是基本类型的数组,用arraycopy后,修改其中一个数组的值,另一个数组是不变的;如果是对象的数组,则值会改变)
inheritance
1.constructors are not inherited
2.instance of 检查对象的类型(类,接口,数组)
3.cast
up cast(parent class = subclass) :直接用=转化
downward(subclass = parent class):如果该对象不是要转化的那个对象,则会在runtime的时候出错。
4.overloading method
必须有不同的参数,可以有不同的返回类型
5.overriding method
有相同的函数名,参数类型,和返回值,实现可以不一样。并且子类的方法不能比父类的函数的访问权限小。
6.super
在constructor中如果要调用super的话应该写在第一行
super能指定参数调用父类的constructor
如果在constructor中没有调用super,则compiler会隐含调用父类的”default”的constructor
如果父类没有定义非私有的“default”的constructor,则compile error
7.构造函数初始化
1.分配对象的空间,把instance variable设置default value(boolean->false,integer,float->0,reference->null)
2.
2.1绑定constructor的参数
2.2如果有this(),跳到2.5
2.3递归调用implicit 或explicit的super
2.4执行instance variable的explict的赋值
2.5执行当前的constructor.
7.constructor的rule
在constructor中调用的函数应为私有函数。
因为如果超类employee的constructor中有公有函数getdetail,类manager继承employee,而manager中override此函数getdetail,声明一个manager的对象时会递归调用employee的constructor,而因为是runtime check,实际上emplyee中调用的getdetail是manager的getdetail。
public class emploee extends object {
private string name;
private double salary = 15000.00;
private date birthdate;
private string summary;
public emploee(string n, date dob) {
name = n;
birthdate = dob;
summary = getdetails();
}
public emploee(string n) {
this(n, null);
}
private string getdetails() {
return "name: " + name + "/nsalary: " + salary
+ "/nbirth date: " + birthdate;
}
public static void main(string[] arg)
{
manager m = new manager("2","gl");
}
}
class manager extends emploee {
private string department;
public manager(string n, string d) {
super(n);
department = d;
}
public string getdetails() {
return "/ndept: " + department;
}
}
8.如果重载equals,最好重载hascode()
9.tostring
闽公网安备 35060202000074号