-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,235
  • 9
  • 32
  • 52
0

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

fabian
  • 72,938
  • 12
  • 79
  • 109
Ivan Zelenskyy
  • 640
  • 7
  • 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,466
  • 3
  • 26
  • 46