Results 1 to 5 of 5

Thread: Vector

  1. #1
    Anandiscool is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Red face Vector

    Hi,
    I am confused with the statement:
    "A Vector can store only object"
    Vector v=new Vector();
    v.addElement(10);
    v.addElement("Java");
    this code gives output [10 Java],
    In this code, How can '10' and 'Java' be an object?
    Thank you.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Autoboxing.

    Google that.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    "Java" means java.lang.String class ref ... so it is an Object ;)

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Anandiscool
    How can '10' and 'Java' be an object?

    work with the wrapper classes of java. example for your datas

    Integer i1 = new Integer(10);
    String s1 = new String("Java");

    so i1 und s1 are objects now and you can add them to collections that need objects. see the java spec for more details.
    Last edited by j2me64; 03-28-2010 at 07:13 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    work with the wrapper classes of java. example for your datas

    Integer i1 = new Integer(10);
    String s1 = new String("Java");

    so i1 und s1 are objects now and you can add them to collections that need objects. see the java spec for more details.
    String s1= "Java"; also makes s1 a reference to an object and you can use ordinary ints for objects because of autoboxing (present from Java 1.5) as Masijade already pointed out in reply#1.

    kind regards,

    Jos

Similar Threads

  1. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  2. Vector
    By sanox in forum New To Java
    Replies: 20
    Last Post: 09-01-2009, 04:21 PM
  3. Vector
    By MuslimCoder in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2009, 03:44 PM
  4. Replies: 1
    Last Post: 04-06-2009, 06:45 AM
  5. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •