5 Replies Latest reply: Apr 1, 2007 2:02 PM by 800282 RSS

    warning: [unchecked] unchecked cast.

    807606
      Hello!

      I have a problem with this code.
      I get:
      warning: [unchecked] unchecked cast.

      How should i do the cast?
      Or is it something else that i have done wrong?
      Socket socket = new Socket("localhost", this.port);  
      LinkedList<String> times = new LinkedList<String>();
      ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                          
      try
      {
          times = (LinkedList<String>)ois.readObject();
      }
      catch(ClassNotFoundException cnfe)
      {
      
      }
        • 1. Re: warning: [unchecked] unchecked cast.
          807606
          warning: [unchecked] unchecked cast.
          It's a warning. Your code will run fine (assuming
          the list is truly a list of String)
          times = (LinkedList<String>)ois.readObject();
          The compiler is saying that it is not sure (at compile time)
          that the return value is always a list of String.

          You can either ignore the warning,
          or you can change it to "times = (LinkedList) ois.readObject();"
          (that is, you give up your claim that, not only will the result
          be a List, but the result is specifically a List that only contains Strings)
          • 2. Re: warning: [unchecked] unchecked cast.
            807606
            Or, add @SuppressWarnings("unchecked") in front of that method,
            to tell the compiler that you know there are some typecasts in that method
            which cannot be checked at compile time.
            @SuppressWarnings("unchecked")
            public void someMethod() {
               ...
               ...
               times = (LinkedList<String>)ois.readObject();
               ...
            }
            • 3. Re: warning: [unchecked] unchecked cast.
              800282
              That's because it is not 100% sure (at compile time) that the object is really of a type LinkedList<String>, that's why you received a warning (note that this is just a warning: not an exception or error). You cannot do anything about is. You could suppress the warning like this:
                  @SuppressWarnings("unchecked")
                  void yourMethod() {
                      try {
                          Socket socket = new Socket("localhost", 666);  
                          LinkedList<String> times = new LinkedList<String>();
                          ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                          times = (LinkedList<String>)ois.readObject();
                          // ...
                      } catch(Exception cnfe) {
                          cnfe.printStackTrace();
                      }
                  }
              Good luck.
              • 4. Re: warning: [unchecked] unchecked cast.
                800282
                Miss McDonnell, were you just reading my mind?
                ; )
                • 5. Re: warning: [unchecked] unchecked cast.
                  807606
                  No sir. :)