This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help understanding generics.

 
Akshatha Bhat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean isn't arr[] supposed to store Aks objects which take only string values?
 
Akshatha Bhat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* does not
 
Graeme Jenkinson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried the code ,and found out that the 3 assignment will all cause a warning,my question is how to compile without warning
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic