1 / 18

Lecture 9: More on objects, classes, strings

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

Download Presentation

Lecture 9: More on objects, classes, strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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!

  2. HW3

  3. HW4 • Builds on our Date class • dayOfWeek method

  4. 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

  5. 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

  6. 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

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

  8. 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

  9. 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

  10. 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));

  11. 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

  12. 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()

  13. 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

  14. 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

  15. 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);

  16. String methods • You only need to know charAt • subString is also useful • There are lots of others

  17. 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

  18. 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

More Related