Docsity

Java Programming Language Review: Objects, Variables, Methods, and Garbage Collection - Pr, Study Guides, Projects, Research of Programming Languages

Toggle sidebar
bg1
1
CMSC433, Spring 2004
Programming Language Technology and
Paradigms
Java Review
Jeff Foster
Feburary 3, 2004
33
Administrivia
•Reading: Liskov, ch 4, optional Eckel, ch 8, 9
•Project 1 posted
–Part 2 was revised Friday to make it better, easier
–A few subsequent small clarifications since then
–Important: Must change WebServer to use -Dport=...
•Class accounts e-mailed out
–Contact me if you didn’t get one
34
Demo
•Running basic web server
–Finding hostname of machine
–Connecting with web browser
•Telnet to a web server
35
Objects and Variables
•Variables of a primitive type contain values
–e.g., byte, char, int, ...
–int i = 6;
–Uninitialized values contain 0
–Assignment copies values
•Variables of other types contain references to the heap
–int[] a = new int[3];
–Objects are allocated with new
–Uninitialized object references are null
–Assignment copies references, not the objects themselves
36
Example
int i = 6;
int j; // uninitialized
int [] a = {1, 3, 5, 7, 9};
int [] b = new int[3];
String s = “abcdef”;
String t = null;
37
Example: Assignments
j = i;
b = a;
t = s;
pf3
pf4
pf5

Partial preview of the text

Download Java Programming Language Review: Objects, Variables, Methods, and Garbage Collection - Pr and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

CMSC433, Spring 2004

Programming Language Technology and

Paradigms

Java Review

Jeff Foster

Feburary 3, 2004

33

Administrivia

• Reading: Liskov, ch 4, optional Eckel, ch 8, 9

• Project 1 posted

  • Part 2 was revised Friday to make it better, easier
  • A few subsequent small clarifications since then
  • Important: Must change WebServer to use -Dport=...

• Class accounts e-mailed out

  • Contact me if you didn’t get one 34

Demo

• Running basic web server

  • Finding hostname of machine
  • Connecting with web browser

• Telnet to a web server

35

Objects and Variables

• Variables of a primitive type contain values

  • e.g., byte, char, int, ...
  • int i = 6;
  • Uninitialized values contain 0
  • Assignment copies values

• Variables of other types contain references to the heap

  • int[] a = new int[3];
  • Objects are allocated with new
  • Uninitialized object references are null
  • Assignment copies references, not the objects themselves 36

Example

int i = 6; int j; // uninitialized int [] a = {1, 3, 5, 7, 9}; int [] b = new int[3]; String s = “abcdef”; String t = null; 37

Example: Assignments

j = i; b = a; t = s;

38

Garbage Collection

• What happens to array [0, 0, 0] in previous

example?

  • It is no longer accessible
  • When Java performs garbage collection (GC) it will automatically reclaim the memory it uses

• Notice: No free() or delete in Java

  • Makes it much easier to write correct programs
  • Most of the time, very efficient 39

Mutability

• An object is mutable if its state can change

  • Example: Arrays are mutable

• An object is immutable if its state never changes

  • Once its been initialized
  • Example: Strings in Java are not mutable
    • There are no methods to change the state of a string 40

Example: Mutability

a[1] = 0; // also changes b[1]

• Moral: Always be

careful when you have

aliasing

  • Multiple references to the same object 41

Method Invocation

• Syntax

o.m(arg1, arg2, ..., argn);

  • Run the m method of object o with arguments arg1...argn

• Two ways to reuse method names:

  • Methods can be overridden
  • Methods can be overloaded 42

Overriding

• Define a method also defined by a superclass

class Parent { int cost; void add(int x) { cost += x; } } class Child extends Parent { void add(int x) { if (x > 0) cost += x; } } 43

Overriding (cont’d)

• Method with same name and argument types in

child class overrides method in parent class

• Arguments and result types must be identical

  • otherwise you are overloading the method

• Must raise the same or fewer exceptions

• Can override/hide instance variables

  • both variables will exist, but don’t do it

50

Overloading Example

class Parent { int cost; void add(int x) { }^ cost += x; void add(Object s) throws NumberFormatException { cost += Integer.parseInt((String)s); } } class Child extends Parent { void add(String s) throws NumberFormatException { if (x > 0) cost += Integer.parseInt(s); } } Child c = new Child(); c.add((Object)“-1”); System.out.println(c.cost); Prints - 51

Static Fields and Methods

• static – stored “with the class”

  • Static fields allocated once, no matter how many objects created
  • Static methods are not specific to any class instance, so cannot refer to this or super

• Can reference class variables and methods through

either class name or an object ref

  • Clearer to reference via the class name 52

Static Field Example

int foo int foo int foo Foo int bar; Public class Foo { int foo; static int bar; } Class definition Class implementation Objects of class Foo 53

Some Static Fields and Methods

• public static void main(String args[]) { … }

• public class Math {

public final static PI = 3.14159…;

• public class System {

public static PrintStream out = …;

54 Actual type B Declared type A

Static Method Dispatch

• Let B be a subclass of A, and suppose we have

A a = new B();

• Then

  • Class (static) methods invoked on a will get the methods for the declared type A - Invoking class methods via objects strongly discouraged - Instead, invoke through class - A.m() instead of a.m() 55

Static Method Dispatch Example

public class A { static String g() { return “This is A! “; } } public class B extends A { static String g() { return “This is B! “; } public static void main(String args[]) { A a = new B(); B b = new B(); System.out.println(a.g() + b.g()); } }

Prints This is A! This is B!

56

Better Use of Static Methods

public class A { static String g() { return “This is A! “; } } public class B extends A { static String g() { return “This is B! “; } public static void main(String args[]) { System.out.println(A.g() + B.g()); } }

Prints This is A! This is B!

57

Other Field Modifiers

• final – can’t be changed

  • Must be initialized in declaration or in constructor

• transient, volatile

  • Will cover later

• public, private, protected, package (default)

  • Respectively, visible everywhere, only within this class, within same package or subclass, within same package 58

Other Method Modifiers

• final – this method cannot be overridden

  • Useful for security
  • Allows compiler to inline method

• abstract – no implementation provided

  • Class must be abstract

• public – visible outside this package

• native, synchronized

  • Will cover later 59

Poor man’s polymorphism

• Every object is a subtype of Object

• Thus, a data structure Set that implements sets of

Objects

  • can also hold Strings
  • or images
  • or … anything!

• The trick is getting them back out:

  • When given an Object, you have to downcast it 60

Downcasting

• (Bar) foo

  • Run-time exception if object reference by foo is not a subtype of Bar
  • Compile-time error if Bar is not a subtype of foo (i.e., it always throws an exception)
  • No effect at run-time; just treats the result as if it were of type Bar

• o instanceof Foo returns true iff o is a subtype of

Foo

61

Example

class DumbSet { public void insert(Object o) {..} public bool member(Object o) {..} public Object any() {..} } class MyProgram { public static void main(String[] args) { DumbSet set = new DumbSet(); String s1 = “foo”; String s2 = “bar”; set.insert(s1); set.insert(s2); System.out.println(s1+”in set?”+set.member(s1)); String s = (String)set.any(); // downcast System.out.println(“got “+s); } }