/* abstract class A { A() {System.out.println("A's ctor");} //ctor in abstract class //abstract static void absstat(); //syntax error } class X extends A { //char literals: what if not bfnrt... //char c12 = '\s'; //syntax error static void abstat() {System.out.println("implemnted abstract static");} //final int i = 5/0; //runtime error, not compile-time public static void main(String [] args) { X x = new X(); int i12 = 3000000000; //3G int i13 = 0xfffffffff; //9 f's byte b1 = (byte) 101.99; //101 byte b2 = (byte) 128.1; //-128 byte b3 = (byte) 123456789.0; //21 System.out.println(""+b1+" "+b2+" "+b3); boolean b=true; char c = 'a'; // if (b == c) ; //syntax error // if (c == "hi") ; //syntax error //sibling classes: Y y = new Y(); W w = new W(); if (y.equals(w)) ; // if (y == w) ; //syntax error if (y == x) ; // if (y instanceof String) ; //syntax error: inconvertible Boolean bo = new Boolean("asdfasdf"); //false System.out.println("bo="+bo); int a = 1; a += ++a + a++; System.out.println("a="+a); int a12 = +a; //unary + System.out.println("a12="+a12); } public void f() {System.out.println("X's f()");} static void g(){} } interface I { void f(); } class W extends X {} class Y extends X implements I { //by inheriting X, implents I? yes // void g() {} //override static by non-static? NO } */ /* class X { static int i; void f() {System.out.println("X's f");} } class Y extends X { void f() {System.out.println("Y's f i="+i);} //static i inherited void g() {super.f();} //any method can call overridden super method public static void main(String [] args) { Y y = new Y(); y.f(); y.g(); X [] xarr = new Y[5]; Y [] yarr = (Y [])xarr; } } */ /* class X { public static void main(String [] args) { char c4='\7'; char c5='\77'; char c6='\177'; int a=2; System.out.println(" "+ (++a + ++a * ++a) + " " + a); byte b1=127, b2=1; byte sum = (byte)(b1+b2); byte b3 = ++b2; //cast to byte not needed for ++ System.out.println("sum="+sum+" b3="+b3); int i=Integer.MAX_VALUE; i +=1; //wrap-around to neg. System.out.println("i= "+i); byte b=-11; short sh = 1; char c = (char)b; //must cast any number to char char c2 = (char)sh; System.out.println("c="+c+" c2="+c2); //mod sign is sign of first operand System.out.println("7%3="+(7%3)+" -7%3="+(-7%3)+" -7%-3="+(-7%-3)+ " 7%-3="+(7%-3)); //System.out.println(b1 > i ? 5 : "hi"); //both must be same type System.out.println(b1 > i ? 5 : 4); System.out.println("Float.MAX_VALUE="+Float.MAX_VALUE+ " Float.POSITIVE_INFINITY="+Float.POSITIVE_INFINITY+ " Float.NaN="+Float.NaN); //negative inf if numerator is negative System.out.println("1.0/0="+(1.0/0)+" -1.0/0="+(-1.0/0) + " 1.0/-0="+(1.0/-0)+" -1.0/-0="+(-1.0/-0)); //Nan: System.out.println("0.0/0="+(0.0/0)+" 7%0.0="+(7%0.0)+ " 1.0/0 %2="+(1.0/0%2)); } } */ /* class X { public static void main(String [] args) { try { throw new Exception(); // throw new Throwable(); // throw new Object(); //error can't throw Object } catch (RuntimeException e) {System.out.println("caught it");} catch (Exception e) {System.out.println("caught it"); System.exit(1);} catch (Throwable e) {System.out.println("caught throwable it");} //catch (Object e) {System.out.println("caught object it");} //error finally {System.out.println("finally");} } } */ /* class X { public static void main(String [] args) { byte b1 = (byte)0xff; //ff=255, (byte)255=-1 byte b2 = 0xffffffff; //=-1 System.out.println("b1="+b1+" b2="+b2); int a = 37, b=1234; a ^= b; //swaps ints b ^= a; a ^= b; System.out.println("a="+a+" b="+b); a = 1; // ++a++; //error.postfix first, value is not variable for prefix // (++a)++; //error b= 3; System.out.println("a+++b= "+(a+++b)); //4 postfix before prefix } } */ /* class X { int X; X() {} void X(){ //int X; //error, in inner scope {int X; } int X; //var defined from point of declaration to end of scope } } */ /*class X { int i=10; void f() {i++; System.out.println("X's f");} void g() {i++; System.out.println("X's g");} static void h() {System.out.println("X's h");} } class Y extends X { int i; //shadow void g() {i++; System.out.println("Y's g");} //overridding static void h() {System.out.println("Y's h");} //shadow int getSuperI() {return super.i;} void callSuperG() {super.g();} } class test { public static void main(String [] args) { Y y = new Y(); y.f(); //inherited f accesses base X's i System.out.println("y.i="+y.i); System.out.println("Y's super i="+y.getSuperI()); System.out.println("((X)y).i="+((X)y).i); System.out.println(); X x = new Y(); System.out.println("x.i="+x.i); //accesses X's i System.out.println("((Y)x).i="+((Y)x).i); //accesses Y's i! System.out.println(); x.h(); //X's static h() ((Y)x).h(); //Y's h() System.out.println(); //overriding: y.g(); //Y's overriding g() ((X)y).g(); //also Y's overriding g() x.g(); //also Y's overriding g() ((X)x).g(); //also Y's overriding g() //need call of super.g() in Y to access X's g() y.callSuperG(); //((X)y).callSuperG(); //error: X does not have method //x.callSuperG(); //error: X does not have method ((Y)x).callSuperG(); } } */ /* interface I { void m(); } abstract class X implements I {} class Y extends X { public void m() {} } */ /* class X { int [] A = new int[100000]; static long calls; void inf_recursion() { calls++; inf_recursion(); } public static void main(String [] args) { Runtime rt = Runtime.getRuntime(); System.out.println("total JVM memory: "+rt.totalMemory()); System.out.println("free JVM memory: "+rt.freeMemory()); //test stack overflow: // try{ // new X().inf_recursion(); // } catch (Error e) { // System.out.println(" "+calls); //gets to 90233 // } //test out of memory: int i=0; X [] arr = new X[100000]; //references try { while (true) { //System.out.println(" "+i); arr[i++] = new X(); //up to 163 = ~64MB before OutOfMemoryError } } catch (OutOfMemoryError e) { System.out.println(e.getMessage()+" "+i); // Runtime rt = Runtime.getRuntime(); System.out.println("total JVM memory: "+rt.totalMemory()); System.out.println("free JVM memory: "+rt.freeMemory()); System.gc(); System.out.println("total JVM memory: "+rt.totalMemory()); System.out.println("free JVM memory: "+rt.freeMemory()); } System.out.println("still running... "); } } */ /* misc. from Exploring Java class X { private X() { //what happens from derived class call of super()? System.out.println(" howdy from X ctor"); } X(int i) { //but avoiding default ctor is OK System.out.println(" howdy from X ctor"); } public static void main(String [] args) { int m=-2147483648; System.out.println("Integer.MIN_VALUE: "+ Integer.MIN_VALUE + " m="+m); m = 0x80000000; System.out.println(" m="+m); m = +5; //unary + System.out.println(" m="+m); int n; if (true) n = 2; n+=1; //OK, complier knows that init happens X x = new X(); //String s = (String)x; //compiler error: inconvertible types // Y y = new Y(); //private ctor of base X doesn't allow Y to extend int [] A = new int[5]; // A.length = 6; //error: .length is final variable! System.out.println(" "+A.length); Object o=null; System.out.println(o.toString()); //runtime error: null pointer //k=2; //error to use before declare int k; //int p=q; //same error. int q=2; // if (k instanceof int) //error: must be class or array type // ; // for (int i=0, int j=0; i<5; i++,j++) //error int j... for (int i=0, j=0; i<5; i++,j++) ; // return 2; // return 3; //error: unreachable } } class Y extends X { //test private ctor of base class. compiler error Y(int i) {super(i);} //unless avoid use of super(), for super(int) } */ /* //interface I { abstract class I { abstract void f(int); //error: must have name of arg. Not C++ prototype } */ /* class X { final int i; //syntax error: final must be initialized } */ /* //private and final methods inheritance class A { private void f() {System.out.println("A's f");} final void g() {System.out.println("A's g");} } class B extends A { // void f() {System.out.println("B's f");} //can't overrirde private // void g() {System.out.println("B's g");} //can't overrirde final public static void main (String[] args) { B b = new B(); // b.f(); //f is private in superclass b.g(); A a = new B(); // a.f(); //f is private in superclass a.g(); } } */ //8.11 /* class D { public static void main (String[] args) { Boolean b1 = Boolean.valueOf("trUE"); // 1 Boolean b2 = Boolean.valueOf("Even more true"); // 2 Boolean b3 = Boolean.valueOf(null); // 3 System.out.print((b1==b2) + ","); System.out.print((b2==b3) + ",b2 is:"+b2+" b3 is:"+b3); System.out.println(b3==b1); }} */ /* class D { public static void main (String[] args) { Integer b1 = Integer.valueOf("121"); // 1 Integer b2 = Integer.valueOf("121"); // 2 // Integer b3 = Integer.valueOf(null); // 3 //System.out.print((b1==b2) + ","); System.out.print((b1==b2) + ",b1 is:"+b1+" b2 is:"+b2); //System.out.println(b3==b1); }} */ /* //5.30 abstract class A { private int x = 4; private int y = 2; public int x() {return x;} public void x(int x) {this.x = x;} public int y() {return y;} public void y(int y) {this.y = y;} } interface B {int math();} class C { static class D extends A implements B { public int math() {return x()+y();} } // static A a1 = new A() implements B { //syntax error: implements static A a1 = new A() { public int math() {return x()+y();} }; public static void main(String[] args) { System.out.print(new C.D().math()); // System.out.print(a1.math()); //syntax error: math method not in A }} */ /* //5.25 class MWC104 { public static void main (String[] args) { char[] c = {'a','b','c','d'}; String s1 = new String(c); //char array can be arg to String ctor! boolean b = true; for (int i = 0; i < s1.length(); i++) { //add () to length b &= (c[i] == s1.charAt(i)); } System.out.print(b); }} */ /* //5.11 class C { public static void main(String[] args) { Boolean b1 = Boolean.valueOf(true); Boolean b2 = Boolean.valueOf(true); Boolean b3 = Boolean.valueOf("TrUe"); Boolean b4 = Boolean.valueOf("tRuE"); System.out.print((b1==b2) + ","); System.out.print((b1.booleanValue()==b2.booleanValue()) + ","); System.out.println(b3.equals(b4)); }} */ /* //5.6 class SRC104 { public static void main (String[] args) { System.out.println(Math.round(Float.NaN)); //int 0 System.out.println(Math.round(Float.POSITIVE_INFINITY)); //2G-1 System.out.println(Math.round(Float.NEGATIVE_INFINITY)); //-2G }} */ /* //4.29 abstract class A { private int x = 4; private int y = 2; public int x() {return x;} public void x(int x) {this.x = x;} public int y() {return y;} public void y(int y) {this.y = y;} public abstract int math(); } class B { //stupid ; at end of anonymous class declration static A a1 = new A() {public int math() {return x()+y();}}; static A a2 = new A() {public int math() {return x()-y();}}; static A a3 = new A() {public int math() {return x()*y();}}; static A a4 = new A() {public int math() {return x()/y();}}; public static void main(String[] args) { System.out.print("" + a1.math() + a2.math() + a3.math() + a4.math()); } } */ /* //4.9 class Shorts { public static void main(String args[]) { new Short("1"); new Short("-1"); //new Short("1.0");//runtime error. string must be decimal integer lierteral //new Short("0x1"); //runtime error new Short("011"); //leading 0 discarded, i.e. not octal! } } */ /* //3.24 class B { private static String s1 = "s1"; final String s2 = "s2"; B () {new Z("s5","s6");} static class Z { final String s3 = "s3"; static String s4 = "s4"; Z (final String s5, String s6) { System.out.print(s1); //static nested can access static fields and methods of enclosing class by simple name //System.out.print(s2); //can not access non-statics of enclosing System.out.print(s3); System.out.print(s4); System.out.print(s5); System.out.print(s6); }} public static void main(String args[]) {new B();} } */ /* //3.19 class Primitives { static void printFloat(float f) {System.out.println(f);} static void printDouble(double d) {System.out.println(d);} public static void main(String[] args) { byte b = 1; // 1 short s = b; // 2 char c = (char)s; // 3 char c2 = (char)b; int i = c; // 4 long l = i; // 5 float f = l; // 6 printFloat(i); // 7 printFloat(l); // 8 printDouble(l); // 9 }} */ /* //3.11 class EBH202 { static boolean a, b, c; public static void main (String[] args) { boolean x = (a = true) || ((b = true) && (c = true)); //even with extra parens the ||'s left is done first System.out.print(a + "," + b + "," + c); }} */ /* //2.27 class A {void m1(A a) {System.out.print("A");}} class B extends A {void m1(B b) {System.out.print("B");}} class C extends B {void m1(C c) {System.out.print("C");}} class D extends C { void m1(D d) {System.out.print("D");} public static void main(String[] args) { //A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D(); A a1 = new D(); B b1 = new D(); C c1 = new D(); D d1 = new D(); //same d1.m1(a1); d1.m1(b1); d1.m1(c1); d1.m1(d1); }} */ /* class A { int field; A() { System.out.println("hi from A ctor"); field = 1; } } class B extends A { int field; //shadows inherited from A B() { System.out.println("hi from B ctor"); field = 2; } int getf() { //return super.field; return ((A)this).field; //same } public static void main(String[] args) { B b = new B(); System.out.println(b.field); System.out.println(b.getf()); } } */ /* class B { static void f(int i) {} static void g(short i) {} public static void main(String[] args) { //f(1.0); //f(1L); g(1); //implicit cast of literal NOT done for arguments } } */ /* //2.4 class SRC102 { public static void main (String[] args) { int i1 = Math.round(0.5f); //float arg returns int int i2 = Math.round(1.5d); //double arg returns long. (double) needed System.out.print(i1 + "," + i2); }} */ /* //1.26 //methods called left to right class EBH001 { static int m(int i) { System.out.print(i + ", "); return i; } public static void main(String s[]) { System.out.println(m(m(1) - m(2) + (m(3) * m(4)))); }} */ /* //1.22 class GFC100 { public static void main(String[] args) { // final short s1 = 1; // 1 // final char c1 = 1; // 2 short s1 = 1; // 1 char c1 = 1; // 2 byte b1 = s1; // 3 byte b2 = c1; // 4 byte b3 = 1; // 5 byte b4 = 1L; // 6 byte b5 = 1.0; // 7 byte b6 = 1.0d; // 8 int i1 = 1L; //long, float, double NOT autoconverted to any smaller type int i2 = 1.0; long l1 = 1.0f; }} */ /* class JSC201 { static byte m1() { final char c = '\u0001'; return c; //ok inited final is compile-time constant // 1 } static byte m3(final char c) {return c;} // 2 syntax error public static void main(String[] args) { char c = '\u0003'; System.out.print(""+m1()+m3(c)); }} */ /* //1.9 class A {A() throws Exception{}} class B extends A {B() throws Exception{}} class C extends A {C() {try {super();}catch(Exception e){}}} */ /* //1.7 class A {A(int i){}} class B extends A{} //class B extends A{B(int i){}} //still no A() error */ /* //two mains: no problem public class chisholm { public static void main(String[] args) { System.out.println("hi from A"); } } class B { public static void main(String[] args) { System.out.println("hi from B"); } } */