Is there any keyword or methods in Java that is similar to Go's select statement, that is only used for selection of communication channels?
Asked
Active
Viewed 577 times
4
-
You can use `switch` statement in Java – Vishal Kawade Nov 11 '17 at 09:29
-
I was wondering if there are other options besides switch? – nadia Nov 11 '17 at 09:30
-
This is other solution [Alternative to Switch Case in Java](https://stackoverflow.com/questions/1425659/alternative-to-switch-case-in-java) – Vishal Kawade Nov 11 '17 at 09:35
2 Answers
3
The other answers misunderstand the question (or they don't know what select is in Go).
Go does have a switch and a select statement, where the switch is the rough equivalent of Java's switch, and the select statement is similar to switch but only to select on communication operations (the cases are comm. ops.: a send statement or a receive operation).
Java does not have an equivalent to Go's select. You can achieve the same but in a more verbose way, and it's not built into the language like Go's select.
See related questions:
Concurrency Java example of Go
icza
- 389,944
- 63
- 907
- 827
0
The similar syntax in Java is switch. For detail, you could refer to https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html.
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
default: monthString = "Invalid month";
break;
}
Ben Cheng
- 769
- 10
- 25