1 / 16

Object Casting and Vectors in Java Programming

Learn about casting objects in Java, converting data types, and utilizing Vectors for flexible data storage. Understand how to work with objects within inheritance hierarchies and use methods with Objects. Explore the Vector Class for dynamic array-like storage in Java.

joannb
Download Presentation

Object Casting and Vectors in Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword

  2. OO Concepts • Casting: • Casting can be used to convert one primitive data type to another. • For example: • double x = 3.45; • float y = (float)x; • The above example converts a double value to a float value and • assigns it to the float variable named y. • It is also possible to Cast Objects from one type to another.

  3. OO Concepts • Casting Objects: • The relationship between the Class Object and the Class • String in an inheritance hierarchy is as follows: Object String

  4. OO Concepts • Casting Objects: • Because the String Class and the Object Class are in the same • inheritance hierarchy a String is-a type of Object. • Because of this relationship, Java allows us to declare a String • as follows: • Object a = new String(“Hello”); • The object being referenced by ‘a’ is a String, but its type has • been downplayed.

  5. OO Concepts • Casting Objects: • The String has been downplayed and so we cannot • use the String in its full capacity. • The following example will fail: • Object a = new String(“Hello”); • int theLength = a.length(); • Length is a method specific to the String Class. It cannot • be used because the String has been downplayed to an Object • type.

  6. Casting Objects • Casting Objects: • If we wish to use the object reference ‘a’ to its full • capacity, we must cast the object back to its specific • type. • Object a = new String(“Hello”); • //Cast the a object reference a back to a String • String b = (String)a; • int theLength = b.length(); • It is important to understand that object casts can only be • be done within an inheritance hierarchy.

  7. Casting Objects • The inheritance hierarchy for String: • java.lang.Object • | • +--java.lang.String • The inheritance hierarchy for Integer: • java.lang.Object • | • +--java.lang.Number • | • +--java.lang.Integer • Because Integer and String are not in the same hierarchy we • cannot cast a String to an Integer or vice versa.

  8. OO Concepts • Casting Objects: • The Java language allows us to downplay objects to make the • language more flexible. • For example we can declare an Array as type Object and store • any kind of an object in the array: • Object[ ] a = new Object[2]; • a[0] = new String(“Hello”); • a[1] = new Integer(10);

  9. OO Concepts • Casting Objects: • We can create methods that accept arguments of type Object. • Methods that accept Objects can accept any type of Object • such as Strings or Integers. • String a = new String(“Hello”); • Integer b = new Integer(10); • Public void add(Object aValue) { • //Method body • }

  10. OO Concepts • Vectors: • The Vector Class is a commonly used Class from the Core • Java API. • A Vector is an indexed list of object, much like an array. • You use Vectors when you need greater flexibility • than arrays provide. • It can be found in the java.util package. • The main advantage is that a Vector can grow and shrink in size • as required, but an array has a fixed size.

  11. OO Concepts • Vectors: • Once that array is created, it has a set size, and additional • elements cannot be added. • You may think of a vector as a dynamic array that automatically • expands as more elements are added to it. • Vectors can only accommodate objects. Primitives • are not allowed. • If you wish to store individual numbers in a Vector you will • have to wrap them in their associated wrapper Class first.

  12. OO Concepts • Vectors: • All vectors are created with some initial capacity. • When space is needed to accommodate more elements, • the capacity is automatically increased. • Like arrays, elements in a vector have an associated index. • For these reasons vectors are commonly used in • Java programming.

  13. OO Concepts • Vectors: • Vector a = new Vector(); • A Vector object has been created • with an initial capacity of ten and • is being referenced by the variable a. • Add a String object to the Vector • String b = new String(“Hello”); • a.addElement(Object obj); Index: 0 “Hello”

  14. OO Concepts • Vectors: • String d = new String(“Good Day”); • a.addElement(d); • To Use an Object stored in a Vector • use the following syntax: • String e = (String)a.elementAt(0); • A cast is required to use the String in its full capacity. • Remember, all objects are downplayed when placed in a Vector. Index: 0 1 Hello Good Day

  15. OO Concepts • Vectors & Casting: • Casts are commonly used with vectors. • When retrieving a value from a vector, its type is only known • as the generic type Object • Because of this, you must use a cast to cast it back to the type • of the object that you inserted into the vector.

  16. Vectors • The following are some important methods we will be • using with the vector Class: • Method: Description • Vector() A constructor commonly used to • create a vector Object with an initial • capacity of 10 elements. • addElement(Object obj) Adds an object to the vector. • insertElementAt(Object Adds obj to the vector at index • obj, int index) • elementAt(int index) Returns the element at the specific index

More Related