• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

doubt on code

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain this code
public class Gen<G>
{
G g;
Gen(G g)
{
this.g =g;
}
public static void main(String[] args)
{
Gen<String> arr[] = new Gen[5]; //line 1
arr[0] = new Gen("Java"); //line 2
arr[1] = new Gen(1); //line 3
arr[2] = (Gen<String>) new Gen(1); //line 4
arr[3] = (Gen<String>) new Gen<Integer>(1); //line 5
for(Gen o:arr)
{
System.out.println(o);
}
}
}

What is the effect of the above program

a)Compile time Error at line 1
b)Compile time Error at line 3
c)Compile time Error at line 4
d)Compile time Error at line 5
e)Run Time Error

---
I removed the smileys, Bu.

[ August 03, 2007: Message edited by: Burkhard Hassel ]
[ August 03, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi aneea,

what do you think is the correct answer and why?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have a compile time error at line 5 beause you are trying to cast Gen<Integer> to Gen<String>.
All the other lines are compiling correct but generate several waring because as you do not specify (unchecked call) the which specific class will be the core of the Generic class: anyway it will run.

ciao
Maurizio
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Maurizio Nagni

Please tell if i am correct or not?

What i think is compile time error must come
at line 3 bcoz in the main method


Now here it is shown Generic will take only String types
but later at line 3 we are trying to insert integer 1......

I am getting confused with the above ..........
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:
Please tell if i am correct or not?

Sorry, but Maurizio is right.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Gen<G>
{
G g;
Gen(G g)
{
this.g =g;
}
public static void main(String[] args)
{
Gen<String> arr[] = new Gen[5]; //line 1
arr[0] = new Gen("Java"); //line 2
arr[1] = new Gen(1); //line 3
arr[2] = (Gen<String> new Gen(1); //line 4
arr[3] = (Gen<String> new Gen<Integer>(1); //line 5
for(Gen o:arr)
{
System.out.println(o);
}
}
}


1-4 lines are correct
[Assign Non - Generic Objects of Gen and Gen Sub types only to Generic Reference]

5 th line you cannot assign Incompatible and Polymorphic Sub Types(Exception when you use wild character ?).
reply
    Bookmark Topic Watch Topic
  • New Topic