• 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

Question on Generics

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<String> s= new ArrayList();

Is this a valid statement? Will we be able to add any object to s since the type is not specified during instantiation?
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


This is a valid statement but you will only be able to add String objects.

s.add("s"); //Will work
s.add(1); //Won't work

Object myObject = new Object();
s.add(myObject); //Won't work

Try this out by coding up. If you compile your code you will get some messages saying there are unchecked or unsafe operations.

HTH
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well sandhya let me try to explain this up to my knowledge-

You can do this

List<String> list1 = new ArrayList();

but it will generate a compile time warning. This is because now list1 will represent a list of Strings.

Lets take an example



the above case is the same as List<String> list = new ArrayList(); as you are assigning an untyped list to a typed list. If you take the example 1 and add this statement to the end

String s = stringList.get(0);

This will generate a ClassCastException. Now you will say that why did this happen. We didn't use any cast. But actually List<String> and List means the same thing after compilation(because of type erasure). So the compiler will add a cast at the statement like this

String s = (String) stringList.get(0);

But at index 0, 1 is stored which is an Integer so a ClassCastException will be thrown..

So basically the compiler will warn you that no matter the code may look like it has no type casts, but I will insert them whenever needed and this might result in exceptions at run-time because you are using a typed ArrayList through an untyped List reference....
 
Sandhya Bhaskara
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit,

In your example you were doing a get on stringList .What if you try to add an object to stringList? Even though the reference(stringList) is typed, the actual list itself is not typed.

In short my question is what is the difference between the below two statements

1. List<String> stringList = new ArrayList();
only reference variable is typed

2. List<String> stringList = new ArrayList<String>();
Both the variable and the object are typed


If the above two statements are not the same, how does add works in the first case( I know in the second case compiler makes sure you can add only string objects)

Thank you
Sandhya
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sandhya,
Even I have doubt.


1. List<String> stringList = new ArrayList();
only reference variable is typed

2. List<String> stringList = new ArrayList<String>();
Both the variable and the object are typed

The only difference I am seeing is..
the statement
List<String> stringList = new ArrayList();
is giving warning message...

Is there any other difference??
Thanks,
Suresh.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandhya Bhaskara:
Ankit,

In your example you were doing a get on stringList .What if you try to add an object to stringList? Even though the reference(stringList) is typed, the actual list itself is not typed.

In short my question is what is the difference between the below two statements

1. List<String> stringList = new ArrayList();
only reference variable is typed

2. List<String> stringList = new ArrayList<String>();
Both the variable and the object are typed


If the above two statements are not the same, how does add works in the first case( I know in the second case compiler makes sure you can add only string objects)

Thank you
Sandhya



Hi Sandhya,

The only reason why add in 1 works is because Sun wanted to provide a compatibility to all the code prior to java 5.

The compiler generates warnings for you. It wont stop you from adding anything. It is upto the programmer to make sure what you add is of the supported type.
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<String> stringList = new ArrayList();

Above statement is valid because java has to support the non generic code.It is same as passing non-generic collection to a method which takes type safe collection as an argument.

Is there any other difference??



no difference is there except in first statement you are mixing generics and non-generics code and in second it is purely generics code.
 
suresh pilakal babu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramesh/Chander,

Thank you for the explanation.

You mean both

List<String> stringList = new ArrayList(); and
ListstringList = new ArrayList();

are same ?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no. because List stringList = new ArrayList() is not typesafe.You can add any type of object to stringList
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,
For the following code, I get the error "Cannot find symbol" for add(int) method.

Here, my reference is type-safe.

But, if I change to the following, there is a successful compilation but with warnings.

Here, my object is type-safe and I am able to add and retrieve both the indices' values.


Due to type erasure, both these sets of code will finally be the same, right? Then, why is this error occuring?

I am using Java6 compiler.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference is that the first add uses the declared reference variable stringList which is List<String>. That says that the compiler will only honor adds of Strings.

The second is not a generic specification so any add will work.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, can I say that only the reference type (and not the type specified in the object) determines whether a declaration is type-safe or not?
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic