aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt About Generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt About Generic" Watch "Doubt About Generic" New topic
Author

Doubt About Generic

Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
Dear All,

I have a small program like shown below :



Why there's no compiler error for line 46 ? Meanwhile b is a reference variable that refers to an
object Basket<Orange>, and it is casted to Basket<Apple> at line 46.


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
In line 45 b is declared as reference to raw type 'Basket', not as reference to 'Basket<Orange>'
During compilation compiler cares only about how 'b' is declared, it doesn't check what could be eventually assigned to 'b' somwhere earlier in the programm.
It is allowed to mix assigments of 'untyped' raw reference to 'typed' reference, even without cast - look at this:

It compiles fine, only on line 3 compiler gives warning (not error!) that it is unchecked conversion (raw 'untyped' b reference is assigned to 'typed' b0 variable). If you use explicity cast as in line 4, compiler doesn't give you any warning.
[ July 09, 2008: Message edited by: Ireneusz Kordal ]
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Faber ,



At line 45 , you will get compiler warning , because you are assigning
a type safe reference to unsafe reference.

At line 46 , you are trying to cast a non-safe reference to type safe reference. It is OK because you are safe.

If you try to do like this
Basket<Apple> bA = (Basket<Apple> bo ;// compile-time error
Because you are trying to do wrong cast .

After line 48 , your collection bo has different type of objects like orange and apple.

At line 50
Orange o = bO.getElement(); //line 50
If at that point, object is of type Orange then it is OK otherwise
program will blow.
[ July 09, 2008: Message edited by: Madhukar Ojha ]

SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
Hi Madhukar,

Actually there will be Runtime exception at line 50.



after line 45 b refers to bO, then after line 46, we cast b to bA. The scary thing happens at line 48 when we set the bA's element with an Apple, thus the bO's element is set too.

line 50 throws runtime exc becos we try to case an Apple to an Orange.

Correct me if i'm wrong.
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Faber ,

You should remember that there is only one object on heap which is referred by three different type of references , 2 of them are type safe and one is of raw type.
One more thing you should keep in mind when you mix up type safe and raw type it is ok at compile type. But at runtime autual object matters. Hence it can throw Exception.

after line 45 b refers to bO, then after line 46, we cast b to bA. The scary thing happens at line 48 when we set the bA's element with an Apple, thus the bO's element is set too.

line 50 throws runtime exc becos we try to case an Apple to an Orange.

Correct me if i'm wrong.[/QB]

Hi Faber ,
Basket b = bO; //line 45
Now you have lost type safety .

Basket<Apple> bA = (Basket<Apple> b ;// 46
Now again you cast raw type b to type safe bA.

bA.setElement(new Apple());// 48
Compiler allows it only because it cares about reference type that is bA.
Since bA is raw type that is why compiler lets you do it.

Try to do it with reference with bo

bo.setElement(new Apple());// error
It is compile-time error because knows it very well that bo is of type Basket<Orange> and you are trying to add wrong thing in type safe collection.

Orange o = bO.getElement();
Now since compiler thinks that element should be Orange because of reference type so it allows it.

But at runtime JVM finds that it is Apple object and you are trying to assign it to different type Orange , that is why it is upset and throws Exception.
 
 
subject: Doubt About Generic