What Is The Use Of String and Vector in Java

In this article, you will learn about the concept of String and Vector in Java programming language in detail with an example. Here, we will also discuss the methods of vector and strings with their description.

What is a String in Java?

A string is a group of different characters, such as the four characters in the string “Java.” A string is an immutable object in Java programming, which implies that the string cannot be modified after creation.

There are two ways in which we can make or create Strings:
1. Using New Keyword
2. Using String Literal
Now, you will learn how to create string using new keyword and string literal.

New Keyword

As we know, the compiler only built one object and made both literals point to the string when we assigned the same string object to two separate literals.

The statements in the following block show how to create a string using new keyword:
String str1 = new String(“MetaTutorials”);
String str2 = new String(“MetaTutorials”);
In this instance, the compiler would produce two distinct objects with identical strings in the memory.

String Literal

In Java, string literals are created by specifying the value in the double quotes.

The following statements show how to create a string using string literals:

String str1 = “MetaTutorials”;
String str2 = “Java Tutorial”;

Methods of String Class

In Java, there is a string class which gives various methods to perform operations on strings. Few of them are mentioned below:

This method of String class in java adds the string at the end of another string.


This method compares two string.


This method shows the length of the specified string.

This method replaces the existing string with the new string.


This method of String class splits the specified string into array of multiple strings.


This method converts the upper case characters of string into lower case characters.


This method converts the lower case characters of string into upper case characters.


This String class method checks whether the specified string is empty or not.

Example of a String:

public class String_Example{
public static void main(String args[]){

String S1 = “MetaTutorials”; //Here, we creare the new string (S1) with the help of string literal.
char arrS2[]={‘J’,’A’,’V’,’A’};
String S2 = new String(arrS2); // Here, we convert the character array to the string S2.

String S3 = new String(“Strings In Java “); //Here, we create the new string (S3) with the help of new keyword.

//Now, we are going to display all the three strings S1, S2, and S3 with the help of java println function.

Output of above code:
Strings In Java

Vector in Java

The Vector is same as the dynamic array in Java which grow its size when elements are added and shrink its size when elements are removed from it. In Java language, vectors are found in java.util package and allows you to implement the List interface which provides all the Methods of List interface to use.

Its storage increase in chunks. Each vector makes an effort to optimize storage management by incrementing the capacity. The size of capacity increments when the new element is added. Thus, the capacity is always at least as large as the vector size. It is typically larger. The capacity of a vector can be increased by an application before the specific number of values are inserted. This minimizes the amount of incremental reallocation.
The iterator and list iterator methods of this class return fail-fast iterators that will issue a Concurrent Modification Exception when the vector is structurally updated at any point after the initialization of iterator.

Syntax of Vector in Java

public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
In this syntax, 
• E indicates an element.
• List interfaces are implemented, and AbstractList is extended.
• It supports the Iterable <E>collection<E>List<E>, Cloneable, Serializable, and RandomAccess interfaces.
• Stack is the direct subclass.

Methods of Vector


extends(E> c)

This method adds each element from the provided collection.

addAll(int index,

Collection<? extends E> c)

This method adds every element from the given collection to the vector at the given point.

addElement(E obj)

This method increases the size of the vector by one and add element to the vector.


This method returns the vector’s current capacity.


Deletes every component from this vector.


A copy of this vector is given back.

contains(Object o)

This function returns true if the requested element exist in this vector.

containsAll(Collection<?> c)

If this vector includes every element in the given collection, it will return true.

copyInto(Object[] anArray)

This method copies this vector’s elements into the designated array.

element(int index)

This method reveals the component located at the given index.


This List interface method returns a list of the elements that help in creating this vector.

ensureCapacity(int minCapacity)

This method ensures that the vector has enough space to store the minimum number of elements.

equals(Object o)

This method verifies that the given Object and Vector are same or not.


This method shows the first element of this vector, or the item at index 0.

get(int index)

This method shows the element located in this vector at the given point.


This method gives the value of the hash code for this vector.

indexOf(Object o, int index)

This method shows the index of the first occurrence of the given value in vector. If the given element is not found, then the method returns -1.

insertElementAt(E obj, int index)

This method inserts the given object as an element at the given index in the specified vector.


This method determines if this vector is empty.


This method returns an iterator that iterates across the entries in the correct order.


This method returns the vector’s last element.

lastIndexOf(Object o)

This function shows the index of the last occurrence of the passed element in the given vector. If the passed element is not found, then the method returns -1.

lastIndexOf(Object o, int index)

This method shows the index of the last occurrence of the given value in vector. If the given element is not found, then the method returns -1.


This method provides an iterator list of the entries in this list as a result (in proper sequence).

listIterator(int index)

This method returns a list iterator from the specified index value.

remove(int index)

This method removes the specified element from the given location in vector.

remove(Object o)

This method removes the passed element from the first instance of the vector. If the element is not exist in the vector, it remains unchanged.

removeAll(Collection<?> c)

This method removes all the elements of those indexes which are passed in this method.


This method removes all the elements and sets the size of vector as zero automatically.

removeElement(Object obj)

This method removes the first occurrence from the vector.

removeElementAt(int index)

The component at the provided index is deleted.

removeIf(Predicate<? super E> filter)

Removes all of the collection’s components that meet the specified criteria.

removeRange(int fromIndex,

int toIndex)

This method removes all the entries between the given index values. The first index and last index value are also included.

replaceAll(UnaryOperator<E> operator)

This list’s elements are replaced with the outcome of applying the operator to each element.

retainAll(Collection<?> c)

This method only keeps the components of this vector present in the given collection.

set(int index, E element)

This method replaces the element at the specified index with the new element.

setElementAt(E obj, int index)

This method replaces the object at the specified index with the new object.

setSize(int newSize)

This method adjusts this vector’s size.


This method returns the vector’s total number of components.

sort(Comparator<? super E> c)

This method sorts the list in an order that the given comparator has induced.


This method creates a splitter over the items in the list that is late-binding and fail-fast.

subList(int fromIndex, int toIndex)

This method shows all the elements between the given index values. The first index and last index value are also included.


This method returns an array of the vector elements.

Example of Vector in Java

import java.util.*;
// This is the main class
class MetaTutorals
    // Main driver method
public static void main(String[] args)
int n = 6; // Here, n is the size of the Vector
Vector <Integer> v = new Vector <Integer>(n); // Here, we declare the vector with initial size ‘n.’
for (int i = 1; i <= n; i++)
v.add(i); // Here, we Append the new element at the end of the vector.
// Now, we are going to display the elements
v.remove(2); // Remove element at index 2
System.out.println(v); // Here, we display the vector after detection
for (int i = 0; i < v.size(); i++)  // iterating over vector elements using for loop
System.out.print(v.get(i) + ” “); // Print the elements one by one
Output of above code:
[1, 2, 3, 4, 5, 6]
[1, 2, 4, 5, 6]
1 2 4 5 6

Leave a Comment