-3

Decided to edit this question.

(String) refers to casting.

Ex:

protected String methodA(Vector X) {
    return (String) X.get(0);
}

In this case the get method returns an object, and for the methodA to return a String there needs to be a cast to String. And the cast is done as demonstrated.

Hugo Silva
  • 27
  • 5

3 Answers3

0

To my understanding, the (String) is a cast to string. Without being into the details, I suppose that the return type of get is Object, even if the contents are of String type. The cast is necessary to match the return type of method.

Codor
  • 17,447
  • 9
  • 29
  • 56
0

This method returns first element in the Vector cast to String.

fabian
  • 80,457
  • 12
  • 86
  • 114
Ivan Zelenskyy
  • 659
  • 9
  • 26
0

If X.get(0) returns an Object but the underlying is String then another way besides (String) X.get(0) is to use X.get(0).toString(). That might be more familiar.

stefana
  • 2,606
  • 3
  • 29
  • 47