Results 1 to 5 of 5
  1. #1
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    55
    Rep Power
    0

    Default Why are these types incompatible?

    when I uncomment the line <T1=StringList.get(2);> I get an incompatible types error. Expected String, found object.
    However, if I run the code as posted below and put a watch on the expression <StringList.get(2);> I get type string.
    What am I missing?




    Java Code:
    package testapp;
    import java.util.Vector;
    public class TestApp 
    {
            public static void main(String[] args) 
        {
            Vector StringList=new Vector<String>();        
            StringList.add("first line");
            StringList.add("Second Line");
            StringList.add("Third Line");
            
            
            int x=0,y=1,change=0, len=0;
            String T1;
            //T1=StringList.get(2);
           
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Why are these types incompatible?

    Because you have not declared your Vector instance properly. It is presently declared as a raw type. It should be one of the following:

    Java Code:
    Vector<String> StringList = new Vector<String>();
      // or
    Vector<String> StringList = new Vector<>(); // JDK 1.7+
    Also, by convention, classes should start with a capital letter but methods and fields or variables should start with lower case.

    Regards,
    Jim
    Last edited by jim829; 10-05-2013 at 03:47 AM. Reason: YAT
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    55
    Rep Power
    0

    Default Re: Why are these types incompatible?

    That's got it, thanks.

    For my edification: Why does the vector take the strings, but not recognize the data type it is storing? I would have looked for such an error to be flagged at the declaration line.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Why are these types incompatible?

    Because a String is an Object so you can add the string to a raw type which holds Objects. But an Object is
    not necessarily a String so when you get it and try to assign it to a String, you get a complaint.

    Note: In your first example, you could have done the following:
    Java Code:
      T1=(String) StringList.get(2);
    Then you would not have received the compile time error. However, if you cast it wrong. Say you did this:
    Java Code:
    int a = (int)StringList.get(2);
    You would get a class cast exception during runtime when the statement was encountered. This is why it is so important
    to use generic types. It catches incompatible types at compile time as opposed to run time.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Why are these types incompatible?

    Quote Originally Posted by ras_oscar View Post
    That's got it, thanks.

    For my edification: Why does the vector take the strings, but not recognize the data type it is storing? I would have looked for such an error to be flagged at the declaration line.
    It would be flagging a warning. Something about using raw types.
    Generics was a later add on to Java, so they didn't want to prevent earlier code (without generics) from being compiled.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. incompatible types
    By mephistochen in forum New To Java
    Replies: 14
    Last Post: 05-20-2013, 11:40 AM
  2. Incompatible types?
    By jacjacjac in forum New To Java
    Replies: 1
    Last Post: 10-13-2012, 09:01 PM
  3. incompatible types?
    By slider57 in forum New To Java
    Replies: 3
    Last Post: 09-26-2012, 12:55 PM
  4. Incompatible types
    By Yokomoko in forum New To Java
    Replies: 10
    Last Post: 07-10-2012, 03:49 AM
  5. incompatible types!!
    By sneha343 in forum New To Java
    Replies: 7
    Last Post: 11-30-2011, 11:49 PM

Posting Permissions

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