+ 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?

12th Dec 2019, 10:55 PM
Eduardo Cavalcante
Eduardo Cavalcante - avatar
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>
12th Dec 2019, 11:17 PM
Ipang
+ 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);
12th Dec 2019, 11:02 PM
Avinesh
Avinesh - avatar
+ 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 ); }}
13th Dec 2019, 12:50 AM
zemiak