• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java puzzle: fun with classes

 
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, some more Java fun. The following will print 'You won'.
What arguments (you may modify both!) to check would make the code compile without casting to Class, and still result in "You won" being  printed?
(I've used the fully specified name of Arrays so you don't get funny ideas and create a class with that name, adding a static equals method that simply returns false all the time.)
Please post your base64-encoded answer as a comment, so you don't spoil the fun for others. Long-time readers of Peter Verhas's blog, javax0.wordpress.com, will be at a definite advantage (true in general, BTW).
 
Marshal
Posts: 70251
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is tricky, but I have worked out how it works, and also how it would get you fired on the spot if you use it at work I think the documentation for Arrays#equals() will also help. Presumably you would want the method you don't want to returrn false regardless, however
 
Istvan Kovacs
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right in having to return false, not true, I've updated the post. Thanks!
 
Istvan Kovacs
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why the Javadocs of Arrays#equals would be helpful, or why something related to this puzzle would get one fired.
Instead, I'd recommend checking out Class.getDeclaredMethods.
 
Campbell Ritchie
Marshal
Posts: 70251
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You think you can get away with using a cast that produces an “unchecked” compiler warning? Remember this is the “diversions” forum where you are allowed to exaggerate. Maybe I was mistaken about Arrays#equals().
 
Istvan Kovacs
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Maybe I did not explain properly what the challenge is about.
<T> void check(Class<T> type1, Class<T> type2) enforces that type1 and type2 are both Class<Foo> (if T=Foo).
If this were not enforced, it'd be trivial to just send two unrelated classes to check, and their methods would be different.
By using the raw cast (from Class<String> to raw Class), we pretty much 'disable the generics' on the invocation of check), since, based on the 2nd argument, the compiler will deduce that T = Object, and the raw type Class can be assigned to any parameterised Class<T> reference, so we'll have an assignment of Class (pointing at String.class) to Class<Object> as the 1st argument to check.

Now, your job is to find arguments for check such that the cast is not needed. In other words, replace ABC and XYZ with something that
  • does not involve a cast
  • results in compilable code
  • and prints 'You won'



  •  
    Campbell Ritchie
    Marshal
    Posts: 70251
    282
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I got the bit about the cast to the raw type being interpreted as Class<Object>. I am not sure I can get the arguments without the cast. I concede defeat!
     
    Istvan Kovacs
    Ranch Hand
    Posts: 111
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK, sorry about the edit.
    Would you like a hint, or the solution?
     
    Campbell Ritchie
    Marshal
    Posts: 70251
    282
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Apology accepted Let's leave it for a day or so in case somebody else gets the solution.
     
    Rancher
    Posts: 3626
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Istvan Kovacs wrote:I've used the fully specified name of Arrays so you don't get funny ideas



    I note you didn't do the same for Class... not sure if that's intentional. There's a solution that exploits that omission.
     
    Mike Simmons
    Rancher
    Posts: 3626
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And another solution that doesn't exploit that loophole.  But it's hard to tell what's "legal" for the puzzle, and what isn't.
     
    Mike Simmons
    Rancher
    Posts: 3626
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBjaGVjayhnZXRDbGFzcygiRm9vIiksIGdldENsYXNzKDEyMykpOwogICAgfQogICAgCiAgICBwcml2YXRlIHN0YXRpYyBDbGFzcyBnZXRDbGFzcyhPYmplY3Qgb2JqKSB7CiAgICAgICAgcmV0dXJuIG9iai5nZXRDbGFzcygpOwogICAgfQogICAgCiAgICBwcml2YXRlIHN0YXRpYyA8VD4gdm9pZCBjaGVjayhDbGFzczxUPiB0eXBlMSwgQ2xhc3M8VD4gdHlwZTIpIHsKICAgICAgICBpZiAoIWphdmEudXRpbC5BcnJheXMuZXF1YWxzKHR5cGUxLmdldERlY2xhcmVkTWV0aG9kcygpLCB0eXBlMi5nZXREZWNsYXJlZE1ldGhvZHMoKSkpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJZb3Ugd29uIik7CiAgICAgICAgfQogICAgfQ==
     
    Istvan Kovacs
    Ranch Hand
    Posts: 111
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, that's essentially the same as a raw cast.
    Hint (quoting the Javadoc of getDeclaredMethods()): SWYgdGhpcyBDbGFzcyBvYmplY3QgcmVwcmVzZW50cyBhbiBhcnJheSB0eXBlLCBhIHByaW1pdGl2ZSB0eXBlLCBvciB2b2lkLCB0aGVuIHRoZSByZXR1cm5lZCBhcnJheSBoYXMgbGVuZ3RoIDAuIA==
     
    Istvan Kovacs
    Ranch Hand
    Posts: 111
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Mike Simmons wrote:

    Istvan Kovacs wrote:I've used the fully specified name of Arrays so you don't get funny ideas



    I note you didn't do the same for Class... not sure if that's intentional. There's a solution that exploits that omission.



    It was unintentional. The solution does not need any tricks. You can solve it in 22 characters, even if nicely formatted. :-)
     
    Mike Simmons
    Rancher
    Posts: 3626
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, I for one am curious to see what your intended solution was then.
     
    Istvan Kovacs
    Ranch Hand
    Posts: 111
    1
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ynl0ZS5jbGFzcywgQnl0ZS5jbGFzcw==
     
    Mike Simmons
    Rancher
    Posts: 3626
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah, interesting.  Thanks!
     
    I found a beautiful pie. And a tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
      Bookmark Topic Watch Topic
    • New Topic