• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt About Generic

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic