Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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..?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic