In this page let's discuss what is a separator in java and how many types of separators available in java. So let's get started.
A separator is a symbol that is used to separate a group of code from one another is called as separators in java.
In java, there are few characters that are used as a separator. The most commonly used separator is a semicolon. As we have seen it is used to terminate the statement.
Separators help define the structure of a program. The separators used in HelloWorld are parentheses, ( ), braces, { }, the period, ., and the semicolon, ;.
There are six types of separators used in java. They are:
1) Parentheses ()
Used to enclose parameters in the method definition and invocation. Also used for defining the expression in control statements.
2) Braces {}
Used to define a block of code for classes and methods. Also used to contain the values of arrays.
3) Brackets []
Used to declare an array type and also used for dereferencing array values.
4) Semicolon ;
It is used to separate or terminate the statement.
5) Comma ,
Used to separate consecutive identifiers (or) Variable declarations. Also used to chain statements together inside a for a statement.
6) Period .
Used to separate package names from sub-packages and classes. Also used to separate a variable or method from a reference variable.
class Separator
{ //uses braces {} separator
public static void main(String args[]) //uses bracket [] separator
{
int x = 2, y = 5; // uses comma , separator
int z = x*y; //uses semicolon ; separaotor
System.out.println(z); // uses paranthesis () separator
}
}
Output:
10