// B.java
class A
{
}
public class B
{
}
class C
{
}
2. for boolean, there are only true and false, no -1, 0,1in C++, you can write like this:
while(1)
{
}
but in Java, while(1) is wrong, and you can only use:while (true)
{
}
3. define array
//-1
int num[];
num=new int[3]; //ok;
int num[]=new int[3];//ok
int num[]={1,2,3};//ok
int num[]=new int[]{1,2,3};//ok
int num[]=new int[3]{1,2,3};//error
int num[3]; //error
//-2
int [][] num;
num=new int[2][];
num[0]=new int[3]; //3 elements
num[1]=new int[4]; //4 elements
4. for loopfor(int i=0; i<5; i++)
{
int a=i; //ok
}
int b=i;// error, i is not defined out of the for loop
5. print hex numberint hx=0xffffffff; int hc=hx<<2; System.out.println(Integer.toHexString(hc));6. default value
int 0; char '\0' boolean false object null7. same name for parameter and member
class Point
{
int x,y;
void func1(int x, int y)
{
x=x; //parameter to parameter, not what we want
y=y; //parameter to parameter, not what we want
}
void func2(int x, int y)
{
this.x=x; //parameter x to member x
this.y=y; //parameter y to member y
}
}
8. call another construction method class Point
{
int x,y;
Point(int a, int b)
{
x=a;
y=b;
}
Point()
{
this(3,4);// call another construct method, and should be the first statement
}
}
9. static
static methods could call static methods and access the static variable, could not call the non-static methods or access variable;
while non-static methods could call static methods and access to static variable.
10.. final variable
In most cases, the final variable should be initialized when it is defined.
class Point
{
final double PI=3.1415926;
}
one exception:
class Point
{
final double PI;
Point()
{
PI=3.1415926;
}
}
however, if the final variable is also static, you must initialize it when defining it:static final double PI=3.1415926;11. call parent method
public class MyAnimal
{
MyAnimal()
{
System.out.println("I am Animal! without parameter");
}
MyAnimal(int param)
{
System.out.println("I am Animal! with parameter");
}
}
class MyFish extends MyAnimal
{
public MyFish()
{
// in this place, super()is called even if you do not write it;
// so if there is no MyAnimal() at all, error would happen.
// in that case, you could use super(0) if MyAnimal(int param) is defined
// or you could redifine the MyAnimal()
System.out.println("I am fish! without parameter");
}
public MyFish(int param)
{
//in this place, still super(),not super(0), is called even if you do not write it;
// to call MyAnimal(int param), should write super(0) in this place
// 0 represents the parameter, you can use anyone you like
System.out.println("I am fish! with parameter!");
}
public void breathe()
{
super. breathe(); // use this call parent breathe();
System.out.println("Fish breathe!");
}
}
12. polymorphic
class Parent
{
void fun1()
{
System.out.println(" Parent 1");
}
void fun2()
{
System.out.println(" Parent 2");
}
}
class Child extends Parent
{
void fun1()
{
System.out.println(" Child 1");
}
public static void main(String[] args)
{
Child cd=new Child();
Parent pt=cd;//Ok
pt.fun1(); // result: Child 1, because of new Child();
pt.fun2(); // result: Parent 2, because Child dose not override fun2()
Parent pt1=new Parent();//Ok
Child cd1=pt1;//error, can not convert from Parent to Child
}
}
No comments:
Post a Comment