"unchecked or unsafe operations." - when this type of exception thrown?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    "unchecked or unsafe operations." - when this type of exception thrown?

    [CODE=java]
    package test;

    import java.util.Array List;

    public class MyArrayList{
    public static void main(String args[])
    {
    ArrayList al = new ArrayList();
    al.add("Jerald" );
    al.add("PJerald ");
    al.add(1);
    }
    }
    [/CODE]

    I compiled my code as follows. It scold some thing that i dont know..

    [$ ~]$ javac MyArrayList.jav a -d .; java test.MyArrayLis t
    Note: MyArrayList.jav a uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by pjerald
    [CODE=java]
    package test;

    import java.util.Array List;

    public class MyArrayList{
    public static void main(String args[])
    {
    ArrayList al = new ArrayList();
    al.add("Jerald" );
    al.add("PJerald ");
    al.add(1);
    }
    }
    [/CODE]

    I compiled my code as follows. It scold some thing that i dont know..

    [$ ~]$ javac MyArrayList.jav a -d .; java test.MyArrayLis t
    Note: MyArrayList.jav a uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    Did you recompile with -Xlint unchecked for the details?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      @OP: when Sun decided to introduce 'generics' they also decided that some
      objects should be part of it: e.g. their Collection framework. This simply implies
      that you don't just have an Arraylist anymore, you have an ArrayList that contains
      a certain type, even if the type is just an Object. If you don't specify a type for
      the objects to be stored in the ArrayList, all the compiler can do is whine at you.

      Just because of backwards compatibility it has to accept that you want to store
      just an object (or a derivative thereof) in your ArrayList, but it whines at you.

      That's what you saw and you have two options:

      1) ignore it and go on programming 'old style' which is semantically valid;
      2) agnowledge the fact that you're just storing T type objects in an ArrayList.

      For 2) you have to specify the type T using that funny <...> notation and you
      have to fill in the dots as in:

      [code=java]
      ArrayList<Strin g> myList= new ArrayList<Strin g>();
      [/code]

      It's all about static type checking where the compiler is boss ...

      kind regards,

      Jos

      Comment

      • pjerald
        New Member
        • Oct 2007
        • 77

        #4
        Originally posted by JosAH
        @OP: when Sun decided to introduce 'generics' they also decided that some
        objects should be part of it: e.g. their Collection framework. This simply implies
        that you don't just have an Arraylist anymore, you have an ArrayList that contains
        a certain type, even if the type is just an Object. If you don't specify a type for
        the objects to be stored in the ArrayList, all the compiler can do is whine at you.

        Just because of backwards compatibility it has to accept that you want to store
        just an object (or a derivative thereof) in your ArrayList, but it whines at you.

        That's what you saw and you have two options:

        1) ignore it and go on programming 'old style' which is semantically valid;
        2) agnowledge the fact that you're just storing T type objects in an ArrayList.

        For 2) you have to specify the type T using that funny <...> notation and you
        have to fill in the dots as in:

        [code=java]
        ArrayList<Strin g> myList= new ArrayList<Strin g>();
        [/code]

        It's all about static type checking where the compiler is boss ...

        kind regards,

        Jos
        Oh I feel sorry jos and r035198x, since i signed out and left yesterday, but you people took pain and answered. Dear r035198x, then i compiled my code with -Xlint. It showed me three warnings something like this,

        [$ ~]$ javac -Xlint MyArrayList.jav a
        MyArrayList.jav a:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
        al.add("Jerald" );
        ^
        MyArrayList.jav a:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
        al.add("PJerald ");
        ^
        MyArrayList.jav a:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
        al.add(1);
        ^
        3 warnings

        Here with this
        1) what is actually unchecked ?
        2) should i specify explicitly the type of object which i going to add in the ArrayList( Am i wright jos?) ?

        Thanks,
        P.Jerald

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by pjerald
          Oh I feel sorry jos and r035198x, since i signed out and left yesterday, but you people took pain and answered. Dear r035198x, then i compiled my code with -Xlint. It showed me three warnings something like this,

          [$ ~]$ javac -Xlint MyArrayList.jav a
          MyArrayList.jav a:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
          al.add("Jerald" );
          ^
          MyArrayList.jav a:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
          al.add("PJerald ");
          ^
          MyArrayList.jav a:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
          al.add(1);
          ^
          3 warnings

          Here with this
          1) what is actually unchecked ?
          2) should i specify explicitly the type of object which i going to add in the ArrayList( Am i wright jos?) ?

          Thanks,
          P.Jerald
          Now read Jos' response above.

          Comment

          • pjerald
            New Member
            • Oct 2007
            • 77

            #6
            Originally posted by r035198x
            Now read Jos' response above.
            i think my question number two is the answer!. ie., before we use ArrayList, We must specify what type of object we are going to store. am i right ?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by pjerald
              i think my question number two is the answer!. ie., before we use ArrayList, We must specify what type of object we are going to store. am i right ?
              Yep .

              Comment

              • pjerald
                New Member
                • Oct 2007
                • 77

                #8
                Originally posted by r035198x
                Yep .
                k
                thanks, r035198x.
                regards,
                P.Jerald

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Note that you have to rethink the statement at line 11 of your op: a1.add(1).
                  An int is definitely not a String and it won't be 'autoboxed' to a String either.

                  kind regards,

                  Jos

                  Comment

                  Working...