This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
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..?


Thanks
user101
Problems And Solutions - Algorithms
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 57
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

Joined: May 13, 2010
Posts: 104
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Ok..

now i got the concept..

Object is superclass and check is done at compile time.. Thank you very much
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics
 
Similar Threads
Having some trouble with this code.
doubt in hashCode and Equals method
testing a delimeter
toString() method
flow of the program?