180 likes | 205 Views
Lecture 9: More on objects, classes, strings. discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException garbage collection more on strings equals and == explicit and implicit parameters side effects immutable classes
E N D
Lecture 9: More on objects, classes, strings • discuss hw3 • assign hw4 • default values for variables • scope of variables and shadowing • null reference and NullPointerException • garbage collection • more on strings • equals and == • explicit and implicit parameters • side effects • immutable classes • interned string values • console input • your input!
HW4 • Builds on our Date class • dayOfWeek method
Default values for variables • Data fields (instance variables) are assigned default initial values • 0 for numeric types (int, double, etc) • false for boolean • \u0000 for char • null for object references • Local variables declared inside methods are not assigned default initial values
Scope of variables • A variable declared inside a method is called a local variable • Its scope starts from its declaration and continues to the end of the smallest {…} block that contains the variable. • Before and after that it is not accessible. • Method parameters are also variables: their scope is the entire method • The scope of a data field (static or instance variable in a class) is the entire class, regardless of where it’s declared: good style to declare them all before the methods • They can be declared in any order unless the initial value of one depends on the initial value of another, but this is not good style anyway • See Oct10.java
Shadowing • Occurs when access to an instance variable is prevented by declaration of a local variable with the same name • A local variable cannot be shadowed by another local variable because their scopes are not allowed to overlap
Null reference • Player jack = null; • means jack does not point to any object • If we now invoke jack.flip(), a NullPointerException occurs • Player jill = jack; // now jill is null too
Garbage and garbage collection • Player jack = new Player(); • Player jill = new Player(); • jill = jack; // jill object is inaccessible • but if we first did • Player jasmine = jill; // copy of jill • the object IS accessible • Figuring out what is and what is not accessible is the job of the Java garbage collector: it reclaims wasted space • In C, have to explicitly release space • In Java, don’t, but can help by setting reference values to null when no longer needed
More on strings • A string is an object • Only thing special about a string is special syntax in the language • Concatenation operator + • Use of “…” to construct a string
equals and == • String s1 = “abc”; • String s2 = new String(“abc”); • String s3 = new String(“abc”); • System.out.println(s1 == s2); • System.out.println(s1.equals(s2)); • System.out.println(s2 == s3); • System.out.println(s2.equals(s3));
Explicit and implicit parameters • The explicit parameters in a method call are the actual parameters (arguments) matching the parameters in the signature • The implicit parameter in a call to an instance method is the object instance name • jack.multipleFlip(n) • s1.equals(s2) • complete lack of symmetry between these can be very confusing • our text book does not use these terms, but they are standard
Side effects • Something that happens as the result of a method call which is not the main purpose of the method is called a side effect • For example, a method intended to display information makes changes to an implicit or explicit parameter object • Generally it is undesirable to make a change to an explicit parameter object • Usually, if a change to an object is desired this is done by passing it as an implicit parameter, as in jack.multipleFlip()
Undesirable vs Impossible • It is impossible to change the value of a primitive type variable in a method by passing it as an explicit parameter to another method • However, it is often desirable to change the values in an array by passing it as an explicit parameter to another method • It is generally undesirable to change the data fields of an object by passing it as an explicit parameter to another method: usually better to pass it as an implicit parameter • However, when an object is immutable it is impossible to make changes to it by passing it as either an explicit or implicit parameter
Immutable objects • An object is immutable if it is impossible to make changes to it once it is constructed • The class is also called immutable • Strings are immutable String s1 = new String(“abc”); s1 = new String(“def”); • The string that was constructed to contain “abc” is not changed; it is lost • Requirements for an immutable class include • all data fields (instance variables) are private • there are no mutator methods, only accessor methods • But this is not enough! See p. 232 of text
Interned string values • String s1 = “abc”; • String s2 = “abc”; • System.out.println(s1 == s2); • Both s1 and s2 point to the same object constructed to contain “abc” • String s3 = new String(“abc”); • System.out.println(s1 == s3); • System.out.println(s1.equals(s3)); • String s4 = s3.intern(); // get interned string • System.out.println(s1 == s4);
String methods • You only need to know charAt • subString is also useful • There are lots of others
Passing strings to main • public static void main(String[] args) • up until now we have never accessed args, which is an array of String objects (any name can be used) • this can be used to pass information into main from the command line
Other information in Chapter 8 • A lot of technical detail that you do not need to learn, though you may find some of it useful • We’ll return to files later • The Scanner class: use this if you want to read from the console instead of using JOptionPane • Scanner builds on System.in, which is very complicated to use directly