This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Need help understanding generics.

 
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?
 
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
 
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 ]
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!