Casting in java && wrapper class

In summary, you can store any object in a vector, but you should use the ArrayList class. When you access an element in the vector, you can get the element's index (or simply use the get() method), or you can store the accessed reference in a variable and then use the int value method on that variable.
  • #1
the_kool_guy
37
0
casting in java...&& wrapper class

Homework Statement



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...
example.
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...

thanks

Homework Equations


The Attempt at a Solution

 
Physics news on Phys.org
  • #2


the_kool_guy said:

Homework Statement



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...
example.
Vector employee = new Vector(50,20);
can i store a reference to integer by wrapping it in this vector
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.

the_kool_guy said:
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...

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

Code:
import java.util.ArrayList;

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

        for (int i : al) {
            System.out.println(i);
        }

        int x = al.get(1);
        System.out.println(x);
    }
};

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.
 
  • #3


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...?
 
  • #4


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:
List list = new ArrayList();
list.add(new Game());
list.add(new Employee());

You should be able to say:
Code:
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:
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:
for (Employee employee : list){
     ...do something with employee
}

But this only work if you declare list as an Employee list.

Code:
List<Employee> list = new ArrayList<Employee>();

I hope that makes some kind of sense.
 
  • #5


the_kool_guy said:
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.?.

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:
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.

the_kool_guy said:
also i am not getting the (for) format used by you..can you please explain it...?

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:
for (Employee emp : employees) {
    System.out.println(emp);
}

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:
for (int i = 0; i < employees.size(); ++i)
{
    Employee emp = employees.get(i);
    System.out.println(emp);
}

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?
 
  • #6


thanks everyone...
that was really informative
 

FAQ: Casting in java && wrapper class

1. What is casting in Java?

Casting in Java is the process of converting a value of one data type to another. This can be done explicitly using the cast operator or implicitly by the compiler.

2. Why do we need to use casting in Java?

We use casting in Java to convert data types in situations where the target data type is different from the source data type, for example, when performing arithmetic operations or passing arguments to methods that expect a specific data type.

3. What is the difference between upcasting and downcasting in Java?

Upcasting is the process of converting a derived class to its base class, while downcasting is the process of converting a base class to its derived class. Upcasting can be done implicitly, while downcasting requires explicit casting using the cast operator.

4. What are wrapper classes in Java?

Wrapper classes in Java are classes that wrap primitive data types to provide additional functionality and enable them to be used as objects. They also provide methods for converting between primitive and object types.

5. How do we convert a primitive data type to its corresponding wrapper class in Java?

We can convert a primitive data type to its corresponding wrapper class by using the valueOf() method, which is available in all wrapper classes. For example, to convert an int to an Integer object, we can use Integer.valueOf(int).

Similar threads

Replies
12
Views
2K
Replies
4
Views
1K
Replies
7
Views
2K
Replies
1
Views
1K
Replies
2
Views
3K
Replies
2
Views
1K
Replies
7
Views
2K
Replies
15
Views
2K
Back
Top