Home » Java programming language

Vector Class in Java

In this article, we are going to learn about Vector class of Java. Here, we will explain about it with the help of example.
Submitted by Jyoti Singh, on February 09, 2018

Vector is a collection which holds heterogeneous Objects that are it can hold objects of different class type and then each object is type casted to its original class. Vector is synchronized that is multiple threads can work on a vector simultaneously which makes vector slow.

    Vector V = new Vector ();
    V.add(new Student('100','ramesh'));
    V.add(new Company('101','Airtel'));

Here, Vector V holds two objects of different classes Student and Object.

We can also add integer, float or string type values in vector as object. These values are not needed to typecast.

Advantages of Vector

  1. Vectors can hold Objects of different classes.
  2. Vectors are synchronous.

Disadvantage of Vector

Vector is Slow

Let’s understand vector more clearly using the below example:

package logicProgramming;
import java.util.Vector;

//class representing the Student 
class Student
	//data members of class	
	private int rollno;
	private String name;
	private String schoolCode;

	public Student(int rollno,String name,String schoolcode)
	//putStudent() to print the values of the student Object
	public void putStudent()
		System.out.println("RollNo :"+this.rollno+"\nName :"+this.rollno+"\nSchoolCode :"+this.schoolCode);	 

//class representing the employee
class CompanyEmployee
	//data memebers of the class
	public int id;
	public String name;
	public long salary;
	public CompanyEmployee(int id,String name,long salary)
	//putEmployee() to print the values of the employee object
	public void putEmployee()
		System.out.println("Id :"+this.id+"\nName :"+this.name+"\nSalary :"+this.salary);	 

//main class 
public class ExVector {
	public static void main(String[] args) {	
		Vector V=new Vector(); //Vector class to hold the objects
		V.add(new CompanyEmployee(100,"Harendra Chekkur",34000)); // adding CompanyEmployee Object to Vector 
		V.add(new Student(10,"Madhav Singh","CB2025"));  // adding Student Object to Vector
		V.add(new Integer(70));    // adding Integer as Object to Vector
		V.add(new String("Testing Vectors")); //// adding String as an Object to Vector
		V.add(new Float(57.4)); //// adding employee as Object to Vector

		//iterating the vector to print the Objects 
		for(Object O:V)
			/* as Vector holds hetrogeneous data objects thus we have to cast the object to it's type
			in order to do this we are using getName() function which gets the name of the class of the given object 
			and compares it with the given class , if it's matches than typecast the object to that class */
			else if(O.getClass().getName().equals("logicProgramming.Student"))
			// if no match is found that is we will simply print th Object 


Id :100
Name :Harendra Chekkur
Salary :34000

RollNo :10
Name :10
SchoolCode :CB2025


Testing Vectors


Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.