aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes autoboxing in vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "autoboxing in vectors" Watch "autoboxing in vectors" New topic
Author

autoboxing in vectors

rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi,

I have a doubt regarding autoboxing.
When we try to add an primitive to vectors doesn't autoboxing automatically converts the primitive to wrapper class?
like is this not a valid statement:
2) Vector v=new Vector();
v.addElement(99);

What are rules of autoboxing.When it comes into play?
Thanks in advance.
Bhanurekha Chintagunta
Greenhorn

Joined: Jul 28, 2008
Posts: 4
Vector take any Object if we don't specify the type like Vector<object_type> . all the primitives that are added will converted to wrapper classes. Check your have compiler if the version has autoboxing?

import java.util.*;
public class D {

class sub {

sub() {
System.out.println("created a sub");
}

public String toString() {
return "Object::sub";
}
}

D() {
Vector v = new Vector();
v.addElement(99);
v.addElement("some string");
v.addElement(new sub());
for (Object i : v) {
System.out.println("Value:: " + i + "\t Class:: " + i.getClass().getName());
}
}

public static void main(String[] args) {
D sub = new D();
}
}

the above code prints

Value:: 99 Class:: java.lang.Integer
Value:: some string Class:: java.lang.String
Value:: Object::sub Class:: D$su


Njoy
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
it applies to all the Collection (concrete) (sub-)classes maps,sets list etc


SCJP 6 83%
Working on SCWCD/OCPJWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: autoboxing in vectors
 
Similar Threads
cloning array of vectors
What is the best way to work with a large amount of data?
Implementing MultiDimensional Vectors
can you have an array of vectors
Sorting vectors... a nightmare?