• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Get in if you like generics

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look below and try to answer if this will compile. If it does what output will be generated (don't cheat).
Have fun

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think it will compile.....
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it will not compile
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can solve this like men; bring your six-shots and meet in the town middle at noon .
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile or Not compiling?? Question is not interesting??

what the result and How??..Some what Interesting??

Loking for response??
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your's compiles as well as the following: (it looks weird with ;;; but it runs fine)

class A<A extends Number> { A A;;; <A extends Number> A A(A A) {
System.out.println(A.getClass().getName());
return A; }}

class B extends A {

B(){System.out.println(new A().A(128));}
static public void main(String... String) {
A<Integer> A = new A<Integer>();
A.A = 1;
A<Byte> B = new B();

}}

OUTPUT:
C:\myJava>java B
java.lang.Integer
128
[ June 07, 2006: Message edited by: Firas Zureikat ]
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right guys (except S Thiyanesh - sorry man .
It compiles. Those A's look consfusing, but compiler says it's not problem for him.

Generic A in class declaration shadows class name A.
And generic A in method declaration shadows previously declared generic A.

Main with String...String parameter is ok cause variables can have same name as types.
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following also runs fine:

class A<A extends Number> { A A;;; <C extends Byte> C A(C A) {
System.out.println(A.getClass().getName());
return (C)A; }}

class B extends A {

B(){Byte x= new Byte("2");System.out.println(new A().A(x));}
static public void main(String... String) {
A<Integer> A = new A<Integer>();
A.A = 1;
A<Byte> B = new B();

}}

OUTPUT:
java.lang.Byte
2

REASON method A(C A) works:
Instance variable A extends Number but using "new A()" uses the non generic style (i.e. no need to be strict with generics in terms of what you pass the A constructor)
At the method level, C extends Byte and it is determined by the compiler that it's a byte when you sent the method the varable x. So casting variable A to a Byte is no problem because A is a Byte variable itself.
[ June 07, 2006: Message edited by: Firas Zureikat ]
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sheesh
dirty bit of coding
If only the makers knew how it would mess up the language....

where'd you pick up this thing from anyway? java puzzlers by bloch?
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't need to understand this level of generics to get all the generics questions right. :roll:
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made it myself .
It's true this is crapy question but it shows what messy code could be written (whitout errors) with generics.
 
warren li
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lucius Stephienn:
I made it myself .
It's true this is crapy question but it shows what messy code could be written (whitout errors) with generics.




you know that not everyone likes the decision to have generics in java right from the beginning.
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you know that not everyone likes the decision to have generics in java right from the beginning.



Yes I know. I was one of them, but If you want to pass SCJP 5.0 you have to learn it. Even if dislike it.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it was nice code Lucius Stephienn...It was like
for me...
I am preparing for SCJP1.5 but you scared me
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody tell me that the concept that "a superclass can not access it's members from a subclass with its own object applies only to PROTECTED MEMBERS..."
is true statement...
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dont be scared. These kind of question dont appear on exam (but watch out for shadowing; it's tricky).

Swapnil Trivedi: I dont understand your question, could you be more precise?
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucius,
Thanks dear, I have got my doubt

Lucius do you have the link to the mock exams from Sun microsystems.

[ June 09, 2006: Message edited by: Swapnil Trivedi ]
[ June 09, 2006: Message edited by: Swapnil Trivedi ]
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

static public void main(String... String) {}



Isn't String a keyword? If so, why is is accepted above as a variable name?

I thought keywords are not supposed to be used a variable literals.
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Sun? No. The only links I have are these from javaranch (there was a thread about mocks).

Firas Zureikat: String isn't a keyword, better remember it. Moreover, you can write variable names same as types:

Object Object = new Object();
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, It takes time to get used to the fact that a type name could be given to a variable name as in args main() about.

Thanks
 
Don't play dumb with me! But you can try this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic