aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need help understanding generics. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need help understanding generics." Watch "Need help understanding generics." New topic
Author

Need help understanding generics.

Akshatha Bhat
Greenhorn

Joined: Oct 29, 2008
Posts: 4
public class Aks<G>
{
G g;
Aks(G g)
{
this.g =g;
}
public static void main(String[] args)
{
Aks<String> arr[] = new Aks[5]; //line 1
arr[0] = new Aks("Java"); //line 2
arr[1] = new Aks(1); //line 3

}
}

How does line 3 work ? Isn't arr[] supposed to store only string values?
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
It is not type safe on right hand side(new Aks[5]). it means your are allowed to put anything in the array.

Please correct me if am wrong.

Preparing Scjp1.5
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
Originally posted by Preetha Arun:
It is not type safe on right hand side(new Aks[5]). it means your are allowed to put anything in the array.

Please correct me if am wrong.

Preparing Scjp1.5


That's is essentially my understanding. The Langauge Specification states (http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#108850):

"If no bound is given for a type variable, Object is assumed. "

thanks,

Graeme
Akshatha Bhat
Greenhorn

Joined: Oct 29, 2008
Posts: 4
I mean isn't arr[] supposed to store Aks objects which take only string values?
Akshatha Bhat
Greenhorn

Joined: Oct 29, 2008
Posts: 4
If line 1 were to be this :

Aks arr[] = new Aks<String>[5]; //line 1

then arr[] does exhibit generic behaviour and can store any Aks object and not necessarily Aks which takes in a string.
Akshatha Bhat
Greenhorn

Joined: Oct 29, 2008
Posts: 4
* does not
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
It seems that this may not be a straighforward as I thought. Typing "java generics array" into your favoured search engine brings up a lot of links all of which suggest that (at least in Java 5) there is a problem in creating typesafe arrays using generics (I haven't had time to look at this properly but it appears to be a consequence of the type erasure at runtime). Perhaps some other ranchers could spread some light on the subject?

(Given the issues I can only assume that this isn't really in scope for SCJP )
[ October 29, 2008: Message edited by: Graeme Jenkinson ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17



In the above code, the first assignment of int doesn't generate any error, but the second one will. That's because in the second assignment the compiler knows that you are doing an illegal assignment. But in the first one, the compiler will warn that the assignment is not safe and might result in exceptions at run-time...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
i tried the code ,and found out that the 3 assignment will all cause a warning,my question is how to compile without warning


SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help understanding generics.