Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Intro to Java Part II

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

3 Exps: objectReference.methodName(argumentList); or objectReference.methodName();

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

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

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

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

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

9 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

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

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

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

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

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

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

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

17 //place code here String 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( ));

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

19 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);

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

21 Next Assignment G:/ ….collaboration/assignment2.txt Complete questions and code – copy to G:/ drive hand-in folder Due Date: 02/03/2003 BOC


Download ppt "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."

Similar presentations


Ads by Google