Results 1 to 8 of 8
Like Tree4Likes
  • 2 Post By KevinWorkman
  • 1 Post By DarrylBurke
  • 1 Post By gimbal2

Thread: Why can't *this* not be checked by the compiler?

  1. #1
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Why can't *this* not be checked by the compiler?

    I have a class with a member

    ArrayList<String> elements;

    and the following member function:

    public ArrayList<String> sorted() {
    ArrayList<String> result=(ArrayList<String>)elements.clone();
    Collections.sort(result);
    return result;
    }

    The compiler complains:

    uses unchecked or unsafe operations.

    From what I understand, this warning is issued when the compiler can't verify that the correct type is passed, and that this typically relates to template types.

    Now I have three questions to this:

    1. What exactly is unsafe here? I explicitly cast the result of clone(), and sort should accept a List<String>. Since ArrayList implements the List interface, this should be fine.

    2. As I certainly don't want to live with this warning, how do I fix it?

    3. The compiler didn't issue a line number for this warning. I only new that the problem must be in *that* code I've posted here, because this was the only thing I've changed. If I hack a longer piece of code, and then compile, I can only guess where the error is located (or comment out the new code piece by piece, until I find the culprit). A nightmare! There must be an easier way. How do experienced Java programmers cope with such a situation?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Why can't *this* not be checked by the compiler?

    Quote Originally Posted by rovf View Post
    1. What exactly is unsafe here? I explicitly cast the result of clone(), and sort should accept a List<String>. Since ArrayList implements the List interface, this should be fine.
    It's giving the warning because the clone() function is only guaranteed to return an Object. You're casting that Object to an ArrayList that uses generics, so you get the warning.

    Quote Originally Posted by rovf View Post
    2. As I certainly don't want to live with this warning, how do I fix it?
    If it really bothers you there's an annotation for ignoring warnings, or you could set the compiler itself to ignore the warning.

    Quote Originally Posted by rovf View Post
    3. The compiler didn't issue a line number for this warning. I only new that the problem must be in *that* code I've posted here, because this was the only thing I've changed. If I hack a longer piece of code, and then compile, I can only guess where the error is located (or comment out the new code piece by piece, until I find the culprit). A nightmare!
    If you want more information, the warning should tell you to compile with an extra optional flag for more info.

    Quote Originally Posted by rovf View Post
    There must be an easier way. How do experienced Java programmers cope with such a situation?
    This is just a compiler warning. We cope by not worrying about warnings that don't matter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Why can't *this* not be checked by the compiler?

    Or in this case: just write code that is guaranteed safe.

    Java Code:
    public ArrayList<String> sorted() {
      ArrayList<String> result=new ArrayList<String>();
      result.addAll(elements);
      Collections.sort(result);
      return result;
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Why can't *this* not be checked by the compiler?

    Note that ArrayList, like other collections, has a "copy constructor" that takes a Collection as an argument.
    gimbal2 and rovf like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: Why can't *this* not be checked by the compiler?

    Thanks for the explanation. I didn't know that casting into a generic class would rise such a warning, and I've placed an annotation to ignore the warning, as you suggested.

    Just being curious: Is there a reason, why this cast is treated differently from the casts to non-generic types? In both cases, the compiler verifies at run-time, that the cast is possible, and throws and exception if I'm casting some nonsense. Isn't it?

  6. #6
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: Why can't *this* not be checked by the compiler?

    Thanks a lot! Indeed, this makes even more sense.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Why can't *this* not be checked by the compiler?

    Quote Originally Posted by rovf View Post
    ...the compiler verifies at run-time
    No, the JVM throws a ClassCastException at runtime. The compiler's role is over once the source code is compiled to bytecode.

    db
    rovf likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Why can't *this* not be checked by the compiler?

    Quote Originally Posted by DarrylBurke View Post
    No, the JVM throws a ClassCastException at runtime. The compiler's role is over once the source code is compiled to bytecode.

    db
    I wish my job were so easy. You just build the stuff according to specs and then a whole other entity entirely is tasked with checking and reporting the semantics related problems.
    rovf likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Checked Exception
    By deeevo in forum New To Java
    Replies: 1
    Last Post: 06-13-2013, 10:58 AM
  2. why we need checked exceptions
    By akiravelmont in forum New To Java
    Replies: 2
    Last Post: 12-03-2011, 08:51 PM
  3. Replies: 1
    Last Post: 02-08-2010, 02:57 PM
  4. radio button checked by default
    By whiteasshweta in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 07-06-2009, 02:11 PM
  5. Checked Exception
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:59 PM

Tags for this Thread

Posting Permissions

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