intro to java part ii. calling an objects methods use qualified names to call the objects methods....

21
Intro to Java Part II

Post on 19-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Intro to Java

Part II

Page 2: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Calling an Objects Methods

• Use qualified names to call the objects methods.

• To form – you append the method name to an object reference by using the dot operator ( . ).

• Also include in parenthesis any argument to the method.

• If none use ( ).

Page 3: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Exps:

objectReference.methodName(argumentList);

or objectReference.methodName();

Page 4: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

• Recall Rectangle class:– Two methods:

• area to compute the rectangle’s area• move to change the rectangle’s origin

Here's the CreateObjectDemo code that calls these two methods:

System.out.println("Area of rect_one: " + rect_one.area()); ... rect_two.move(40, 72);

As with instance variables objectReference must be a reference to an object.

Page 5: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

• Other ways:– You can use any expression that

returns an object reference.– Exp : new returns an object reference

so:• new Rectangle(100, 50).area()

This returns an object reference that refers to a Rectangle object.

Page 6: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

• And:– For for some methods, such as area that

return a value.– You can use the method call in expressions.– You assign the return value to a variable, use

it to make decisions, control loops.– Exp:int areaOfRectangle = new Rectangle(100,

50).area(); Note: Invoking a method on a particular object

–> as sending a message to that object.

Page 7: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Garbage Collection

• Some languages require the programmer to clean up

• Not Java – it allows you to create as many objects as your system or specs will allow.

• Java runtime environment – deletes objects when it determines they are no longer needed.

• Called garbage collection.

Page 8: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Garbage Cont’d

• An object is eligible for garbage collection when there are no more references to that object.

• When a variable goes out of scope.• Or you drop an object ref by

setting to null.• All ref’s to the object must be

dropped before it can go into garbage collection.

Page 9: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

The Collector

• Java runtime environment has a garbage collector that periodically frees memory used by objects that are no longer ref’d.

• Done automatically – although – you can manually run by calling the gc method in the system class.

• Objects can clean up after themselves by a call to the objects finalize method.

• Known as finalization

Page 10: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Characters & Strings

• Three classes:– Character : a class whose instances

can hold a single character value.– String: A class for working with

immutable (unchanging) data composed of multiple characters.

– StringBuffer: A class for storing and manipulating mutable data composed of multiple characters.

Page 11: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Characters

• An object of character type contains a single character value.

• Use – character instead of char when an object is required – for exp; – When passing a character value into a

method that changes the value.– Or when placing a character value

into a data structure – vector.

Page 12: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Character a = new Character('a'); Character a2 = new Character('a'); Character b = new Character('b');

Character(char)The Character class's only constructor,

which creates a Character object containing the value provided by the argument. Once a Character object has been created, the value it contains cannot be changed.

Page 13: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

compareTo(Character) An instance method that compares the

values held by two character objects: the object on which the method is called (a in the example) and the argument to the method (b in the example). This method returns an integer indicating whether the value in the current object is greater than, equal to, or less than the value held by the argument. A letter is greater than another letter if its numeric value is greater.

Page 14: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

equals(Object) An instance method that compares the

value held by the current object with the value held by another. This method returns true if the values held by both objects are equal.

toString()An instance method that converts the

object to a string. The resulting string is one character in length and contains the value held by the character object.

Page 15: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

charValue()An instance method that returns the

value held by the character object as a primitive char value.

isUpperCase(char)A class method that determines

whether a primitive char value is uppercase. This is one of many Character class methods that inspect or manipulate character data.

Page 16: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Why two Strings?

• String & StringBuffer• StringBuffer class provides for strings that

will be modified – Exp: you use string buffers when you know that

the value of the character data will change. (Reading text data from a file)

• String class provides for strings whose value will not change. – Exp: you write a method that req’s string data

and the method is not going to modify the string in any way, pass a String object into the method.

Page 17: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

//place code hereString palindrome = "Dot saw I was Tod"; int len = palindrome.length(); StringBuffer dest = new StringBuffer(len); for (int i = (len - 1); i >= 0; i--)

{ dest.append(palindrome.charAt(i)); } System.out.println(dest.toString( ));

Page 18: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Creating Strings & StringBuffers

• A string is often created from a string literal. – a series of characters enclosed in double quotes.

• Exp : “Gobbledygook” creates an string object – value is Gobbledygook.

• String Demo does the same with palindrome variable.

• Can also create String objects by using the new keyword and a constructor.

Page 19: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

• String classes provides several constructors that allow you to provide the initial value using different sources, array’s of char’s or bytes or a string buffer.

• Exp:char[ ] helloArray = { 'h', 'e', 'l', 'l', 'o' }; String helloString = new String(helloArray); System.out.println(helloString);

Page 20: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

• Must always use new for StringBuffer|String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); StringBuffer dest = new StringBuffer(len); |dest is a StringBuffer the constructor sets the

buffer capacity.Initial capacity equal to the length of the String

palindrome ensures only one memory allocation for dest.

By performing this initial size length you minimize the number of times memory must be allocated for it.

Making your code more efficient.

Page 21: Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference

Next Assignment

• G:/ ….collaboration/assignment2.txt

• Complete questions and code – copy to G:/ drive hand-in folder

• Due Date: 02/03/2003 BOC