Variables, Strings and Arrays in Java. Wednesday, September 14, 16

Variables, Strings and Arrays in Java YHL/SPM Wednesday, September 14, 16 Container Class 1 Which variable is referenced? Which variables are vi...
Author: William Mathews
2 downloads 2 Views 200KB Size
Variables, Strings and Arrays in Java

YHL/SPM

Wednesday, September 14, 16

Container Class

1

Which variable is referenced? Which variables are visible? public class B0 { public int i0 = 0; public Integer ii0 = new Integer(0); public B0( ) { } }

i0, ii0 visible in B0

public class B1 extends B0 { public int i1 = 1; public Integer ii1 = new Integer(1); public B1( ) { } }

i0, ii0 from B0, i1 and ii1 from B1 are all visible in B1

public class B2 extends B1 { public int i0 = 2; public int i1 = 2; public Integer ii0 = new Integer(2); public Integer ii1 = new Integer(2); public B2( ) { } public int getB1i1( ) {return super.i1;} } YHL/SPM

Wednesday, September 14, 16

i0, ii0, i1 and ii1 from B2 are all visible in B2. getB1i1 returns the i1 declared in B1.

Container Class

2

Referencing the variables public class T { public static void main(String[ ] args) { B0 b0 = new B0( ); B1 b1 = new B1( ); B2 b2 = new B2( ); System.out.println("b0.i0 = "+b0.i0); // b0.i0 = 0 // NOT DECLARED System.out.println("b0.i1 = "+b0.i1); System.out.println("b0.ii0 = "+b0.ii0.intValue( )); // b0.ii0 = 0 // NOT DECLARED System.out.println("b0.ii1 = "+b0.ii1.intValue( )); System.out.println("b1.i0 = "+b1.i0); // b1.i0 = 0 System.out.println("b1.i1 = "+b1.i1); // b1.i1 = 1 System.out.println("b1.ii0 = "+b1.ii0.intValue( )); // b1.ii0 = 0 System.out.println("b1.ii1 = "+b1.ii1.intValue( )); // b1.ii1 = 1 ... } }

YHL/SPM

Wednesday, September 14, 16

Container Class

3

public class T { public static void main(String[ ] args) { ... System.out.println("b2.i0 = "+b2.i0); // ??? System.out.println("b2.i1 = "+b2.i1); // b2.i1 = 2 System.out.println("b2.ii0 = "+b2.ii0.intValue( )); // ??? System.out.println("b2.ii1 = "+b2.ii1.intValue( )); // b2.ii1 = 2 System.out.println("b2.super i1 = "+b2.getB1i1( )); // ??? b1 = b2; System.out.println("b1.i0 = "+b1.i0); // ???? System.out.println("b1.i1 = "+b1.i1); // ??? System.out.println("b1.ii0 = "+b1.ii0.intValue( )); // ??? System.out.println("b1.ii1 = "+b1.ii1.intValue( )); // ??? } } YHL/SPM

Wednesday, September 14, 16

Container Class

4

public class T { public static void main(String[] args) { ... System.out.println("b2.i0 = "+b2.i0); // b2.i0 = 2 System.out.println("b2.i1 = "+b2.i1); // b2.i1 = 2 System.out.println("b2.ii0 = "+b2.ii0.intValue( )); // b2.ii0 = 2 System.out.println("b2.ii1 = "+b2.ii1.intValue( )); // b2.ii1 = 2 System.out.println("b2.super i1 = "+b2.getB1i1( )); // b2.super i1 = 1 b1 = b2; System.out.println("b1.i0 = "+b1.i0); // b1.i0 = 0 System.out.println("b1.i1 = "+b1.i1); // b1.i1 = 1 System.out.println("b1.ii0 = "+b1.ii0.intValue( )); // b1.ii0 = 0 System.out.println("b1.ii1 = "+b1.ii1.intValue( )); // b1.ii1 = 1 } }

YHL/SPM

Variables are not resolved polymorphically. Look at the type of the reference and get the variable visible in that class. Container Class

Wednesday, September 14, 16

5

Another example -- Base Class public class BaseC { public int k = 1; public int j = 2; public Integer v = new Integer(8); public BaseC( ) { } public void bCaller(DerivedC dArg) { System.out.println("bCaller"); System.out.println("k = "+k); System.out.println("j = "+j+", v.intValue( ) = "+v.intValue( )); System.out.println("dArg.j = "+dArg.j+", dArg.v.intValue( ) = "+dArg.v.intValue( )); } }

YHL/SPM

Wednesday, September 14, 16

Container Class

6

public class DerivedC extends BaseC { public int j = 20; public Integer v = new Integer(80); public DerivedC( ) { } public void dCaller(BaseC bArg) { System.out.println("dCaller"); System.out.println("k = "+k); System.out.println("j = "+j+", v.intValue( ) = "+v.intValue( )); System.out.println("bArg.j = "+bArg.j+", bArg.v.intValue( ) = "+bArg.v.intValue( )); } }

YHL/SPM

Wednesday, September 14, 16

Container Class

7

public class MainC { public static void main(String[] args) { BaseC b = new BaseC( ); DerivedC d = new DerivedC( );

b

b.bCaller(d); d.dCaller(b);

DerivedC object

d

} }

YHL/SPM

Wednesday, September 14, 16

BaseC object int k 1 int j 2 Integer v

Container Class

BaseC object int k 1 int j 2 Integer v int j 20 Integer v

Integer object value = 8

Integer object value = 8

Integer object value = 80 8

public class BaseC { ... public void bCaller(DerivedC dArg) { System.out.println("bCaller"); System.out.println("k = "+k); System.out.println("j = "+j+", v.intValue( ) = "+v.intValue( )); System.out.println("dArg.j = "+dArg.j+", dArg.v.intValue( ) = "+dArg.v.intValue( )); } } Integer BaseC object object int k 1 value = 8 int j 2 Integer v

b this

bCaller k=1 j = 2, v.intValue( ) = 8 dArg.j = 20, dArg.v.intValue( ) = 80 The example doesn’t show it, but dArg.k would print 1

YHL/SPM

Wednesday, September 14, 16

DerivedC object

d dArg Container Class

BaseC object int k 1 int j 2 Integer v int j 20 Integer v

Integer object value = 8

Integer object value = 80 9

public class DerivedC extends BaseC { ... public void dCaller(BaseC bArg) { System.out.println("dCaller"); System.out.println("k = "+k); System.out.println("j = "+j+", v.intValue( ) = "+v.intValue( )); System.out.println("bArg.j = "+bArg.j+", bArg.v.intValue( ) = "+bArg.v.intValue( )); } }

bArg

b this

dCaller k=1 j = 20, v.intValue( ) = 80 bArg.j = 2, bArg.v.intValue( ) = 8

DerivedC object

d

Variables are not resolved polymorphically. Look at the type of the reference and get the variable visible in that class. YHL/SPM

Wednesday, September 14, 16

BaseC object int k 1 int j 2 Integer v

Container Class

BaseC object int k 1 int j 2 Integer v int j 20 Integer v

Integer object value = 8

Integer object value = 8

Integer object value = 80 10

Java has built-in strings • String and StringBuffer Classes • String objects are immutable – Cannot be changed after creation – Can be garbage collected by the Java system if there are no references to it. • Garbage Collection, or GC, is kind of analogous to having the system do a C free, automatically. • String literals are double quoted, i.e. “this is a string” is a string literal with the value of this is a string • A given string literal is often only stored once in memory. Always true for strings created at compile time. YHL/SPM

Wednesday, September 14, 16

Container Class

11

String uniqueness example //StringLiteralUniqueness.java class X { public static String strX = "hello"; } class Y { public static String strY = "hello"; } class Z { public static String strZ = "hell" + "o"; }

//(A) //(B) //(C)

class Test { public static void main( String[] args ) { // output: true System.out.println( X.strX == Y.strY );

//(D)

// output: true System.out.println( X.strX == Z.strZ );

//(E)

String s1 = "hel"; String s2 = "lo"; // output: false System.out.println( X.strX == ( s1 + s2 ) );

//(F)

// output: true System.out.println( X.strX == (s1 + s2).intern() );

//(G)

} } YHL/SPM

Wednesday, September 14, 16

Container Class

12

String uniqueness example //StringLiteralUniqueness.java class X { public static String strX = "hello"; } class Y { public static String strY = "hello"; } class Z { public static String strZ = "hell" + "o"; } class Test { public static void main( String[] args ) { // output: true System.out.println( X.strX == Y.strY ); // output: true System.out.println( X.strX == Z.strZ ); String s1 = "hel"; String s2 = "lo"; // output: false System.out.println( X.strX == ( s1 + s2 ) ); // output: true YHL/SPM Container Class System.out.println( X.strX == (s1 + s2).intern() ); } }

Wednesday, September 14, 16

13

String uniqueness example class X { public static String strX = "hello"; } class Y { public static String strY = "hello"; } class Z { public static String strZ = "hell" + "o"; } class Test { public static void main( String[] args ) { // output: true System.out.println( X.strX == Y.strY );

// output: true System.out.println( X.strX == Z.strZ ); String s1 = "hel"; String s2 = "lo"; // output: false System.out.println( X.strX == ( s1 + s2 ) );

//(F)

// output: true System.out.println( X.strX == (s1 + s2).intern() );

//(G)

}

YHL/SPM }

Wednesday, September 14, 16

Container Class

14

String uniqueness example class X { public static String strX = "hello"; } class Y { public static String strY = "hello"; } class Z { public static String strZ = "hell" + "o"; } class Test { public static void main( String[] args ) { // output: true System.out.println( X.strX == Y.strY ); // output: true System.out.println( X.strX == Z.strZ );

String s1 = "hel"; String s2 = "lo"; // output: false first, then true System.out.println( X.strX == ( s1 + s2 ) ); System.out.println( X.strX == (s1 + s2).intern() ); } }

YHL/SPM

Wednesday, September 14, 16

Container Class

15

How strings should be compared class X { public static String strX = "hello"; } class Y { public static String strY = "hello"; } class Z { public static String strZ = "hell" + "o"; } class Test { public static void main( String[] args ) { // output: true System.out.println( X.strX == Y.strY ); // output: true System.out.println( X.strX == Z.strZ );

String s1 = "hel"; String s2 = "lo"; // output: true System.out.println( X.strX.equals(s1 + s2)); System.out.println( X.strX == (s1 + s2).intern() ); } }

YHL/SPM

Wednesday, September 14, 16

Container Class

16

Constructing Strings and StringBuffers

• Strings are usually straightforward – String str = new String(“hello there”); – String str = “hello there”; – String str = new String( ); // empty – String str = “”; // empty • A String is not a StringBuffer – StringBuffer sb = str; // is wrong! – StringBuffer sb = “hello world” // is wrong! YHL/SPM

Wednesday, September 14, 16

Container Class

17

Constructing StringBuffers • Empty StringBuffer – StringBuffer sb = new StringBuffer(“”); – StringBuffer sb = new StringBuffer( ); • Non-empty StringBuffer – like String, except use StringBuffer • storage occupied is length of argument (in characters) + something • length is the number of characters – StringBuffer sb = new StringBuffer(1024); YHL/SPM

Wednesday, September 14, 16

Container Class

18

Other String Operations • Constructors exist to create Strings from integers, Arrays of char, etc. • Can do insert, substitute, access individual characters, and many other things possible with, e.g., Python

YHL/SPM

Wednesday, September 14, 16

Container Class

19

Remember Strings are immutable public class Immut { public static void main(String[ ] args) { String s1 = new String("012345"); String s2 = s1; s1 = s1.replace('2', 'R'); System.out.println("s1 = "+s1+", s2 = "+s2); } $ java Immut } s1 = 01R345, s2 = 012345 YHL/SPM

Wednesday, September 14, 16

Container Class

20

StringBuffers are mutable public class Mut { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer("012345"); StringBuffer sb2 = sb1; sb1 = sb1.replace(2, 2, "R"); System.out.println("sb1 = "+sb1+", sb2 = "+sb2); } }

$ java Mut sb1 = 01R2345, sb2 = 01R2345

YHL/SPM

Wednesday, September 14, 16

Container Class

21

In General . . . • Use StringBuffers – when you want mutability – Mutability desirable when doing I/O, editing strings, etc. • Use String – When you don’t want mutability – Saves some storage – You don’t need to worry about who else is referencing the changed String

YHL/SPM

Wednesday, September 14, 16

Container Class

22

Java Arrays

YHL/SPM

Wednesday, September 14, 16

Container Class

23

Java array declaration // 3 elements with in indices 0, 1 and 2 int [ ] data = new int[3]; String [ ] strs = new String[3]; // Each element holds a reference // to s string object. There is no way to put an object // itself in an array // an array with initialization int [ ] data2 = new int[ ] {1, 2, 3}; String [ ] strs2 = new String[ ] {“s1”, “s2”, “s3”}; // an illegal declaration int [ ] data3 = new int[3] {1, 2, 3}; YHL/SPM

Wednesday, September 14, 16

Container Class

24

Arrays are objects An array int[ ] intArray = new int[10]; can be declared. int[ ] is a new user defined type. Can say: Object o = intArray; int[ ] intArray2 = (int[ ]) o;

YHL/SPM

Wednesday, September 14, 16

Container Class

25