• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8472
  • Last Modified:

Type safety unchecked cast warning

What would be the proper way to write the following so that i don't get the type safety unchecked cast or should be parameterized errors.

public int mymethod(Object o1, Object o2) {
    Vector v1 = (Vector) o1;
    Vector v2 = (Vector) o2;

    // do stuff
}

I have also tried this but got the type safety unchecked cast error:
Vector <Object>v1 = (Vector<Object>) o1;
Vector <Object>v2 = (Vector<Object>) o2;
0
tbboyett
Asked:
tbboyett
  • 2
2 Solutions
 
objectsCommented:
why isn't it

public int mymethod(Vector o1, Vector o2) {
    Vector v1 = o1;
    Vector v2 = o2;
0
 
ksivananthCommented:
try this,

public int mymethod(Object o1, Object o2) {
   if( ( o1 instanceof Vector ) && ( o2 instnceof Vector ) ){
      Vector v1 = (Vector) o1;
      Vector v2 = (Vector) o2;
    // do stuff
   }

}
0
 
tbboyettAuthor Commented:
Objects that's a very good question and I believe I will change that.  However, I will still need to convert the objects to Vectors in either case (before or after calling the method).

ksivananth, i'll give that a shot and get back to you.
0
 
tbboyettAuthor Commented:
well there doesn't seem to be any solution to this so if i use
@SuppressWarnings("unchecked") above the class then the warnings don't show.

I will leave this open a bit longer to see if you guys know of a better way to handle this.  
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now