File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes This code is right or wrong ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "This code is right or wrong ?" Watch "This code is right or wrong ?" New topic
Author

This code is right or wrong ?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
LinkedList<Float> fl = new LinkedList<Float>();
LinkedList<Number> nl = fl ;

Is this wrong ???

LinkedList<Integer> il = fl ;

what about this code ???

thanks a lot .
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
LinkedList<Float> fl = new LinkedList<Float>();
correct

LinkedList<Number> nl = fl ;
wrong

LinkedList<Integer> il = fl ;
wrong

Additional examples:
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Can you explain the examples ...

thanks a lot .
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Oct 11, 2004
Posts: 3830
Yes Joyce ...
But what about this :

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

thanks a lot .
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Oct 11, 2004
Posts: 3830
Because we can assign sub class object into super class reference .

Object o = myClassObject .

please reply .

thanks .
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 11, 2003
Posts: 1392
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: This code is right or wrong ?
 
Similar Threads
Base type vs parameter type
Number Conversion Problem
java.lang.ClassCastException:
Compilation error
Best implementation for Queue interface.