Värde och typ? Vilket värde och vilken typ har uttrycken vi skriver ut? Problem Joachim von Hacht out.println( 5 / 2 ); // 1 out.println( 5.0 / 2 ); // 2 out.println( 5 / 2 == 5.0 / 2 ); // 3 out.println('5' / '2' == 5 / 2); // 4 out.println( "5 / 2" + 5.0 / 2 ); // 5 1 Vad skrivs ut? Referenssematik (1) Förklara (i detalj)! int x; int y; x = y = 1; out.println(x + ":" + y); int[] a1 = {1, 2, 3; int[] a2 = {4, 5, 6; // Before int[] a3 = doit(a1, a2); // Call int[] doit(int[] x, int[] y) { x[0] = y[1]; return x;
Referenssemantik (2) H h1 = new H(1); H h2 = new H(2); // Before doit(h1, h2); // Call out.println(h1.i); void doit(h h1, H h2) { h1.i = 4; H tmp = h1; h1 = h2; h2 = tmp; class H { H(int i) { this.i = i; Referenssemantik (3) A[] as = new A[3]; A a1 = new A(); a1.i = 1; a1.s = "aaa"; A a2 = new A(); a2.i = 2; a2.s = "bbb"; as[0] = a1; as[1] = a2; // Before as[2] = doit(as); // Call A doit( A[] as ){ as[0].s = as[1].s; return as[0]; Referenssemantik (4) A a = new A(); a.arr = new int[]{1, 2, 3; a.d = 1.5; A b = new A(); b.arr = new int[]{4,5,6; b.d = 2.5; // Before a = doit( b); // Call int[] arr; double d; A doit( A a ){ A tmp = new A(); tmp.arr = a.arr; tmp.d = a.d; return tmp; Referenssemantik (5) B b1 = new B("b"); A a1 = new A( b1 ); // Before B b2 = doit( a1 ); // Call B b; A( B b ){ this.b = b; B doit( A a ){ a.b.s = "c"; return a.b; class B { B ( String s ){ this.s = s;
Typer (1) Vilka rader ger kompileringsfel? Vilka ger körningsfel? ( Integer <: Object och Double <: Object) Integer[] is = {1, 2, 3; Double[] ds = {1.0, 2.0, 3.0; Object o = is; // 1 o[0]++; // 2 Typer (2) Vilka rader ger kompileringsfel? Vilka ger körningsfel? A a = new A(); // 1 B b = new B(); // 2 IX ix = new IX(); // 3 IY iy = null; // 4 a = b; // 5 b = (A) a; // 6 interface IX { interface IY { Object[] os1 = is; // 3 Object[] os2 = (Object[]) is; // 4 Double[] ds1 = (Double[]) os1; // 5 os2 = ds; // 6 Double[] ds2 = (Double[]) os2; // 7 Integer i1 = os1[0]; // 8 Integer i2 = (Integer) os1[0]; // 9 ix = iy; // 7 iy = ix; // 8 ix = a; // 9 a = (A) ix; // 10 ix = b; // 11 iy = b; // 12 ix = (IX) iy; // 13 class A implements IX { class B implements IY { Typer (3) Vilka rader ger kompilerings resp. körningsfel. Varför? Om fel åtgärdas vad skrivs ut? A a = new A(); B b = new B(); IX ix = a; // 1 IY iy = a; // 2 a.doit(); // 3 ix.doit(); // 4 iy.doit(); // 5 iy.doother(); // 6 iy = b; // 7 iy = (IY) b; // 8 iy.doother(); // 9 iy = new C(); // 10 iy.doother(); // 11 interface IX { void doit(); interface IY { void doother(); class A implements IX, IY { public void doit() { out.println("a doit()"); out.println("a doother()"); class B { out.println("b doother()"); class C implements IY { out.println("c doother()"); Typer (4) Vilka exempel ger kompilerings resp. körningsfel. Varför? Om fel åtgärdas vad skrivs ut? //a A a = new B(); a.doa(); //b IA a = new X(); a.doa(); //c C c = new B(); c.doc(); //d B b1 = new C(); b1.dox(); //e IX x = new C(); X x1 = (X) x; x1.doa(); //f IX x2 = new C(); B b2 = (B) x2; b2.doc(); public interface IA {void doa(); public interface IX {void dox(); public abstract class A implements IA { public void doa() {out.println("a.doa()"); public abstract void doc(); public class B extends A { public void doc() {out.println("b.doc()"); public class C extends B implements IX { public void doa() {out.println("c.doa()"); public void dox() {out.println("c.dox()"); public void doc() {out.println("c.doc()"); public class X implements IX { public void dox() {out.println("x.dox()"); public void doa() {out.println("x.doa()");
Vad skrivs ut? Kompilerar detta? Fungerar det att köra? Alla typer <: Object Object[] o = { 0, "abc", 1.23 ; for (int i = 0; i < o.length; i++) { classify(o[i]); void classify(integer i) { out.println("it's an integer"); void classify(string s) { out.println("it's a String"); void classify(object o) { out.println("don't know, it's anything"); Vad händer? Kompilerar detta? Får vi något körningsfel? Integer[] arr = {1, 2, 3; update(arr); void update(object[] arr) { a[0] = null; a[1] = 1; a[2] = 1.0; Skissa en OO model (1)? 1.) Minsta möjliga modell för kärnan i programmet nedan (klasser). 2.) En metod för att skjuta in en ny kolumn (insert column)? Hur? Hur många Objekt (1)? Hur många objekt finns i koden? Hur kommer vi åt dessa? String[] s = { "aaa", "bbb", null; s[2] = s[0] + s[1]; A a = new A(s[2]); A[] as = new A[]{a; A( String s ){ this.s = s; Programmet
Hur många Objekt (2)? Hur många objekt finns i koden? Hur kommer vi åt dessa? new Random().nextInt(10); int s = sum(new int[]{1,2,3); for( char ch : "abc".tochararray()){ // out.println("123" + "abc"); int sum( int[] arr ){ int sum = 0; for(int i = 0 ; i < arr.length; i++){ sum += i; return sum; Klass kontra Instans Vilka rader kompilerar inte? public class Mix { public static int i = 1; public int j = 2; public static void doit() { i = 5; // 1 i = j; // 2 j = i; // 3 doother(); // 4 this.doother(); // 5 new Mix().j = 5; // 6 new Mix().doOther(); // 7 i = 5; // 8 i = j; // 9 j = i; // 10 doit(); // 11