Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/11/2016ITK 2751 String String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1:FFFF218C This is a String!! s2:FFFF218C.

Similar presentations


Presentation on theme: "3/11/2016ITK 2751 String String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1:FFFF218C This is a String!! s2:FFFF218C."— Presentation transcript:

1 3/11/2016ITK 2751 String String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1:FFFF218C This is a String!! s2:FFFF218C public String s; s = "This is a String!!"; s = s.toUpperCase(); s:FFFF218C This is a String!! FFFF21BD THIS IS A STRING!!

2 3/11/2016ITK 2752 String public String s; s = "This is a String!!"; ChangeToUpper(s); s: public void ChangeToUpper (String s) { s = s.toUpperCase(); } s:FFFF218C This is a String!! FFFF21BD THIS IS A STRING!! s:FFFF21BDs:FFFF218C String class objects are immutable This function does not have any side effect

3 3/11/2016ITK 2753 Array is mutable int[] a; a = new int[4]; for (int i=0;i<a.length;i++) a[i] = i*i; } swap(a,1,2); public void swap(int[] a, int i, int j){ int temp; temp = a[i]; a[i]=a[j]; a[j]=temp; } a:FFFF218C 0 [0] 1 [1] 4 [2] 9 [3] FFFF218C 0 [0] 4 [1] 1 [2] 9 [3] a:FFFF218C i:1 j:2 FFFF218C [0] [1] [2] [3]

4 3/11/2016ITK 2754 Renew an Array... int a = new int[6];... RenewArray(a,4); // renew array a to size 4... public static void RenewArray(int[] a, int size) {... a = new int[size];... } a 0 1 2 3 4 5 a 0 1 2 3 Any Problem? 4 size How to fix?

5 3/11/2016ITK 2755 Renew an Array... int a = new int[6];... a = RenewArray(a,4); // renew array a to size 4... public static int[] RenewArray(int[] a, int size) {... a = new int[size];... return a; } a 0 1 2 3 4 5 a 0 1 2 3 4 size

6 3/11/2016ITK 2756 Copy the reference class Stack { private int size,top=-1; private int[] stk;..... }...... FFFF218C 3 [0] 4 [1] 7 [2] 0 [3] size:4 top:2 stk:FFFF218C public static void main(...) { Stack a,b; a = new Stack(4);.... b = a;.... } Stack a:Stack b:

7 3/11/2016ITK 2757 Shallow Copy class Stack { private int size,top=-1; private int[] stk;..... }...... Shallow Copy Shall copy can be done by the default clone FFFF218C 3 [0] 4 [1] 7 [2] 0 [3] size:4 top:2 stk:FFFF218C public static void main(...) { Stack a,b; a = new Stack(4);.... b = (Stack) a.clone();.... } Stack a:Stack b:...... size:4 top:2 stk:FFFF218C

8 3/11/2016ITK 2758 public class SillyStack implements Cloneable { public int size,top=-1; public int[] stk;.... public Object clone() { try { return (Object) super.clone(); } catch(CloneNotSupportedException e) { return null; }.... } Cloneable Shall copy can be done by the default clone marker interface

9 3/11/2016ITK 2759 Deep Copy Deep copy needs to be programed by the programmer class Stack { private int size,top=-1; private int[] stk;..... }...... Shallow Copy FFFF218C 3 [0] 4 [1] 7 [2] 0 [3] size:4 top:2 stk:FFFF218C public static void main(...) { Stack a,b; a = new Stack(4);.... b = (Stack) a.clone();.... } Stack a:Stack b:...... size:4 top:2 stk:FFFF00A5 3 [0] 4 [1] 7 [2] 0 [3] Deep Copy

10 3/11/2016ITK 27510 public class Stack implements Cloneable { public int size, top=-1; public int[] stk;.... // This is the copy constructor public Stack(Stack s) { this.top = s.top; this.size = s.size; this.stk = new int[this.size]; for (int i=0;i<this.size;i++) { this.stk[i] = s.stk[i]; }.... } Copy constructor for deep copy public static void main(...) { Stack a,b; a = new Stack(4);.... b = Stack(a);.... } Using the copy constructor

11 Clone method for deep copy 3/11/2016ITK 27511 public class Stack implements Cloneable { public int size, top=-1; public int[] stk;.... public Object clone() { Stack newStack; newStack = new Stack(this.size); newStack.top = this.top; for (int i=0;i<this.size;i++){ newStack.stk[i] = stk[i]; } return (Object) newStack; }.... } public static void main(...) { Stack a,b; a = new Stack(4);.... b = (Stack) a.clone();.... } Using the clone method Why bother? Why can we just use copy constructor?

12 3/11/2016ITK 27512 public class A { public int a; public A(int a) {// This is the constructor this.a = a; } public A(A obj) { // This is the copy constructor this.a = obj.a; } public String print() { return "- : "+this.a; } Copy constructor vs. clone

13 3/11/2016ITK 27513 public class B extends A { public int b; public B(int b) { // constructor super(b+1); this.b = b; } public B(B obj) { // copy constructor super(obj); // call the copy constructor // of the super class this.b=obj.b; } public String print() { return super.print()+" : "+this.b; } Subclass B

14 3/11/2016ITK 27514 public class C extends B { public int c; public C(int c) { // constructor super(c+1); this.c = c; } public C(C obj) { // copy constructor super(obj); // call the copy constructor // of the super class this.c = obj.c; } public String print() { return super.print()+" : "+this.c; } Subclass C A + a B + b C + c

15 3/11/2016ITK 275 15 A[] a = new A[2]; B[] b = new B[2]; C[] c = new C[2]; for (int i=0; i<2; i++) { a[i] = new A(i); b[i] = new B(i); c[i] = new C(i); } printArray(a,b,c); System.out.println("**"); for (int i=0; i<2; i++) { a[i] = b[i]; b[i] = c[i]; c[i].a = 100; } printArray(a,b,c); Arrays of A, B, C a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] - : 0 - : 1 : 0 - : 2 : 1 : 0 [1] - : 1 - : 2 : 1 - : 3 : 2 : 1 ** [0] [1]

16 3/11/2016ITK 275 16 A[] a = new A[2]; B[] b = new B[2]; C[] c = new C[2]; for (int i=0; i<2; i++) { a[i] = new A(i); b[i] = new B(i); c[i] = new C(i); } printArray(a,b,c); System.out.println("**"); for (int i=0; i<2; i++) { a[i] = b[i]; b[i] = c[i]; c[i].a = 100; } printArray(a,b,c); Simple Assignment....... ** [0] - : 1 : 0 - : 100 : 1 : 0 [1] - : 2 : 1 - : 100 : 2 : 1 a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] [1] 100

17 3/11/2016ITK 27517 A[] a = new A[2]; B[] b = new B[2]; C[] c = new C[2]; for (int i=0; i<2; i++) { a[i] = new a(i); b[i] = new b(i); c[i] = new c(i); } printArray(a,b,c); System.out.println("**"); for (int i=0; i<2; i++) { a[i] = new B(b[i]); b[i] = new C(c[i]); c[i].a = 100; } printArray(a,b,c); Using copy constructor....... ** [0] - : 1 : 0 - : 2 : 1 : 0 - : 100 : 1 : 0 [1] - : 2 : 1 - : 3 : 2 : 1 - : 100 : 2 : 1 a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] [1] B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 100

18 3/11/2016ITK 27518 A[] a = new A[2]; B[] b = new B[2]; C[] c = new B[2]; for (int i=0; i<2; i++) { a[i] = new A(i); b[i] = new B(i); c[i] = new C(i); } printArray(a,b,c); System.out.println("**"); ACopy(c,a); // copy c to a printArray(a,b,c); Problem of using copy constructor // copy x to y public static void ACopy(A[] x, A[] y) { for (int i=0; i<x.length; i++) y[i] = new A(x[i]); } a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] [1] A a:2 A a:3 ……………………………………… ** [0] - : 2 - : 1 : 0 - : 2 : 1 : 0 [1] - : 3 - : 2 : 1 - : 3 : 2 : 1

19 3/11/2016ITK 27519 public class X implements Cloneable { public int x; public X(int x) { this.x = x; } public X(X obj) { // This is the copy constructor this.x = obj.x; } public String print() { return ": "+x; } public Object clone() { X n; try { n = (X) super.clone(); } catch (CloneNotSupportedException e) { return null; } n.x = this.x; return (Object) n; } Superclass X implements Cloneable Marker interface To handle exception when super is not cloneable

20 3/11/2016ITK 27520 public class Y extends X implements Cloneable{ public int y; public Y(int y) { // constructor super(y+1); this.y = y; } public Y(Y obj) { // copy constructor super(obj); // call the super’s constructor this.y = obj.y; } public Object clone() { Y n; n = (Y) super.clone(); // super is cloneable n.y = this.y; return (Object) n; } public String print() { return super.print()+" : "+y; } Subclass Y

21 3/11/2016ITK 27521 public class Z extends Y implements Cloneable{ public int z; public Z(int z) { // constructor super(z+1); this.z = z; } public Z(Z obj) { // copy constructor super(obj); // call the super’s constructor this.z = obj.z; } public Object clone() { Z n; n = (Z) super.clone(); // super is cloneable n.z = this.z; return (Object) n; } public String print() { return super.print()+" : "+z; } Subclass Z

22 3/11/2016ITK 27522...... for (int i=0; i<2; i++) { x[i] = new X(i); y[i] = new Y(i); z[i] = new Z(i); } AClone(y,x); AClone(z,y); printArray(x,y,z); Using clone method // clone a to b public static void AClone(X[] a, X[] b) { for (int i=0; i<a.length; i++) b[i] = (X) (a[i].clone()); } x: y: z: [0] [1] [0] [1] X x=0 X x=1 Y x:1 y:0 Y x:2 y:1 Z x:2 y:1 z:0 Z x:3 y:2 z:1 [0] [1] [0] - : 1 : 0 - : 2 : 1 : 0 [1] - : 2 : 1 - : 3 : 2 : 1 Y x:1 y:0 Y x:2 y:1 Z x:3 y:2 z:1 Z x:2 y:1 z:0

23 3/11/2016ITK 27523 The difference between copy-constructor and clone method // clone a to b public static void AClone(X[] a, X[] b) { for (int i=0; i<a.length; i++) b[i] = (X) (a[i].clone()); } copy-constructor statics determined during the compiling time  clone method dynamic determined during the running time // copy x to y public static void ACopy(A[] x, A[] y) { for (int i=0; i<x.length; i++) y[i] = new A(x[i]); }


Download ppt "3/11/2016ITK 2751 String String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1:FFFF218C This is a String!! s2:FFFF218C."

Similar presentations


Ads by Google