Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

warning: [unchecked] unchecked cast

Posted on 2013-11-14
8
Medium Priority
?
538 Views
Last Modified: 2013-11-14
Hi,

I get this compile error when I try to compile my class with -Xlint:unchecked

warning: [unchecked] unchecked cast
		E[] newData = (E[]) new Object[capacity];
		                    ^
  required: E[]
  found:    Object[]
  where E is a type-variable:
    E extends Object declared in class ArrayList

Open in new window

If I compile without -Xlint, I get a warning, but no error, and the program works.

But it makes me nervous. Is there some way to 'fix' this to make -Xlint happy?

Thanks.
0
Comment
Question by:Kyle Hamilton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:dpearson
ID: 39648768
Have you tried:

E[] newData = new E[capacity] ;

Doug
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39648792
so, putting this in front of the constructor was supposed to eliminate this message, but it had no effect on the message I'm getting in the console:

@SuppressWarnings("unchecked")

Open in new window


I still get this message:

Note: ArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Open in new window


oh well, apparently this is not a big deal. If anyone has any input, I would appreciate it.

Thanks.
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39648801
Hi Doug,

according to the book,
E[] newData = new E[capacity] ;

Open in new window

would give ma an "invalid generic type" and won't compile.
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 28

Expert Comment

by:dpearson
ID: 39648851
Ah - didn't know it was generic.

That's certainly harder.  If you have access to the class itself you can do:

E createObject(Class<E> clazz)
{
      return clazz.newInstance();
}

To create a typesafe instance of the class.  (If we can get that working, then need to figure out how to extend it to the whole array).

But to do that you need to pass in E.class at the time of the call to the generic method/class - can't look it up later on inside the method because E will have had its type erased.

Is that an option?

Doug
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39648934
Ok, so I have this in my class:

private E createObject(Class<E> clazz) throws Exception{
		return clazz.newInstance();
	}

Open in new window


and now this:

E newData = new E[capacity];

Open in new window


now the error is:

error: generic array creation
		E newData = new E[capacity];

Open in new window


Would it be helpful if I posted the whole class code?
0
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 39648973
OK - it looks like the one you really want is
java.lang.reflect.Array.newInstance()

which allows you to create a new array of objects of a specific type, like E.class.

More docs on it here:
http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html#newInstance(java.lang.Class, int)

And a bit of tutorial on the whole thing here:
http://docs.oracle.com/javase/tutorial/reflect/special/arrayInstance.html

Although I'm not sure this will ultimately clear the warning - as it seems
java.lang.reflect.Array.newInstance()
returns type Object
instead of type E[]
...

which looks wrong to me :)

Doug
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 39649010
Thank you. much appreciated.

I better get outta this rabbit hole, for now.

Once I pass Level 1, I'll revisit this :) I think for now, I'm gonna live with the warning.

Cheers,
Koza
0
 
LVL 28

Expert Comment

by:dpearson
ID: 39649168
Haha - fair enough :)

Doug
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question