a h
Posts: 1
Nickname: axih
Registered: Feb, 2005
|
|
Re: Java
|
Posted: Feb 18, 2005 4:02 PM
|
|
> I am a c++ developer and i am working on java lately. I > faced a problem described below. > > I filled a LinkedList called "customers" with objects of > class "customer". Customer objects have attributes x, y, > ID. Then i want to get an object of "customers" and i > write: > > Object cc1=Customers.getFirst();
You probably want to do this instead:
customer cc1 = (customer)Customers.getFirst();
The situation would actually be no different with C++, so you must excuse the lengthy explanation.
Java, like C++, is a statically-typed language. In a dynamically-typed language like Objective C, you would be able to send any object any message (and if the message can't be handled by the object, a runtime exception is raised). In Java, you can attempt to send any message to any object through java.reflection, but there is a compile-time type-checking phase that prevents you from calling method "foo" on an Object o through the syntax "o.foo()" (because Object does not implement any method named "foo()").
The conclusion of the above paragraph is that the Object class does not have attributes x, y and ID; only the customer class has those attributes, so you cannot query any Object for its y value. For example, this would not work:
Object o = new HashTable(); System.out.println("o.x = " + o.x);
And this does not work because Objects do not have attributes "x". HashTables also don't have attributes "x" and the variable "o" could be a HashTable, so the Java compiler prevents the second line from compiling to prevent the mistake of querying the "x" attribute from an object that does not have an "x" attribute.
Similarly, this will not work:
Object cc1 = // some code that returns an Object System.out.println("cc1.x = " + cc1.x);
As you can see, it does not make a difference where the object comes from; all that the Java compiler knows about cc1 is that it's an Object and Objects don't have "x" attributes. So, you need to do this:
customer cc1 = // some code that returns a customer System.out.println("cc1.x = " + cc1.x);
And this will work. However, now you need some code that returns a customer object. This will not work:
customer cc1 = Customers.getFirst();
And this does not work because LinkedList.getFirst returns an Object, not a customer. The Java compiler cannot know what specific subtype of Object will be returned by getFirst but it knows getFirst will return an Object or a subtype of Object. customers are objects, but not all Objects are customers, so the Java compiler does not allow this code to compile (it's being conservative since this might not be a error, but it could be an error). (Note however that all customers are Objects so if you wanted to create an Object from a customer you would not need to cast.) So, you must do this:
customer cc1 = (customer)Customers.getFirst();
The cast is a gaurantee by the programmer to the Java compiler that "yes, the object returned by getFirst will be a customer in this particular instance." If the gaurantee proves wrong, the Java runtime will throw a runtime exception, but the Java compiler performs no further checking and allows the code to compile.
|
|