Title: Lesson : Object Basics and Simple Data Objects
1Lesson Object Basics and Simple Data Objects
- 1. The Life Cycle of an Object
- 2. Characters and Strings
- 3. Numbers
- 4. Arrays
21. The Life Cycle of an Object
- How to create and use objects
- How the system cleans up the object
3Creating Objects
- Declaration
- Variable declarations that associate a name with
a type. - Instantiation
- Creates the new object with new operator.
- Initialization
- Constructor initializes the new object.
Point origin_one new Point(23, 94)
4Declaring a Variable to Refer to an Object
- To declare a variable to refer to an object, you
can use the name of a class. - Declarations do not create new objects, it just
declares a variable. - An empty reference is known as a null reference.
Point origin_one
5Instantiating an Object
- The new operator instantiates a class by
allocating memory for a new object. - The new operator requires a single, postfix
argument a call to a constructor. - The new operator returns a reference to the
object it created and assigned to a variable of
the appropriate type.
6Initializing an Object
- The constructor initializes the new object.
- A constructor has the same name as the class and
has no return type. - No-argument constructor
- The constructor doesn't take any arguments.
7Using Objects
- You may need information from it, want to change
its state, or have it perform some action. - Objects give you two ways to do these things
- 1. Manipulate or inspect its variables.
- 2. Call its methods.
8Referencing an Object's Variables
- The general form of a qualified name
- objectReference.variableName
- The variable's qualified name must be a reference
to an object. - Code that is outside the object's class must use
a qualified name. - int height new Rectangle().height
9A Word About Variable Access
- The direct manipulation of an object's variables
by other objects and classes is discouraged. - A class would provide methods through which other
objects can inspect or change variables. - Ensure that the values of the variables make
sense for objects of that type - The class can change the variables without
affecting its clients.
10Calling an Object's Methods
- Append the method name to an object reference,
with an intervening period (.). - objectReference.methodName(argumentList)
- objectReference.methodName()
- int areaOfRec new Rectangle(100, 50).area()
- Invoking a method on a particular object is the
same as sending a message to that object. - The methods declared public are accessible to any
other class.
11Cleaning Up Unused Objects
- The Garbage Collector
- Periodically frees the memory used by objects
that are no longer referenced. - To run the garbage collection explicitly by
calling the gc method in the System class. - Finalization
- To clean up through a call to the object's
finalize method - The finalize method is a member of the Object
class
122. Characters and Strings
- Character data can be stored and manipulated by
one of three classes in java.lang Character,
String, and StringBuffer.
13Characters
- Character Class
- A class whose instances can hold a single
character value. - Methods
- Character(char)
- compareTo(Character)
- equals(Object)
- toString()
- charValue()
- isUpperCase(char)
14String and StringBuffer
- Store and manipulate strings
- String character data consisting of more than
one character - The String class provides for strings whose value
will not change. - The StringBuffer class provides for strings that
will be modified
15Creating Strings and StringBuffers
- String
- String palindrome "Dot saw I was Tod"
- char helloArray 'h', 'e', 'l', 'l', 'o'
- helloString new String(helloArray)
- StringBuffer
- String palindrome "Dot saw I was Tod"
- int len palindrome.length()
- StringBuffer dest new StringBuffer(len)
16Getting the Length
- Accessor Methods
- Methods used to obtain information about an
object - length accessor method
- Returns the number of characters contained in the
string or the string buffer. - Capacity accessor method
- Returns the amount of space allocated for the
string buffer
17Getting Characters by Index
- charAt accessor method
- Get the character at a particular index within a
string or a string buffer. - substring accessor method
- Get part of a string or string buffer. Remember
that indices begin at 0.
18Modifying StringBuffers
- append method
- Add a character to the end of the destination
string. - dest.append(source.charAt(i))
- insert method
- Insert data into the middle of a StringBuffer.
- sb.insert(6, "Hot ")
- setCharAt method
- Replaces the character at a specific location
with the character specified.
19Converting Objects to Strings
- toString Method
- Convert an object to a String to pass it to a
method that accepts only String values. - StringBuffer dest new StringBuffer(len)
- for (i (len - 1) i gt 0 i--)
- dest.append(source.charAt(i))
-
- return dest.toString()
- valueOf Method
- Convert variables of different types to Strings.
- System.out.println(String.valueOf(Math.PI))
20Converting Strings to Numbers
- The String class does not provide any methods for
converting a String to a numerical type. - Four of the "type wrapper" classes (Integer,
Double, Float, and Long) provide a class method
named valueOf that converts a String to an object
of that type - String piStr "3.14159"
- Float pi Float.valueOf(piStr)
21Strings and the Java Compiler
- Literal Strings
- Use a literal string to initialize a String
- String s "Hola Mundo"
- String s new String("Hola Mundo")
- Use String methods directly from a literal string
- int len "Goodbye Cruel World".length()
- Concatenation and the Operator
- String cat "cat"
- System.out.println("con" cat "enation")
223. Numbers
- The Number class is the superclass for all number
classes in the Java platform. Its subclasses
include Float, Integer, and so on.
23Numbers
- Wrappers for other data types
- Boolean
- Character
- Void
- Beyond basic arithmetic
- Math
244. Arrays
- An array is a fixed-length structure that stores
multiple values of the same type. You can group
values of the same type within arrays.
25Arrays
- Array
- A structure that holds multiple values of the
same type. - An array element
- One of the values within an array and is accessed
by its position within the array.
26Creating and Using Arrays
- Declaring a Variable to Refer to an Array
- int anArray // declare an array of
integers - The declaration for an array variable does not
allocate any memory - Creating an Array
- anArray new int10 // create an array of
integers - Accessing an Array Element
- anArrayi i//assign values to the array
elements - Getting the Size of an Array
- arrayname.length
27Array of Objects
- Arrays can hold reference types as well as
primitive types
public class ArrayOfStringsDemo public
static void main(String args)
String anArray One", "Two", "Three"
for (int i 0 i lt anArray.length i)
System.out.println(anArrayi.toLowerCa
se())
28Arrays of Arrays
String cartoons
"Rubbles", "B", "B", "B" ,
"Jetsons", "G", J", "E", "J", "R", "A"
- Arrays can contain arrays.
int aMatrix new int4
for (int i 0 i lt aMatrix.length i)
aMatrixi new int5 //create sub-array
for (int j 0 j lt aMatrixi.length j)
aMatrixij i j
29Copying Arrays
- Use System's arraycopy method.
public static void arraycopy(Object source,
int srcIndex,
Object dest, int destIndex,
int length)
30Copying Arrays Example
public class ArrayCopyDemo public static
void main(String args) char
copyFrom 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' char
copyTo new char7 System.arraycopy(cop
yFrom, 2, copyTo, 0, 7)