0

I have found this code:

Iterator iterator = set.iterator();
while(iterator.hasNext(){
  String element = (String) iterator.next();
}

What do round brackets in (String) mean?

fgh
  • 169
  • 1
  • 3
  • 15
  • 9
    Does this answer your question? [Casting variables in Java](https://stackoverflow.com/questions/5289393/casting-variables-in-java) – Jordan Feb 04 '20 at 15:36
  • It tells the compiler that object return from iterator.next() will be of type String object, also called typecasting. However in case method returns a different object then class cast exception will be throw at the runtime. – AK DevCraft Feb 04 '20 at 15:40
  • If that code were updated to use generics properly, the casting would be unnecessary. – GriffeyDog Feb 04 '20 at 15:40
  • Yes, **Jordan**, thank you! – fgh Feb 04 '20 at 15:52

1 Answers1

1

It's type conversion (casting).

https://en.wikipedia.org/wiki/Type_conversion

Object returned from iterator.next() is casted to String type.