Lecture02 - CS162: Introduction to Computer Science II Java...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS162: Introduction to Computer Science II Java Fundamentals II
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Primitive types Last time, we reviewed primitive types: byte, short, int, long, float, double, char, boolean Today, we’ll review reference or class types
Background image of page 2
3 Outline 1. Reference Types 2. Wrapper Classes 3. Java Language Coding Guidelines
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Reference types All types that are not primitive are reference or class types Example: String greeting = "Howdy"; greeting is a reference variable, i.e., a variable that contain a reference to (address of) the memory location of the data.
Background image of page 4
5 Objects An object is a program entity that contains data performs certain actions The actions are called methods The actions of various objects interact to form a solution for a given problem
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Classes A class defines the characteristics for all objects of its type A class gives a general description of what an object of the type is (instance data) what an object of the type can do (methods)
Background image of page 6
7 Objects / Classes An object is an instance of a class. A program may have many instances (objects) of one class. All instances of the same class have The same kinds of data The same methods
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 An Example of a Class etc.
Background image of page 8
9 Three instances of the Automobile class
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Instantiation Invoke a constructor Example: Automobile bobsCar = new Automobile(“Sedan”, 2000, 0.9, 55, 21405); Keyword new invokes a constructor for the Automobile class. Note: this example uses an “overloaded” constructor. The “default” constructor does not accept any parameters.
Background image of page 10
11 Object variables An object variable is used to store the address of (reference to) an object When a class is instantiated, the system creates an object variable, and stores the address of the new object therein. E.G., bobsCar (previous slide) is an object variable.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Instantiation bobsCar “Sedan” 2000 0.9 55 21405 A memory cell that contains the memory address of the data for bobsCar
Background image of page 12
Aliases It is possible for two variables to reference the same object. They are aliases Example: Automobile myCar = bobsCar; bobsCar “Sedan” 2000 myCar 0.9 55 21405 If an object has two (or more) aliases,  the object may be referenced and/or  modified through any of those aliases.
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Invoking methods in a program Instead of calling a function to “do something to” an object (imperative programming), tell the object to perform one of its actions (object- based programming ). Examples:
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/28/2009 for the course CS 161 taught by Professor Wallace during the Spring '08 term at Oregon State.

Page1 / 47

Lecture02 - CS162: Introduction to Computer Science II Java...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online