(syllabus and calendar)

Session 3 - part 2  Lecture Notes for First Course in Java

Review

Introducing Classes

constructors

July 2006 zip of examples with comments

zip of examples with 2005 comments from class

zip of Midterm from last semester with answers

Quiz

Review

Review of while loop equivalent to for loop

Looping with char to process the letters of the alphabet


Before we dive into how object-oriented programming works, let's see how much more functionality we could add to a looping program if we were to take advantage of object-oriented programming. One student wrote this program to take advantage of a method provided in the Java API's for String objects.

The output is:

The program takes advantage of the length property available on all arrays. Oddly enough, this fundamental and useful property is NOT documented in the APIs. Sun does explain it in the Tutorial:   http://java.sun.com/docs/books/tutorial/java/data/arraybasics.html

Here's the relevant API method for getting to a location on a String: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#charAt(int)


Introducing Classes, Objects, and Methods

Different instances of the same class can have different data. To create a second instance of the same class, you must re-use the new operator.

Object-Oriented Glossary

class

The class defines the behavior and attributes of the object. An object is an instance of a class. A class is a template that defines the form of an object.

When I visit a retail store, each employee might have a unique name tag and be a unique "object", but I expect them to follow the role of RetailEmployee, that is, to be instances of the class RetailEmployee.

In the real world, we deal with objects. Who has eaten Food today? No one. But we all ate some food objects. Each McDonald's french fry is an object of type FrenchFry that fits the template and has attributes of length, width, color, and temperature.

A class is a logical abstraction. Only when an object is created is memory physically allocated.

A class is a data type. Even the primitive data types, such as int, can be represented by a corresponding class, such as Integer. When you create classes for your applications, you create your own custom data types. (Much of the value of object-oriented programming is the ability to create the custom data types or classes that serve your business needs or fit the problem domain.)

class members

The members of a class are its

main()

The main() method is the point of entry for execution. Your application must have a main method in a file named for the class that contains the main method. Your application can have other classes, and they do not have a main method. (An applet does not have a main method because the Java virtual machine is already available for execution within a web browser.)

dot operator

The dot operator enables you to access a class member through an instance of the class. I get the food value of Food by accessing myFrenchFry.calories or myFrenchFry.flavor. Use this syntax:

object.member


object creation

The new operator dynamically allocates memory for the newly-created object. Objects are created in memory at run-time not compile-time.


reference variable assignment

The primitive types have copy by value by object types have copy by reference (additional assignment results in two references pointing to the same place in memory)

int myInt = 14;
int yourInt = myInt; // independent copy

car myCar = new Car();
car yourCar = myCar; // no new object created

myCar and yourCar are two reference variables, two handles on the same object, like two horns on the same bull.


methods, return values, parameters

Methods are often used to get and set the value of properties.

It is logical that the range() method be encapsulated within the Vehicle class rather than located in the main of the driver class. The class with main should drive the application and let each of the other classes take care of their own logical business. The range() method has a return type of void because it does not return any object or primitive data type. Instead, it makes a call to a method: println().

We can rewrite this method to have a return type of integer.


return keyword

The flow of execution returns to the caller of a method when all the lines of the method have been executed or the return keyword is encountered.


adding parameters to a method

A method can be defined to take zero, one, or many formal parameters (line 3).
At runtime, the arguments passed to the method (lines 13, 15, 17) must match the formal parameter definition.

Here, a method is defined with two parameters.

Let's apply the concept of the parameterized method to the Vehicle class.


Review and summary example

 


constructors

The constructor is for initialization at object-creation time. It looks like a method but it has no return type and it must have the same name and case as the class for which it is used to initialized objects. The default constructor is available automatically, but if you define a constructor, the default constructor is no longer available.


parameterized constructors

Although a constructor is NOT a method, constructors and methods are two kinds of class members that can be defined with formal parameters and that can take arguments at runtime.

Let's apply the concept of a parameterized constructor to the Vehicle class.


new operator and general discussion

The new operator allocates a new area of memory for the newly created object to use. The constructor does initialization for the newly created object. We do not need the constructor or the new operator for primitive operations. It is faster to work with primitive types. They reside in a last-in/first-out (LIFO) data structure called the stack. Objects live in a different type of memory area called the heap. The overhead for creating and managing objects might be considered similar to the overhead of creating a spreadsheet or table to track all the members of the class. We might consider each object to be a row in the spreadsheet or table.


garbage collection and finalize

Demonstrate the periodic process of freeing memory. A use case for the finalize() method is to close a file or database connection so that object destruction is clean.


this keyword

The keyword this is used implicitly by Java to refer to the current object.

Here, we use this explicitly to distinguish between a class property and a local variable


Quiz and Homework

Quiz for Lecture 4

 

1.      A string has a length() method, but an array has the ________ property.

 

2.    What is a constructor?

 

3.    How can you tell a constructor when you see it:

a.     _____

b.     _____

c.     _____

 

4.    What does the keyword “return” do?

 

5.    What is a parameter?

 

6.    Primitives have copy by ________.

 

7.    Objects have copy by _______.

 

8.    What does “this” mean?