I declared the following variable with the Optional<string> datatype which I had to do because of an Interface implementation
public Optional<String> value;
Now I need to write the below line of code which gives datatype mismatch error
value = input.readString();
Is there any way where I can type cast input.readString(); to datatype Optional<string>
You need to assign it as follows:
value = Optional.of(input.readString());You do not cast to or from Optional. It is an entirely different type which has a generic type parameter.