Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

warning: [unchecked] unchecked cast

Posted on 2013-11-14
8
Medium Priority
?
546 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

916 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