• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generics

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;

public class Practice {

/**
* @param args
*/
public static void main(String[] args) {
Practice p = new Practice();
ArrayList a = p.get();
a.add(1);
System.out.println("in main" +a );
}

public ArrayList<String> get() {
ArrayList<String> a = new ArrayList<String>();
a.add("ss");
a.add("aa");
a.add("dd");
//a.add(2);
System.out.println("in " +a);
return a;
}

}


The problem is :

i am creating new ArrayList which will accept only String object in get method(). Return type of method is also ArrayList<String>.
But when method is called in main method (or any other method) and reference of ArrayList is given to that method,ArrayList reference is able to add all object here.

Whats the concept behind it..?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags, when you post codes.

And, normally, we can put/add anything to collections. But we are restricting the collections to hold some types of object by using generics. e you break the generics, then you can add/put anything into that collections.

Here, you break the generic behavior of the ArrayList by referring the ArrayList using legacy collection. So with the reference of legacy collection a, you can add anythings.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Satyajit Bhadange wrote:



The problem is :

i am creating new ArrayList which will accept only String object in get method(). Return type of method is also ArrayList<String>.
But when method is called in main method (or any other method) and reference of ArrayList is given to that method,ArrayList reference is able to add all object here.

Whats the concept behind it..?



Generics is used to ensure type check during compile time. You are using generics only for the method get(), So will return an ArrayList. You need to apply generics for the returned ArrayList as well.


Then you cannot add anything other than string
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply...but i m still not satisfied..

What i understand is ArrayList a = null,

here a is reference..

when i used new keyword at that time object of ArrayList will be created which will hold only Strings,
Now when i am just referring that object through other reference but it's still pointing to same object on heap,so restriction of adding other objects(integer) should be there even if we are referring via other reference.

 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you dropped the generic type (<String>) the ArrayList became a raw type ArrayList. This is similar to an ArrayList<Object> - you can put anything in it. That's why you should try to avoid this as much as possible; most IDEs issue a warning for a reason.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok..

now i got the concept..

Object is superclass and check is done at compile time.. Thank you very much
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic