Separators in Java


Separators are one of the building blocks of a Java program that helps us to differentiate the regions of the block of code and arrange groups of codes or statements. They help us in defining scopes of particular variables and methods.

Have you ever missed a bracket while defining a method or semicolon at the end of a statement? We all do this silly mistake every time we code. In this article, we are going to explore the importance of these separators and what would be the consequences if we neglect them.

Seperators available in Java

Being a programmer we often come across these symbols but we never put our attention to them. We all use them unconsciously without knowing their exact names and uses. If we ignore their importance, it will adversely affect the readability and also the structure of our java program. It can lead to many syntactical errors that can be difficult to debug.

The following table contains the most used separators in any Java program −

Symbols

Names

Uses

;

Semi Colon

We use it to separate conditions in for loop. It instructs the compiler that a particular statement ends here so that compiler can terminate the execution.

,

Comma

We can declare multiple variables in a single line of same datatype. It is used while defining two or more arguments and parameters in method call.

{ }

Curly Braces

It can be used to initialize arrays and matrices. It contains executable statements of methods, classes and control statements. It also determines scopes of variables and methods.

[ ]

Square Braces

It is used to declare size of the array and during dereferencing of array values.

( )

Parenthesis

It is used to enclose parameters and arguments during method definition and call. It contains all the conditional expressions of different control statements.

:

Colon

It is used inside for each loops.

.

Period sign

It is used to call static methods. During importing different packages it acts as a distinguisher between packages and their sub packages.

Example 1

The following example illustrates the use of separators in a simple java program.

First, we declare and initialize an array and using ‘Arrays.sort()’ method we will sort them in ascending order.

import java.util.*;  
public class Main {
   public static void main (String[] args) {
      int aray[] = { 9, 3, 56, 0, -2, -6, 2, 1, 80 };
      System.out.print("The given unsorted array: ");
      for (int print : aray) {
         System.out.print(print + " ");
      } 
      Arrays.sort(aray);
      System.out.println();
      System.out.print("The newly sorted array: ");
      for (int print : aray) {
         System.out.print(print + " ");
      }
      System.out.println();
   }
}

Output

The given unsorted array: 9 3 56 0 -2 -6 2 1 80 
The newly sorted array: -6 -2 0 1 2 3 9 56 80 

In the above code, Periods are used during importing ‘java.util’ package, while calling the ‘Arrays.sort()’ method and printing message on screen. All the executable statements and elements of array are contained inside Curly Braces. The parenthesis encloses conditional statements of for each loop. Semi Colons are used at the end of each statement.

Conclusion

Our top priority should be utilizing these separators correctly to well structure our program which can further enhance readability and make our program understandable with ease. If we miss any separators then we will encounter a syntax error at compile time. However, there are some exceptions too. In this article, we have discussed the most commonly used separators and also seen their uses in a Java program.

Updated on: 12-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements