SCJP

 

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.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值