• 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

This code is right or wrong ?

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedList<Float> fl = new LinkedList<Float>();
LinkedList<Number> nl = fl ;

Is this wrong ???

LinkedList<Integer> il = fl ;

what about this code ???

thanks a lot .
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedList<Float> fl = new LinkedList<Float>();
correct

LinkedList<Number> nl = fl ;
wrong

LinkedList<Integer> il = fl ;
wrong

Additional examples:
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain the examples ...

thanks a lot .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedList<Number> nl = fl; // ILLEGAL!
Let's assume the above statement is legal, see what happens to the following code:



As you can see, there's a loophole for runtime exception if the statement was legal, violating the real purpose of using generics: type-safety.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confuse in this code only ...

n1.add(1.2);

you are adding double in a list that accept only Number . OK

for (Float value : f1)
System.out.println(value);

you are retriving from other list , so how that previous double value will make any difference ( problem ) , how that two are related ( this list & that previous double value )

please reply ...

thanks a lot .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confuse in this code only ...

n1.add(1.2);

you are adding double in a list that accept only Number . OK


The signature for add in java.util.List is:


If the argument type for List is Number, i.e. List<Number>, it is legal to place any element which is a Number or its subclass. In this case, Double is a subclass of Number.


Output is:
1.1
1
1.2
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another example, see if you could understand it.



For generics topic, I suggest you read the generics tutorial by Gilad Bracha.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedList<Number> nl = fl; // ILLEGAL!

The question is still same , why this is illegal .

I got your example but how this is proving this statement ?

I mean to say it is OK that a list which accept Number , we are inserting Float ( because it is subclass of Number ) but my doubt is why we can't assign a list that accept Float in a list that accept Number .

I hope you got my doubt ...

please reply ...

thanks a lot .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understand what your confusion is all about now.



List<Number> n1 means the elements in list n1 must be at least a Number type. So when an element is retrieved from the list, you can be sure the following statement is valid.



But you can't do something like this:

The compiler will complain cos the list could contain other elements like Integer, Short, Long or Float which is a subclass of Number. A variable of Double type cannot reference an Integer object. But a variable of Number type can reference a Number or its subclass object.

So far do you agree with me?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Joyce ...
But what about this :

LinkedList<Number> nl = fl; // ILLEGAL!

thanks a lot .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's pretend you've agreed with me.

Now back to the earlier example I've given.



Assuming the following statement is legal, n1 is now pointing to f1.


In the last post, I've already explained that n1 can contain elements that is of type Number or its subclasses. So this statement is legal, right?


Now I want to retrieve the element using f1 instead of n1. Since f1 is declared as LinkedList<Float>, I expect the elements in LinkedList<Float> f1 to be at least a Float type. Meaning when I retrieve the elements from f1, I could do something like this:


But something goes wrong here, the element retrieved from f1 is a Double object added by n1. How can a value of type Float point to a Double object?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never thought this tiny thing , that two reference are poinging to single object so when we modify object with one reference it is definately has changed for second reference ...

But still has one doubt ....

why this is ILLEGAL :

LinkedList<Number> n1 = f1; // WHY ILLEGAL ?

In my view this should be ILLEGAL after the above statement :

n1.add(1.2); // THIS SHOULD BE ILLEGAL .

because now n1 is reffering to a list that can only accept Float ...

please reply ...

thanks a lot .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if n1 can only contain Number object as you said, it means f1 can only contain Float object, right? So how can n1 point to f1? Isn't a contradictory?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because we can assign sub class object into super class reference .

Object o = myClassObject .

please reply .

thanks .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
n1.add(1.2); // THIS SHOULD BE ILLEGAL .

If this statement is illegal, then what types of elements can n1 add into the list?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because now n1 is pointing to a Float list . so we can add any parameter that is compatable to Float .

n1.add(1.4f); // correct

n1.add(5); // int -> float -> Float

I know that that statement is ILLEGAL but I am not getting the logic . But I am liking this interview ...

thanks a lot .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, type-safety for generics is performed during compile-time not runtime. Once a code is compiled, all generic type information like < > is removed and appropriate casting will be applied. This is called erasure and more information can be found in generics tutorial, section 6.2 - pg 12.

For example,


After compiling and then decompiling the above code, it becomes something like this:



because now n1 is pointing to a Float list . so we can add any parameter that is compatable to Float .

So based on your theory, what happens if the getValue() returned a Double object, what should the JVM do? Throws exception?
reply
    Bookmark Topic Watch Topic
  • New Topic