1. Array
int [] z [] [] = {{{1,2,3},{2,3}},{{3,4},{}}};
int [] z [] [];
=
int z [] [] [];
int [][] a1 = {{},{}};
int [] a2 = a1[0]; // Correct
int [] a3 = (int []) a1; // Error, cannot cast from int[][] to int[]
2. Implement interface
interface Base {
boolean m1 ();
byte m2(short s);
}
class Class2 implements Base {
boolean m1( ) { return false; }
byte m2(short s) { return 42; }
}
// class2 has compile error, as need specify public for both method. as interface methods are implicitly public.
3. Class.forName("")
Class c = Class.forName("TestB"); //错误,必须加上Package path, 尽管在同一package下.
Class c = Class.forName("com.nan.tml.TestB"); //正确
4. 构造函数
class Top {
public Top(String s) {
System.out.print("B");
}
}
class Bottom2 extends Top {
public Bottom2(String s) {
super("s"); //如果没有这句,会报错,因为他会自动调用supper(); 黙认会有一个无参数构造函数, 如果定义了其它的构造函数,刚不会再有无参数构造函数,如需要,需再定义.
System.out.print("D");
}
public static void main(String[] args) {
new Bottom2("C");
System.out.println(" ");
}
}
5. Argument
public class Test {
public static void main(String[] args) {
Arg arg = new Arg();
testArg(arg); // Reference argument
System.out.println(arg.v1); //Will print 2.
}
static void testArg(Arg a) {
a.v1 = 2;
System.out.println(a.v1); // Will print 2.
a = null; //Just specify the reference of argument a is null.
}
}
class Arg {
int v1 = 1;
}
6. Type cast
double > long > int > short
// Correct
short s = 1;
int i = s;
long l = i;
double d =l;
// Wrong
double d = 1;
long l = d;
int i = l;
short s = i;
class Eggs {
int doX(Long x, Long y) {
return 1;
}
int doX(long... x) {
return 2;
}
int doX(Integer x, Integer y) {
return 3;
}
int doX(Number n, Number m) {
return 4;
}
void go() {
short s = 7;
System.out.print(doX(s, s) + " "); // print 4
double d = 7;
System.out.print(doX(d, d) + " "); // print 4
int i = 7;
System.out.print(doX(i, i) + " "); // print 3
long l = 7;
System.out.print(doX(l, l) + " "); // print 1
}
public static void main(String[] args) {
new Eggs().go();
}
}
7. Overload
public class Test {
public static void main(String[] args) {
System.out.println(new Alien().invade((short) 7)); // Print a few
System.out.println(new Alien().invade((short) 7, (short)7)); // Print many
System.out.println(new Alien().invade((short) 7, (short) 7, (short) 7)); // Print many
}
}
class Alien {
String invade(short ships) {
return "a few";
}
String invade(short... ships) {
return "many";
}
}
8. Initialization
class Bird {
{
System.out.print("bl ");
}
public Bird() {
System.out.print("b2 ");
}
}
class Raptor extends Bird {
static {
System.out.print("r1 ");
}
public Raptor() {
System.out.print("r2 ");
}
{
System.out.print("r3 ");
}
static {
System.out.print("r4 ");
}
}
public class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
}
}
// Will print:
// r1 r4 pre bl b2 r3 r2 hawk
9. Serializable
class Player{
Player() { System.out.print("p"); }
}
public class Test extends Player implements Serializable {
Test() { System.out.print("c"); }
private Player player = new Player(); // will throw exception when run, as Player doesn't implements Serializable
public static void main(String[] args){
Test c1 = new Test();
try {
FileOutputStream fos = new FileOutputStream("play.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(c1);
os.close() ;
FileInputStream fis = new FileInputStream("play.txt");
ObjectInputStream is = new ObjectInputStream(fis);
is.readObject();
is.close();
} catch (Exception x ) { }
}
}
// Will print pcp
// It's okay for a class to implement Serializable even if its superelass doesn't.
// However, when you deserialize such an object, the non-serializable superelass
// must run its constructor. Remember, constructors don't run on deserialized
// classes that implement Serializable.