Object Basics and Simple Data Objects - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Object Basics and Simple Data Objects

Description:

Lesson 3. Object Basics and Simple Data Objects. The Life Cycle ... char aChar = anotherPalindrome.charAt(9); String roar = anotherPalindrome.substring(11, 15) ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 18
Provided by: ayse7
Category:

less

Transcript and Presenter's Notes

Title: Object Basics and Simple Data Objects


1
Lesson 3
  • Object Basics and Simple Data Objects
  • The Life Cycle of an Object
  • Characters and Strings
  • Arrays

2
The Life Cycle of an Object
  • Creating Objects
  • CreateObjectDemo.java
  • Point.java
  • Rectangle.java
  • Point origin_one new Point(23, 94)
  • Rectangle rect_one new Rectangle(origin_one,
    100, 200)
  • Rectangle rect_two new Rectangle(50, 100)

3
The Life Cycle of an Object
  • Declaration
  • Point origin_one
  • Instantiation and Initialization
  • Point origin_one new Point(23, 94)

4
The Life Cycle of an Object
  • Instantiation and Initialization
  • Rectangle rect_one new Rectangle(origin_one,
    100, 200)

Question How can you show rect_one?
5
Using Objects
  • Manipulate or inspect its variables.
  • Call its methods.
  • objectReference.variableName
  • System.out.println("Width of rect_one "
    rect_one.width)
  • objectReference.methodName(argumentList)
  • System.out.println("Area of rect_one "
    rect_one.area())
  • rect_two.move(40, 72)

6
Cleaning Up Unused Objects
  • Garbage Collection
  • An object is eligible for garbage collection when
    there are no more references to that object.
  • References that are held in a variable are
    usually dropped when the variable goes out of
    scope. Or, you can explicitly drop an object
    reference by setting the variable to the special
    value null
  • Garbage Collector
  • Automatic
  • But can be run explicitly by calling the gc
    method in the System class

7
Characters and Strings
  • Character class
  • String class
  • StringBuffer class.

8
Character Class
  • An object of Character type contains a single
    character value.
  • You use a Character object instead of a primitive
    char variable when an object is required-for
    example
  • when passing a character value into a method that
    changes the value or when placing a character
    value into a data structure, such as a vector,
    that requires objects.
  • Example Code

9
Two String Classes
  • The Java platform provides two classes, String
    and StringBuffer, that store and manipulate
    strings-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 you use string buffers when you
    know that the value of the character data will
    change. You typically use string buffers for
    constructing character data dynamically for
    example, when reading text data from a file.
  • Because strings are constants, they are more
    efficient to use than are string buffers and can
    be shared. So it's important to use strings when
    you can.
  • Example Code

10
Creating Strings and StringBuffers
  • Different ways to create a String
  • String palindrome "Dot saw I was Tod"
  • char helloArray 'h', 'e', 'l', 'l', 'o'
  • String helloString new String(helloArray)
  • You must always use new to create a string buffer
  • StringBuffer dest new StringBuffer(len)

11
Accessor Methods
  • Getting the Length of a String or a String Buffer
  • String palindrome "Dot saw I was Tod"
  • int len palindrome.length()
  • A string buffer's length is the number of
    characters it contains a string buffer's
    capacity is the number of character spaces that
    have been allocated.
  • The String class doesn't have a capacity method,
    because a string cannot change.

12
Accessor Methods
  • Getting Characters from a String or a String
    Buffer
  • String anotherPalindrome "Niagara. O roar
    again!"
  • char aChar anotherPalindrome.charAt(9)
  • String roar anotherPalindrome.substring(11,
    15)

13
Java Strings Are First-Class Objects
  • By convention, C and C strings are
    null-terminated array of characters there is no
    real entity in C and C that is a string. Java
    strings are first-class objects.
  • Example code which illustrates the difference
    between strings in Java and C/C

14
Arrays
  • An array is a structure that holds multiple
    values of the same type.
  • The length of an array is established when the
    array is created (at runtime).
  • After creation, an array is a fixed-length
    structure.

15
Creating and Using Arrays
  • Declaring a Variable to Refer to an Array
  • Creating an Array
  • Accessing an Array Element
  • Getting the Size of an Array
  • Example Code
  • Array Initializers
  • boolean answers true, false, true, true,
    false

16
Arrays of Objects Arrays of Arrays
  • Arrays can hold reference types as well as
    primitive types.
  • Example Code
  • Arrays of Arrays
  • Example Code

17
Exercise
  • Write a Java program which will generate 10
    random strings, sort them and print the output.
Write a Comment
User Comments (0)
About PowerShow.com