Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Casting in java && wrapper class

  1. Jul 6, 2011 #1
    casting in java....&& wrapper class

    1. The problem statement, all variables and given/known data

    well my question is quite simple.
    1..) when a vector is instantiated ,does it needs to have a particular data type like integer,employee.... like in case of array or does any object can be put into it irrespective of its name....
    Vector employee = new Vector(50,20);
    can i store a reference to integer by wrapping it in this vector

    2..) when a Integer class is stored in vector,how can i get the int value stored in it.
    a.. just by accessing the index where a wrapper class is stored .. or..
    b.. storing the accessed reference in some variable and then applying Intvalue method..?

    second question pls clarify w.r.t every other primitive wrapper class....


    2. Relevant equations

    3. The attempt at a solution
  2. jcsd
  3. Jul 6, 2011 #2
    Re: casting in java....&& wrapper class

    No, you can store any object in the vector, but you shouldn't be using the Vector class anyways, it's deprecated - use ArrayList.

    You can specify what type of object the ArrayList will store like so:

    ArrayList<Integer> al = new ArrayList<Integer>();

    That means you don't have to cast elements in the ArrayList. For example, if you hadn't specified Integer, it would assume instances of Object. So you would have to do this to get element # 0, for example:

    Integer int = (Integer)al.get(0);

    Of course, if it finds a non-Integer in there, you'll get a cast exception.

    Whereas you can't put non-Integers in there if you specified Integer. You could just get it out this way:

    Integer i = al.get(0);

    However, you can go between Integer and int types pretty transparently (autoboxing). This is also perfectly valid:

    int i = al.get(0);

    I'm glossing this over a bit, but that should give you an idea how it works.

    Let me show you a quick snippet of code that should demonstrate how it works:

    Code (Text):

    import java.util.ArrayList;

    public class AL {
        public static void main(String[] args)
            ArrayList<Integer> al = new ArrayList<Integer>();

            for (int i : al) {

            int x = al.get(1);
    Notice how I took little note of the fact that the ArrayList contains Integer objects rather than int primitive types. Other primitive types are pretty much the same.

    If you're using some kind of tutorial or whatever that uses Vector classes, I strongly suggest you find one that is even remotely up to date.

    Hope that clears things up.
  4. Jul 6, 2011 #3
    Re: casting in java....&& wrapper class

    ok.... what if i specified the data type like Employees and store a nonemployee reference like game in it..
    then can i retrieve the game object reference after casting.?.
    also i am not getting the (for) format used by you..can you please explain it...?
  5. Jul 6, 2011 #4
    Re: casting in java....&& wrapper class

    As Grep said, if you don't specify a type for the array list, you should be able to hold anything in that list. So suppose you have this (not recommended) code:

    Code (Text):
    List list = new ArrayList();
    list.add(new Game());
    list.add(new Employee());
    You should be able to say:
    Code (Text):
    Game game = (Game) list.get(0);
    Employee employee = (Employee) list.get(1);
    However, make sure you cast it correctly, else, as Grep said, you'll get an class cast exception. Meaning, if you put more than one type of object in the list, you can't do this:

    Code (Text):
    for (int i = 0; i < list.size(); i++){
         Employee employee = (Employee) list.get(0);
         ....do something with employee
    As for the for loop, that is another way of going through the loop. It's the same as the code I had written above. So instead of writing the above for loop, I could write it like this:

    Code (Text):
    for (Employee employee : list){
         ....do something with employee
    But this only work if you declare list as an Employee list.

    Code (Text):
    List<Employee> list = new ArrayList<Employee>();
    I hope that makes some kind of sense.
  6. Jul 6, 2011 #5
    Re: casting in java....&& wrapper class

    Yes, though you should think hard before doing that. It's generally a bad idea to put objects of different classes with no common base class (other than Object) into the same list. Like in that case, I don't see what Employee and Game objects might have to do with each other, and why they'd be in the same list.

    Since the base class of all objects in Java is the Object class, you can specify that the objects are of type Object. Though that's unnecessary. Just don't specify the class type and you can put anything in (and get anything back out). As I showed you:

    ArrayList al = new ArrayList();

    However, now you need to worry about what the type of each object in the list. You end up with stuff like (written directly here and not tested, just an example):
    Code (Text):

    if (object instanceof Employee) {
        Employee emp = (Employee)al.get(index);
    } else if (object instanceof Game) {
        Game game = (Game)al.get(index);
    } else {
        // Unexpected object type! Good to handle such errors, but better to make sure they
        // can't happen in the first place.
    Usually, when I get to that point, I ask myself what I did wrong.

    That's the ranged for loop. It's a clear and concise way to iterate over just about anything element by element.

    Take this piece of code, where employees is some kind of sequential list of elements:
    Code (Text):

    for (Employee emp : employees) {
    It will go through every element in employees and assign each one in turn to the emp variable (of type Employee). Every element obviously needs to be of type Employee. It's a short version of:

    Code (Text):

    for (int i = 0; i < employees.size(); ++i)
        Employee emp = employees.get(i);
    Of course, the ranged for doesn't have a loop index variable, so if you care what element you're at, you want the regular for loop.

    Does that make it clear?
  7. Jul 7, 2011 #6
    Re: casting in java....&& wrapper class

    thanks everyone...
    that was really informative
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook