Type safety warning...

Discussion in 'Java' started by grz01, Sep 23, 2009.

  1. grz01

    grz01 Guest

    Hi,

    This code:

    String s = "a;b;c";
    List<String> l = Arrays.asList(s.split(";"));

    gives me a warning:

    Type safety: The expression of type List needs unchecked
    conversion to conform to List<String>

    Can someone pse help me correct the second line to avoid this warning
    (without using "@SuppressWarnings") ?

    TIA / grz01
    grz01, Sep 23, 2009
    #1
    1. Advertising

  2. grz01 <> wrote:
    > This code:
    > String s = "a;b;c";
    > List<String> l = Arrays.asList(s.split(";"));
    > gives me a warning:
    > Type safety: The expression of type List needs unchecked
    > conversion to conform to List<String>


    It doesn't for me. (jdk 1.6.0_16 here)

    import java.util.List;
    import java.util.Arrays;
    public class Test {
    public static void main(String[] a) {
    String s = "a;b;c";
    List<String> l = Arrays.asList(s.split(";"));
    }
    }

    No warnings on compile. Which jdk do you use, and
    do you compile from command-line, or through some
    IDE, like eclipse and netbeans? They are known to
    complain about much more, but can be configured
    not to barf at not-really-barfworthy things.
    Andreas Leitgeb, Sep 23, 2009
    #2
    1. Advertising

  3. grz01

    grz01 Guest

    > No warnings on compile.  Which jdk do you use, and
    > do you compile from command-line, or through some
    > IDE, like eclipse and netbeans? They are known to
    > complain about much more, but can be configured
    > not to barf at not-really-barfworthy things.


    Hi,

    Yes, I am building in Eclipse.
    jdk is 1.6.

    / grz01
    grz01, Sep 23, 2009
    #3
  4. grz01

    Qu0ll Guest

    "grz01" <> wrote in message
    news:...
    >> No warnings on compile. Which jdk do you use, and
    >> do you compile from command-line, or through some
    >> IDE, like eclipse and netbeans? They are known to
    >> complain about much more, but can be configured
    >> not to barf at not-really-barfworthy things.

    >
    > Hi,
    >
    > Yes, I am building in Eclipse.
    > jdk is 1.6.


    You can configure Eclipse to not produce a warning in such situations by
    going to Preferences -> Java -> Compiler -> Errors/Warnings -> Generic types
    and change "Unchecked generic type operation" to "Ignore".

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Sep 23, 2009
    #4
  5. grz01

    grz01 Guest

    > You can configure Eclipse to not produce a warning in such situations by
    > going to Preferences -> Java -> Compiler -> Errors/Warnings -> Generic types
    > and change "Unchecked generic type operation" to "Ignore".



    OK.
    But would still be nice to understand exactly *what* it is actually
    warning abt?
    Generally, I definitely prefer "type-safe" programming, if possible,
    rather than just turn off the warnings :)

    How are you actually supposed to write the assignment above?

    / grz01
    grz01, Sep 23, 2009
    #5
  6. grz01

    grz01 Guest

    On Sep 23, 1:32 pm, Michal Kleczek <> wrote:
    > grz01 wrote:
    > >> No warnings on compile.  Which jdk do you use, and
    > >> do you compile from command-line, or through some
    > >> IDE, like eclipse and netbeans? They are known to
    > >> complain about much more, but can be configured
    > >> not to barf at not-really-barfworthy things.

    >
    > > Hi,

    >
    > > Yes, I am building in Eclipse.
    > > jdk is 1.6.

    >
    > What version of Eclipse? Mine does not give any warnings. Looks like it is a
    > bug in Eclipse JDT compiler.
    > Although not necessary because the compiler should be able to infer right
    > types you can try:
    >
    > List<String> l = Arrays.<String>asList(s.split(";"));
    >
    > --
    > Michal



    Sorry, just discovered I had imported Arrays from the wrong package!
    Oh well...
    Thanks, anyway
    grz01, Sep 23, 2009
    #6
  7. grz01 <> wrote:
    >> You can configure Eclipse to not produce a warning in such situations by
    >> going to Preferences -> Java -> Compiler -> Errors/Warnings -> Generic types
    >> and change "Unchecked generic type operation" to "Ignore".

    > OK.
    > But would still be nice to understand exactly *what* it is actually
    > warning abt?


    about a wrongly imported Arrays class, of course!

    (just kidding, saw your posting in the other subthread :)
    Andreas Leitgeb, Sep 23, 2009
    #7
  8. grz01

    Lew Guest

    Qu0ll wrote:
    > You can configure Eclipse to not produce a warning in such situations by
    > going to Preferences -> Java -> Compiler -> Errors/Warnings -> Generic
    > types and change "Unchecked generic type operation" to "Ignore".


    TERRIBLE advice! Do not follow that advice! Bad, bad, bad advice!

    --
    Lew
    Lew, Sep 23, 2009
    #8
  9. grz01

    grz01 Guest

    On Sep 23, 2:51 pm, Lew <> wrote:
    > TERRIBLE advice!  Do not follow that advice!  Bad, bad, bad advice!


    I agree Lew... but no need to shout ;)
    grz01, Sep 23, 2009
    #9
  10. grz01

    Qu0ll Guest

    "Lew" <> wrote in message
    news:h9d5k0$lmj$...
    > Qu0ll wrote:
    >> You can configure Eclipse to not produce a warning in such situations by
    >> going to Preferences -> Java -> Compiler -> Errors/Warnings -> Generic
    >> types and change "Unchecked generic type operation" to "Ignore".

    >
    > TERRIBLE advice! Do not follow that advice! Bad, bad, bad advice!


    I didn't say they *should* do it, merely that they *could* do it if the
    warning was a problem for them. Personally, I have all the warnings turned
    on but YMMV.

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Sep 23, 2009
    #10
  11. grz01

    Wojtek Guest

    grz01 wrote :
    > On Sep 23, 2:51 pm, Lew <> wrote:
    >> TERRIBLE advice!  Do not follow that advice!  Bad, bad, bad advice!

    >
    > I agree Lew... but no need to shout ;)


    Oh yes there is.

    Things like turing off compiler checks and @SuppressWarnings should
    come with a loud noise.

    While they are there for a reason, it is much like using funky pointer
    arithmatic in C. Yes it IS possible, but it should not be used unless
    you realize the full implications of using it.

    So while I do use @SuppressWarnings (with lots of comments and a TODO
    task) and I do have some compiler checks turned off, I am cognizant of
    what may happen if I am wrong, and they were not used lightly.

    A newbie has no such practical experience to draw upon.

    --
    Wojtek :)
    Wojtek, Sep 23, 2009
    #11
  12. grz01

    Lew Guest

    Lew wrote:
    >> TERRIBLE advice!  Do not follow that advice!  Bad, bad, bad advice!

    >


    grz01 wrote:
    > I agree Lew... but no need to shout   ;)


    I assessed that there was. :)

    I was acting for your protection, grz01, and others who read this
    thread. When someone is trying to put out a fire and someone else
    says, "Just put your bare hand into the flame and smother it,"
    wouldn't you shout, "NO! Don't do that!"?

    Colorful analogies aside, Qu0ll has since explained that it was not
    advice, despite being presented exactly as advice in a context where
    it would be taken as advice without disclaimer that it was not
    advisable, but mere information about a possible, though bad,
    practice. I shall ignore the sophistry of that backpedaling and
    acknowledge that he gave good information, though it would make for
    bad advice.

    Phew! Crisis averted.

    --
    Lew
    Lew, Sep 23, 2009
    #12
  13. grz01

    Roedy Green Guest

    On Wed, 23 Sep 2009 02:47:59 -0700 (PDT), grz01 <>
    wrote, quoted or indirectly quoted someone who said :

    >This code:
    >
    > String s = "a;b;c";
    > List<String> l = Arrays.asList(s.split(";"));


    The problem is rarely where you think it is. See
    http://mindprod.com/jgloss/sscce.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Don’t worry about people stealing an idea; if it’s original, you’ll have to shove it down their throats."
    ~ Howard Aiken (born: 1900-03-08 died: 1973-03-14 at age: 73)
    Roedy Green, Sep 23, 2009
    #13
  14. grz01

    Lew Guest

    Wojtek wrote:
    > So while I do use @SuppressWarnings (with lots of comments and a TODO
    > task) and I do have some compiler checks turned off, I am cognizant of
    > what may happen if I am wrong, and they were not used lightly.


    Properly used, @SuppressWarnings("unchecked") would never come with a
    TODO because there would be nothing else to do.

    Joshua Bloch discusses this annotation in /Effective Java/, chapter 5,
    available for free from
    <http://java.sun.com/docs/books/effective/>

    The proper use of @SuppressWarnings("unchecked") is in places where
    you cannot impose compile-time restraints, say casting the result from
    a third-party library or certain operations involving arrays, but
    where you have taken other steps to ensure that you cannot get a
    ClassCastException.

    --
    Lew
    Lew, Sep 23, 2009
    #14
  15. grz01

    Wojtek Guest

    Lew wrote :
    > Wojtek wrote:
    >> So while I do use @SuppressWarnings (with lots of comments and a TODO
    >> task) and I do have some compiler checks turned off, I am cognizant of
    >> what may happen if I am wrong, and they were not used lightly.

    >
    > Properly used, @SuppressWarnings("unchecked") would never come with a
    > TODO because there would be nothing else to do.
    >
    > Joshua Bloch discusses this annotation in /Effective Java/, chapter 5,
    > available for free from
    > <http://java.sun.com/docs/books/effective/>
    >
    > The proper use of @SuppressWarnings("unchecked") is in places where
    > you cannot impose compile-time restraints, say casting the result from
    > a third-party library or certain operations involving arrays, but
    > where you have taken other steps to ensure that you cannot get a
    > ClassCastException.


    It may be possible, however unlikely, that a future language change
    will make that @SuppressWarnings unnecessary.

    I like to leave in these little markers so I can go over them whenever
    I upgrade the Java version.

    --
    Wojtek :)
    Wojtek, Sep 23, 2009
    #15
  16. "Wojtek" <> wrote in message
    news:
    > Lew wrote :
    >> Wojtek wrote:
    >>> So while I do use @SuppressWarnings (with lots of
    >>> comments and a TODO task) and I do have some compiler
    >>> checks turned off, I am cognizant of what may happen if
    >>> I am wrong, and they were not used lightly.

    >>
    >> Properly used, @SuppressWarnings("unchecked") would
    >> never come with a TODO because there would be nothing
    >> else to do. Joshua Bloch discusses this annotation in /Effective
    >> Java/, chapter 5, available for free from
    >> <http://java.sun.com/docs/books/effective/>
    >>
    >> The proper use of @SuppressWarnings("unchecked") is in
    >> places where you cannot impose compile-time restraints,
    >> say casting the result from a third-party library or
    >> certain operations involving arrays, but where you have
    >> taken other steps to ensure that you cannot get a
    >> ClassCastException.

    >
    > It may be possible, however unlikely, that a future
    > language change will make that @SuppressWarnings
    > unnecessary.
    > I like to leave in these little markers so I can go over
    > them whenever I upgrade the Java version.


    JPA Query for example is not a Query<T> but plain Query.

    Query.getResultList() requires @SuppressWarnings("unchecked") no matter
    what.

    Sun is to blame, its their interface.
    Donkey Hottie, Sep 23, 2009
    #16
  17. grz01

    Qu0ll Guest

    "Lew" <> wrote in message
    news:...
    > Lew wrote:
    >>> TERRIBLE advice! Do not follow that advice! Bad, bad, bad advice!

    >>

    >
    > grz01 wrote:
    >> I agree Lew... but no need to shout ;)

    >
    > I assessed that there was. :)
    >
    > I was acting for your protection, grz01, and others who read this
    > thread. When someone is trying to put out a fire and someone else
    > says, "Just put your bare hand into the flame and smother it,"
    > wouldn't you shout, "NO! Don't do that!"?
    >
    > Colorful analogies aside, Qu0ll has since explained that it was not
    > advice, despite being presented exactly as advice in a context where
    > it would be taken as advice without disclaimer that it was not
    > advisable, but mere information about a possible, though bad,
    > practice. I shall ignore the sophistry of that backpedaling and
    > acknowledge that he gave good information, though it would make for
    > bad advice.
    >
    > Phew! Crisis averted.


    Lew, I try to help where I can given that I have received so much help from
    this group over the years. I don't always get it right and unfortunately
    the caning I receive when I don't quite hit the mark tends to discourage me
    from making further such attempts in the future.

    You are absolutely correct that it was bad advice but, as I have already
    explained, it was more an an effort to point out a feature in Eclipse than
    suggest a recommended practice. I couldn't see any reason for the warning
    in the code presented so I wanted the OP to know that they did have the
    option to ignore the warning if it was resulting from a bug in the Eclipse
    compiler.

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Sep 23, 2009
    #17
  18. grz01

    Lew Guest

    Qu0ll wrote:
    > Lew, I try to help where I can given that I have received so much help from
    > this group over the years.  I don't always get it right and unfortunately
    > the caning I receive when I don't quite hit the mark tends to discourage me
    > from making further such attempts in the future.
    >
    > You are absolutely correct that it was bad advice but, as I have already
    > explained, it was more an an effort to point out a feature in Eclipse than
    > suggest a recommended practice.  I couldn't see any reason for the warning
    > in the code presented so I wanted the OP to know that they did have the
    > option to ignore the warning if it was resulting from a bug in the Eclipse
    > compiler.


    "Caning"? WTF? You let someone's addition to information that you
    incompletely expressed bother you that much?

    FWIW I understood your explanation upthread, and referenced it in my
    later response. I don't think anyone misunderstood it.

    You've been reading and writing to this newsgroup for a while. I can
    guess that you've read my posts before. If I were doing any
    metaphorical "caning" it would have been directed to you, not to
    flawed comments. You would have no doubt that there was a "caning"
    going on.

    What you saw above was a comment on what looked like bad advice, and
    was at least information that did not include any disclaimer or
    warnings that what was recommended, er, described had significant
    dangers. It would have been irresponsible to let that information go
    out without an alert as to its red flags.

    I do recommend that you be clear when discussing options if there are
    major dangers to the options you appear to recommend. I also
    recommend that you do not take factual disagreements or corrections as
    "caning" in this newsgroup. The purpose is to get the facts straight
    and the implications explicit.

    You must expect wrong or incomplete information to trigger responses.
    If you're going to get all sensitive when people add to what you've
    said, as I did, or disagree with what you've said, then you are really
    not well suited to this environment. One of the great benefits to
    this and similar forums is that one gets one's own misunderstandings
    clarified. It's certainly happened to me any number of times.

    Grow a thicker skin lest you sacrifice the reception of truth.

    --
    Lew
    Lew, Sep 23, 2009
    #18
  19. grz01

    Qu0ll Guest

    "Lew" <> wrote in message
    news:...
    > Qu0ll wrote:
    >> Lew, I try to help where I can given that I have received so much help
    >> from
    >> this group over the years. I don't always get it right and unfortunately
    >> the caning I receive when I don't quite hit the mark tends to discourage
    >> me
    >> from making further such attempts in the future.
    >>
    >> You are absolutely correct that it was bad advice but, as I have already
    >> explained, it was more an an effort to point out a feature in Eclipse
    >> than
    >> suggest a recommended practice. I couldn't see any reason for the
    >> warning
    >> in the code presented so I wanted the OP to know that they did have the
    >> option to ignore the warning if it was resulting from a bug in the
    >> Eclipse
    >> compiler.

    >
    > "Caning"? WTF? You let someone's addition to information that you
    > incompletely expressed bother you that much?
    >
    > FWIW I understood your explanation upthread, and referenced it in my
    > later response. I don't think anyone misunderstood it.
    >
    > You've been reading and writing to this newsgroup for a while. I can
    > guess that you've read my posts before. If I were doing any
    > metaphorical "caning" it would have been directed to you, not to
    > flawed comments. You would have no doubt that there was a "caning"
    > going on.
    >
    > What you saw above was a comment on what looked like bad advice, and
    > was at least information that did not include any disclaimer or
    > warnings that what was recommended, er, described had significant
    > dangers. It would have been irresponsible to let that information go
    > out without an alert as to its red flags.
    >
    > I do recommend that you be clear when discussing options if there are
    > major dangers to the options you appear to recommend. I also
    > recommend that you do not take factual disagreements or corrections as
    > "caning" in this newsgroup. The purpose is to get the facts straight
    > and the implications explicit.
    >
    > You must expect wrong or incomplete information to trigger responses.
    > If you're going to get all sensitive when people add to what you've
    > said, as I did, or disagree with what you've said, then you are really
    > not well suited to this environment. One of the great benefits to
    > this and similar forums is that one gets one's own misunderstandings
    > clarified. It's certainly happened to me any number of times.
    >
    > Grow a thicker skin lest you sacrifice the reception of truth.


    My skin is as thick as it needs to be. I thoroughly deserved the response I
    received to my comment as it was not expressed very well. I describe it as
    a "caning" but it could have been worse. Let's leave it at that.

    BTW, I don't need you to assess or comment on my suitability for Usenet. I
    am well aware of the pros and cons of the environment.

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Sep 23, 2009
    #19
  20. grz01

    Lew Guest

    Qu0ll wrote:
    >>> You are absolutely correct that it was bad advice but, as I have already
    >>> explained, it was more an an effort to point out a feature in Eclipse
    >>> than
    >>> suggest a recommended practice. I couldn't see any reason for the
    >>> warning
    >>> in the code presented so I wanted the OP to know that they did have the
    >>> option to ignore the warning if it was resulting from a bug in the
    >>> Eclipse compiler.


    That is a very useful thing to do, and the information you gave is helpful.

    --
    Lew
    Lew, Sep 24, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Barowski
    Replies:
    12
    Views:
    2,620
    Larry Barowski
    Aug 30, 2004
  2. niceguyi
    Replies:
    0
    Views:
    445
    niceguyi
    Jan 22, 2008
  3. kathychow
    Replies:
    0
    Views:
    2,103
    kathychow
    Mar 31, 2009
  4. Seebs
    Replies:
    9
    Views:
    3,978
    Phil Carmody
    Sep 17, 2009
  5. Qu0ll
    Replies:
    3
    Views:
    280
    Daniel Pitts
    Sep 24, 2009
Loading...

Share This Page