+ 1
What can I do with the symbol : ?
I don't know what's the function of the symbol : in a Java program. Can someone explain me?
3 Answers
+ 4
It is also used in ternary operator;
<condition> ? <true-block> : <false-block>
Which is a shorthand for an `if-else` block written such as:
if(<condition>)
<true-block>
else
<false-block>
+ 6
The only place I can think of now is that it is used in the enhanced for loop for generally traversing through the elements of an array.
Eg-
int [ ] arr = {1,2,3,4,5};
for (int a : arr)
System.out.println(a);
+ 2
// and method reference ::
import java.util.stream.IntStream;
public class Program { public static void main(String[] args) {
IntStream.range(1, 6).forEach(
System.out :: println
);
}}