Home » Java programming language

Java Vector toString() Method with Example

Vector Class toString() method: Here, we are going to learn about the toString() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 20, 2020

Vector Class toString() method

  • toString() method is available in java.util package.
  • toString() method is used to show how to represent each element of this Vector as a string.
  • toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • toString() method does not throw an exception at the time of string representation.

Syntax:

    public String toString();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is String, it returns string representation of this Vector.

Example:

// Java program to demonstrate the example 
// of String toString() method of Vector 

import java.util.Vector;

public class ToStringOfVector {
 public static void main(String[] args) {
  // Instantiates a Vector object with
  // initial capacity of "20"
  Vector < String > v = new Vector < String > (10);

  // By using add() method is to add the
  // elements in this v
  v.add("C");
  v.add("C++");
  v.add("JAVA");

  // Display Initial Vector Capacity
  System.out.println("v: " + v);

  // By using toString() method is used
  // for string representation of this vector
  System.out.println("v.toString(): " + v.toString());
 }
}

Output

v: [C, C++, JAVA]
v.toString(): [C, C++, JAVA]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.