• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

String object inside a Type Safe Integer array list

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

Can we have a String object inside a Type safe ArrayList<Integer>. Here is how:

ArrayList <Integer> intList = new ArrayList<Integer>();
ArrayList mixList = new ArrayList();

mixList.add(1);
mixList.add(2);
mixList.add("string");

intList = mixList;

System.out.println(" Int List :"+intList);


Result :
Int List :[1, 2, string]


Actually whenever you try to apply the enhanced for loop on intList, there will be a ClassCaseException as it tries to cast a string object into integer. Is it a valid Behaviour ?

Thanks
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Whenever we are the using generics, it is such that any kind of valid/invalid insertion are prompted as a warning in compile time...."GENERICS ALLOWS NON_TYPE SAFE CODE AT RUNTIME",

And as this is a compile time "WARNING" it will definitely run.
 
Gopika Patel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,
Whenever you are using the for each loop it wont allow , it will compile time error of incompatibljavascript: x()
rollinge types .
 
obaid salikeen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks cherish. Can you please elaborate the statement a bit more :
�GENERICS ALLOWS NON_TYPE SAFE CODE AT RUNTIME�
Thanks
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy cowboys!


Generics care only about compile time checking for type safety. Behind the scenes, all generic instances will become non-generic due to type erasure.
All these nice generic collections will store just plain Objects (big O).
As a compile time check, you will get a warning (at compile time) when you assign a List of Integer to a non-generic ArrayList.
But a compile with a warning is still a successful compilation, so the code runs.

The only reason why the compiler only gives a warning (you may demand it should refuse to compile your line intList = mixList; at all) is that pre-generic code still has to work together with newer generic code.

At byte code level
List<Integer> li = new ArrayList<Integer>(); and
List lo = new ArrayList();
are just the same.
Therefore there can be no runtime check if the lists are really contain Integers, because at runtime, they contain only Objects.

But at least, generics will provide you from mistakes e.g.
List<Integer> li;
List<Double> ld = new ArrayList<Double>();
ld.add(new Double("2.3");
li = ld;
because the last line would not compile.
You'll get a compile time error and have to review your code.
In pre-generic times,
List li;
List ld = new ArrayList();
ld.add(new Double("2.3");
li = ld;
the code would be compiled.
Then only at runtime (e.g. when the code is already at the customer...) you will get an Exception when you try to cast the Double object to an Integer.

Compile time errors vs. runtime error this is all generics is about.


And, by the way, Obaid:
Welcome to the Ranch!



Yours,
Bu.
 
pie. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic