Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: type erasure

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default type erasure

    Note: This may not be the appropriate subforum, but I found no way to select one for my thread. I apologize in advance.

    I am quite familiar with C and C++, but new to Java. With some trivial modifications for brevity, I find the following example of type erasure in Oracle's The Java Tutorials perplexing:

    Given the following two classes:

         public class Node<T> {
     
              public T data;
     
              public Node(T data) { this.data = data; }
     
              public void setData(T data) { this.data = data; }
         }
     
         public Class MyNode extends Node<Integer> {
     
              public MyNode(Integer data) { super(data); }
     
              public void setData(Integer data) { super.setData(data); }
         }
    }

    Consider the following code:

         MyNode mn = new MyNode(5);
         Node n = mn;                                   // A raw type - compiler throws an unchecked warning
         n.setData( "Hello" );
         Integer x = mn.data;                        // Causes a ClassCastException to be thrown.

    After type erasure, the code becomes:

         MyNode mn = new MyNode(5);
         Node n = (MyNode)mn;                     // A raw type - compiler throws an unchecked warning
         n.setData( "Hello" );
         Integer x = (String)mn.data;             // Causes a ClassCastException to be thrown.

    End of example

    Type erasure merely adds the two type casts to the original code. The author writes that "trying to assign a String to an Integer (my note: in the final line of the type erasure code) causes a ClassCastException from a cast inserted at the assignment by a Java compiler."

    To me, though, the two type casts are superfluous. (MyNode) type casts a MyNode object to a MyNode. How does this contribute anything? And the ClassCastException should be thrown at run-time even without the (String) type cast.

    What am I missing?

    Thanks in advance.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: type erasure

    What happens when you fix statement with the warning?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: type erasure

    This is just an example of how the compiler catches misuses of generic types. I just don't see why the two compiler casts in the type erasure code are needed at all here--especially why the misuse wouldn't be caught at runtime without the (String) cast in the erasure code, which the author of the example said was needed for this purpose.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: type erasure

    Sorry, I'm not into why the compiler writers did what they did. Perhaps some of the helpers on this forum could explain it better:
    http://www.coderanch.com/forums
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: type erasure

    This is just an example of how the compiler catches misuses of generic types. I just don't see why the two compiler casts in the type erasure code are needed at all here--especially why the misuse wouldn't be caught at runtime without the (String) cast in the erasure code, which the author of the example said was needed for this purpose.

    --- Update ---

    I should add that generics are not the issue here, but rather, type casting. I am quite willing to accept that the (MyNode) cast is just an artifact of the compiler and, in fact, is superfluous. However, the writer's comment regarding the (String) cast bothers me in that some exception should be thrown without the need for the cast. The cast, though, does make it a ClassCastException, so maybe that's the point of the cast.

    I think I may have answered my own question. The compiler probably casts every expression in the type erasure code like this as a matter of course. Its(MyNode) cast is superfluous, and its (String) cast results in a ClassCastException. A different runtime exception would result without the cast.

    Thanks, Norm, for making me think this through!
    Last edited by Steve; July 16th, 2019 at 12:51 PM.

Similar Threads

  1. [SOLVED] how to convert a input array of type Strings to a class type
    By adit in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 17th, 2014, 07:46 PM
  2. Generics & 'Type Erasure': disingenuous documentation?
    By cyberion in forum Java Theory & Questions
    Replies: 27
    Last Post: September 8th, 2013, 03:02 PM
  3. Type Erasure -- Sales pitch?
    By 2by4 in forum Collections and Generics
    Replies: 0
    Last Post: December 10th, 2011, 06:59 AM
  4. Type magic
    By meathead in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2011, 12:12 PM
  5. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM