string

23
03/22/22 ITK 275 1 Strin g String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1: FFFF218C FFFF218C This is a String!! s2: FFFF218C public String s; s = "This is a String!!"; s = s.toUpperCase(); s: FFFF218C FFFF218C This is a String!! FFFF21BD THIS IS A STRING!!

Upload: kato-blanchard

Post on 30-Dec-2015

21 views

Category:

Documents


0 download

DESCRIPTION

public String s1,s2; s1 = "This is a String!!"; s2 = s1;. String. String class objects are immutable. public String s; s = "This is a String!!"; s = s.toUpperCase();. public String s; s = "This is a String!!"; ChangeToUpper(s);. public void ChangeToUpper (String s) { - PowerPoint PPT Presentation

TRANSCRIPT

04/19/23 ITK 275 1

String

String class objects are immutable

public String s1,s2;

s1 = "This is a String!!";s2 = s1;

s1: FFFF218C

FFFF218C This is a String!!

s2: FFFF218C

public String s;

s = "This is a String!!";s = s.toUpperCase();

s: FFFF218C

FFFF218C This is a String!!

FFFF21BD THIS IS A STRING!!

04/19/23 ITK 275 2

String public String s;

s = "This is a String!!";

ChangeToUpper(s);

s:

public void ChangeToUpper (String s){

s = s.toUpperCase();}

s: FFFF218C

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

04/19/23 ITK 275 3

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

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]

04/19/23 ITK 275 4

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

a0

1

2

3

Any Problem?

4size

How to fix?

04/19/23 ITK 275 5

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

a0

1

2

3

4size

04/19/23 ITK 275 6

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:

04/19/23 ITK 275 7

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

04/19/23 ITK 275 8

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

04/19/23 ITK 275 9

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

FFFF00A5 3 [0]

4 [1]

7 [2]

0 [3]

Deep Copy

04/19/23 ITK 275 10

public class Stack implements Cloneable{

public int size, top=-1;public int[] stk;........// This is the copy constructorpublic 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

Clone method for deep copy

04/19/23 ITK 275 11

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?

04/19/23 ITK 275 12

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 constructorthis.a = obj.a;

}

public String print() { return "- : "+this.a; }

}

Copy constructor vs. clone

04/19/23 ITK 275 13

public class B extends A {public int b;

public B(int b) { // constructorsuper(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

04/19/23 ITK 275 14

public class C extends B {public int c;

public C(int c) { // constructorsuper(c+1);this.c = c;

}

public C(C obj) { // copy constructorsuper(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

04/19/23 ITK 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]

04/19/23 ITK 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 - : 100 : 1 : 0[1] - : 2 : 1 - : 100 : 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

100

04/19/23 ITK 275 17

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

100

04/19/23 ITK 275 18

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 ypublic 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

04/19/23 ITK 275 19

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 exceptionwhen super is notcloneable

04/19/23 ITK 275 20

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

04/19/23 ITK 275 21

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

04/19/23 ITK 275 22

......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 bpublic 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 - : 2 : 1 : 0[1] - : 2 : 1 - : 3 : 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

04/19/23 ITK 275 23

The difference between copy-constructor and clone method

// clone a to bpublic 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 ypublic static void ACopy(A[] x, A[] y) { for (int i=0; i<x.length; i++) y[i] = new A(x[i]);}