//5.14 null as arg for ctor class sample { sample(String s) { System.out.println("String"); } sample(Object o) { System.out.println("Object"); } //will be ambiguous syntax error: // sample(StringBuffer sb) // { /// System.out.println("StringBuffer"); // } } class constructor { public static void main(String arg[]) { //matches both STring and Object, does more specific, ie. String sample s1=new sample(null); // sample s2=new sample(12); //no such ctor error // sample s3=new sample(); //no such ctor error } } /* //5.13 static methods bound at compile-time! class Child extends Parent { public static int test (int i) { return 20; } public static void main(String [] args) { Parent c = new Child(); System.out.println(c.test(10)); } } class Parent { public static int test(int i) { return 5; } } */ /* //5.6 interface Observer {} class ApBase implements Runnable { public void run(){} } class ApDerived extends ApBase implements Observer {} class arf { public static void main(String[] args) { ApBase aBase = new ApBase(); ApDerived aDer = new ApDerived(); Runnable rn = aDer; // Observer ob = aBase; //syntax error //Observer ob2 = (Observer) aBase; //compiles but runtime error //aDer = aBase; //compile error: sub = super //ApDerived ad2 = (ApDerived) aBase; //runtime error: sub = (sub)super aBase = aDer; //super = sub aBase = (ApBase)aDer; //redundant cast //because super ref has actual sub ref, can cast to sub: if (aBase instanceof ApDerived) { System.out.println("actual super is instanceof sub now"); ApDerived ad2 = (ApDerived) aBase; } if (aDer instanceof ApBase) System.out.println("sub is always instanceof super"); } } */ /* //5.3 class arf { static String s; public static void main(String[] args) { //int index = args[0].indexOf('a'); //int index = args[0].indexOf("a"); int index = args[0].indexOf("asdf"); System.out.println(index); //-1 if not found String tmp = args[0].substring(0,index); //index cannot be -1 //String tmp = args[0].substring(0,args[0].indexOf('a')); System.out.println(tmp); } } */ /* //4.3 class Char { static int var; public static void main(String arg[]) { while(var < 10) { System.out.println("Hello"); } while(var < 10) { } do; while(false); do { ; } while(false); } } */ /* //4.1 class exam41 { static String s; public static void main(String[] args) { System.out.println(s==null?"is null":"not null"); amethod(s); } static void amethod(String s) throws NullPointerException { float factor; try { //factor = Float.valueOf(args[0]).floatValue(); factor = Float.valueOf(s).floatValue(); System.out.println("worked"); } catch (NullPointerException e) { System.out.println(e.getMessage()); throw e; // factor = Float.NaN; // } catch (NullPointerException e) { // System.out.println("null pointer exception: "+e.getMessage()); } finally { System.out.println("in finally"); } System.out.println("at end of main"); } } */ /* //3.9 abstract class Ghost { // void haunt(); //syntax error abstract void haunt(); void haunt2() {} } */ /* //3.2 class arrayinits { public static void main(String[] args) { //int [][]a1 = {{1,2},{1},{},{1,2,3}}; //int [][]a1 = new int[][]{{1,2},{1},{},{1,2,3}}; int [][]a1 = {{1,2},new int[2]}; // int []a1 = new int [2]{1,2}; //syntax bad for (int i=0; i