Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Casting Confusion

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Location
    California
    Posts
    9
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Casting Confusion

    Here is a simplified case of my problem. In the real world the Object is a two-dimensional array with dissimilar object types so I can't just make it an array of ArrayList.

    import java.util.ArrayList;
     
    public class Test {
     
    	Object o = new ArrayList<String>();
     
    	public Test() {
    		ArrayList<String> l = (ArrayList<String>)o;
    	}
     
    	public static void main(String[] main) {
    		new Test();
    	}
    }

    The compiler (J2SE 1.7.0-b147) complains:


    Test.java:8: warning: [unchecked] unchecked cast
    ArrayList<String> l = (ArrayList<String>)o;
    ^
    required: ArrayList<String>
    found: Object
    1 warning


    How can I fix this warning?
    Your recommendations will be appreciated.

    p.s. The caret in the warning actually points to the object reference at the end of the line.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Casting Confusion

    You can use instanceof to check the type.

    if(o instanceof ArrayList<String>)
    {
        ArrayList<String> l = (ArrayList<String>) o;
    }

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Location
    California
    Posts
    9
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Casting Confusion

    Thanks helloworld922 but that doesn't solve my problem. The instanceof operator is evaluated at runtime and my problem is a compiler warning. What I need is the correct cast syntax.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Casting Confusion

    Hmm... interesting. I thought that would get rid of the error, but doing some testing it seems that's not the case. Yes, it is a runtime check but I seem to remember there being some "code structure" checks Java did for potential problems like this... guess this isn't one of them.

    Strictly speaking this should be "safe" in this case because no one can access o before this cast, and it is guaranteed here to be initialized to an ArrayList of Strings. I don't know if your actual code is, so the check would probably still be helpful.

    You can also add the @SuppressWarnings("unchecked") annotation to this to get the compiler to stop complaining, or change your code.

    More info: java - Type safety: Unchecked cast - Stack Overflow

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Casting Confusion

    I get the impression that you're using parallel arrays to store data rather than creating a class to organize the data in an OOP way. Parallel arrays are to be avoided whenever possible in favor of a more OOP approach.

    Your 2D array of dissimilar types could be modified to be an array (or ArrayList) of a single type with the single type containing the dissimilar types as fields:
    public class MyDissimilarData
    {
        // instance variables
        Type1 data1;
        Type2 data2;
     
        // constructor
        public MyDissimilarData( Type1 data1, Type2 data2 )
        {
            // etc . . . 
        }
     
        //  etc . . . . 
    }

Similar Threads

  1. AVL tree confusion, syntax confusion, just general confusion really...
    By platterofhotfish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2013, 06:04 AM
  2. Package confusion
    By caesius in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 21st, 2011, 11:08 PM
  3. help 2d array confusion
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 29th, 2010, 04:30 PM
  4. ArrayList confusion
    By Stormin in forum Collections and Generics
    Replies: 7
    Last Post: August 13th, 2010, 04:58 PM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM