Results 1 to 10 of 10
  1. #1
    this.that is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default The Generic Trend

    So i came across an issue recently while I was trying to get fancy with generic coding. It actually took me a while to figure out that the culprit was actually how generics are implemented/designed/whatever in Java. This could actually be a trend because I am pretty sure C# has the same behavior, C++ traditionally does not.


    public class Generics {

    /**
    Suspected output:

    test1: test(Object)
    test2: test(String)
    test3: test(Object)
    test4: test(Object) --- this is sooo very wrong! (IMO)
    test5: test(String)
    **/
    public static void main(String[] args) {
    Object o = new String("DFG");
    String s = "sdfsdfsd";

    System.out.print("test1: ");
    test(o);

    System.out.print("test2: ");
    test(s);

    System.out.print("test3: ");
    gtest(o);

    System.out.print("test4: ");
    gtest(s);

    System.out.print("test5: ");
    gtest2(s);
    }

    public static void test(String s) {
    System.out.println("test(String)");
    }

    public static void test(Object o) {
    System.out.println("test(Object)");
    }

    public static <T> void gtest(T t) {
    test(t);
    }

    public static <S extends String> void gtest2(S s) {
    test(s);
    }
    }


    It is my guess that gtest() will always get Object... *SNIF* Oh the fun I could have if this were not the case!

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I think most of us are confused by what you are trying to accomplish. You don't seem to define types T and S. Does this even compile?

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, it does . Those last two methods are methods defined using Generics. What T and S actually represent will determined by what is passed to the method as a parameter.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I happened to look at another thread on generics after this one, and I have a better understanding of the role of generics defined within a method signature represent.

    One note: generics are annotations that are resolved at compile time. They are completely invisible at run time.

    In the code above, the compiler determines that

    gtest() takes a parameter of unspecified type, which I'm guessing roughly equates to Object.

    gtest2() takes a parameter of type that extends String.

    Therefore,

    all calls to gtest() will invoke the Object version of test().

    all calls to gtest2() will invoke the String version of test(), but gtest2() can only be invoked with a parameter the compiler knows is a String.

    Complaining about test 4 is silly. At compile time, the compiler does not know what Type gtest() will receive, so it must invoke the Object version.

    The OP was hoping that a decision would be made at run time as to which overloaded method to call by looking at the actual Type of the parameter instance passed, but the compiler is not going to create that sort of code.

    Imagine a similar but not static method was overloaded further by an extending class at run time. The compiler's code would then be in error.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    This is because the C++ version of Generics (which he is, seemingly, familiar with) will, but that is not because it is determining type at runtime, it is because Generics in C++ (AFAIK) is realised using templates, which actually produces multiple versions of the same code. In Java (and seemingly in C#, as well) Generics is implemented using erasure.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    masijade is correct, and this is one of the complaints leveled against the Java generics implementation. Generics compile to Object types. Generics gives you compile time type safety, but not run time reification.

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I have to admit that I muttered a complaint when I found out that generic type information was not preserved at run time, but the current generic implementation is a huge improvement over what was there before.

    However, even if the generic type was preserved, that would not help the compiler identify which overloaded method to invoke, especially when the overload occurred in an extending class at run time.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    (Let me preface this with: "If I understand templates halfway correctly".) The thing with templates, is that in the compilation process, the templates are done first, and these produce multiple versions of code that are then compiled separately (although in the same process). Therefore, the type was recognised at compile time. However, that precompilation **** is exactly the sort of thing that I wouldn't want, ever to be introduced into Java. So, it is a live with it situation. ;-)

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I imagine that would have to be done during link editing, when all the dependencies are be resolved. I *definitely* prefer the late binding model in Java, regardless of the drawbacks...

  10. #10
    this.that is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Well I am really coming at this from a design perspective, which doesn't really take into account how Java or any language for that matter implements features like generics under the hood. I just think that if my code instantiates a String it should be a String at run time.. It shouldn't matter what function is getting it (generic or not). I do not claim to be smart enough to figure out the best way to actually do this and I know there are always trade offs.

    Generics as is are a tremendous improvement there is no doubt, but the current implementation limits the reach of generic and object oriented programming quite a bit.

Similar Threads

  1. A generic interface example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:42 PM
  2. A simple generic class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:41 PM
  3. Writing generic methods
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 04:28 AM
  4. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:12 AM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 11:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •