This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Generics

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Please tell why i am not getting runtime error for this

class A
{
public static void main(String... args)
{
List<Integer> l=new ArrayList<Integer>();
Collection<?>l1=new ArrayList<String>();//line 1
l.add(1);
l.add(2);
l1=l;//line 2
Integer s=l.get(0);
System.out.println("The values are : "+s+" "+l1);
}
}

At line i have created a reference of collection l1 which is able to refer to any type and i have created an arraylist of String

And at line 2 i am assigning arraylist of Integer to l1.
At run time what is going on?
How the JVM is doing all these things.

i know Generics are used for compile time.
And i won't check at run.

But i am not able understand what is going on?

please tell me
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


line 1 is perfectly allright basically you are saying

Collection<? extends Object> l1 = new ArrayList<String>();

1. ArrayList is a class which implements Collection so Arraylist object can be assigned to Collection referance

2. <?> means any type OR you can say any Object. so String is an Object. so no problem to compiler and no problem to JVM.




in line 2. you doing basically doing.

Collections<?> l1 = new ArrayList<Integer>(); // NOTE. not creating new arraylist

because l = new ArrayList<Integer>();

so it also meets the criteria we discussed above. so no problem to JVM and Compiler.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anil,

Little changes made to your code:


package generics;

import java.util.*;

public class DemoArrayList2 {

public static void main(String... args) {

ArrayList<Integer> alist = new ArrayList<Integer>();
alist.add(1);
alist.add(2);

Collection<?> blist = new ArrayList<String>();

blist = alist;

//blist.get(0);
//Fails, Collection doesn't have get() method

//You can't add anything but null to the blist because of "?" the wildcard
//that simple means read only
System.out.println(blist);

}
}



Referencing a collection object is different from adding something to the
collection.



Thanks and Regards,
cmbhatt
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Shape{}
class Circle extends Shape{}
class Rectangle extends Shape{}

class generics4
{

ArrayList<Circle> a = new ArrayList<Circle>();
a.add(new Circle());
ArrayList b = a;
ArrayList<Rectangle> c = (ArrayList<Rectangle> b;
c.add(new Rectangle());

for(Object o : b)
System.out.ptirnln(o);
}


This is a example form whizlab...

I compile this program with javac -Xclint command line

it shows 1 warning,of unchecked cast... on following line
ArrayList<Rectangle> c = (ArrayList<Rectangle> b;

but, i don't understand why this give warning but not a ERROR even if we cast totally different type...

Instead, why it does not show an error of undefined casting at compile time afterall,Generics checked at compile time, But compiler shows a warning...


if i consider,This is happen becouse of non generic list 'b' but b's content are of type circle....

So,Why it allows to cast ???

Please Reply, if know something...

Thanks.

Rahul. . .
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b is just a ArrayList reference variable pointing to a. And hence when you cast it to an arrayList of type Rectangle and assign it to c it works.But b's contents arent of type Circle....its just a variable referencing an ArrayList of type Circle.So no error.

But compiler gives warnings when it understands that non-type-safe-code can endanger your type-safe-code.Hence it shows a warning for the same assignment.
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandip,

I am not getting your answer.


Though b is just ArrayList reference variable, it's content are location of Object a (i.e new ArrayList<Circle>())
so when b is assign to the reference variable c then its content that is Object Location of a is assigned to c ....

So it indirectly refers to ArrayList a. AND ALSO CAST it...


Am, I Correct ???
[ December 05, 2007: Message edited by: Rahul Nair ]
 
Sandip Sarkar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

yah you are correct.
Look at the following piece of code in java 1.4-----





This code compiles fine but with warnings.And they are because of the unchecked add() method we are using.

Thanks,
Sandip
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandip,

I got Answer...

It gives error when i try to cast an
ArrayList<Circle> a to ArrayList<Ractangle>
but not for an ArrayList refernce b.

i.e. the compiler do not understand what is the content of refrence variable b...at compile time...

That why, it allows it to cast...


Thanks Sandip ----very Great Job.

Thnaks also for reply...


Rahul
[ December 07, 2007: Message edited by: Rahul Nair ]
 
Nothing? Or something? Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!