Punctuators and Separators in Java

Hello Coders! In this post, we will discuss Punctuators and separators in Java. Moreover, students are often confused between these two terms and find them difficult to understand. Let us understand it together in this post.

Introduction

In Java, the program contains classes and methods. Further, the methods contain the expressions and statements required to perform a specific operation. These statements and expressions are made up of tokens. In other words, we can say that the expression and statement are a set of tokens. The tokens are the small building blocks of a Java program that are meaningful to the Java compiler. Further, these two components contain variables, constants, and operators.

What is a token in Java?

The Java compiler breaks the line of code into text (words) is called Java tokens. These are the smallest element of the Java program. The Java compiler identified these words as tokens. These tokens are separated by the delimiters. It is useful for compilers to detect errors. Remember that the delimiters are not part of the Java tokens.

Example

For example, consider the following code-

public class Demo  
{  
public static void main(String args[])  
{  
System.out.println("GeeksToCode");  
}  
}  

In the above code snippet, public, class, Demo, {, static, void, main, (, String, args, [, ], ), System, ., out, println, GeeksToCode, etc. are the Java tokens.

The Java compiler translates these tokens into Java bytecode. Finally, we execute these bytecodes inside the interpreted Java environment.

Types of Tokens: Java tokens include the following:

  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators
  5. Separators
  6. Punctuators
  7. Comments

Keywords: These are the pre-defined reserved words of any programming language. Each keyword has a special meaning. Secondly, we always write them in lower case.

Identifier: We use identifiers to name a variable, constant, function, class, and array. It usually defined by the user. It uses letters, underscores, or a dollar sign as the first character. The label is also known as a special kind of identifier that is used in the goto statement. Remember that the identifier name must be different from the reserved keywords. There are some rules to declare identifiers are:

  1. The first letter of an identifier must be a letter, underscore, or a dollar sign. It cannot start with digits but may contain digits.
  2. We cannot include whitespace in an identifier.
  3. Identifiers are case-sensitive.

Literals: In programming literal is a notation that represents a fixed value (constant) in the source code. It can be categorized as an integer literal, string literal, Boolean literal, etc. It is defined by the programmer. Once it has been defined cannot be changed. Java provides five types of literals are as follows:

  1. Integer
  2. Floating Point
  3. Character
  4. String
  5. Boolean

Operators: In programming, operators are the special symbol that tells the compiler to perform a special operation. There are eight types of operators in Java, are as follows:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Unary Operators
  5. Logical Operators
  6. Ternary Operators
  7. Bitwise Operators
  8. Shift Operators

Punctuators in Java

Every language has a unique set of special symbols that work as punctuators or separators. In java, we use puntuators to group and separate the data values written in java programs.

Punctuators are the punctuation signs that we use as special characters in Java. Some of the punctuators are: ‘? ’ (Question mark), ‘: ‘ (colon), ‘; ‘ (semicolon), ‘.’ (Dot)

The most important punctuator is the semicolon ‘ ; ‘, because we use it as the statement terminator in Java, i.e. it marks the end of any statement in the program.

Separators in Java

We use separators to separate the variable or characters in a Java Program. Separators are basically special characters.

Punctuators and separators in Java

E.g.:  Braces: ( ), { }, [ ] and Comma ‘ , ‘

We use comma to separate multiple variables under the same declaration while the following are the use of braces:

  1. Simple braces: ( ) to enclose any relational or mathematical expressions.
  2. Curly Braces: { } to enclose a group of statements under a single statement.
  3. Square Braces: [ ] to enclose the index number or cell number of a dimensional array.

Sample program for separator in java:

class separator
{
public static void main(String args[])   // uses bracket or [] separator
{                           // uses braces  or {} separator
int a=10,b=15,c;   // uses comma or , separator
c=a+b;                // uses semicolon or ; separaotor
system.out.println("addition of a and b is:"+c)     // uses  paranthesis or () separator
}
}

Output: addition of a and b is: 25

MCQs based on Punctuators and Separators

Q1. Punctuators also known as

a. Equal b. Separators c. Operands d. None of the

Answer: Separators

Q2. The structure of a program by dividing and arranging a set of codes

a. Punctuators b. Operators c. Operands d. None of these

Answer: Punctuators

Q3. <> is a punctuator or a separator?

Answer: Punctuator

Q4. ‘ , ‘ is a punctuator or separator?

Answer: Separator

Q5. ‘;’ is a punctuator or separator?

Answer: Punctuator

Homework

Q1. Can you differentiate between separators and punctuators in the following JAVA code?

class Armstrong {
  public static void main(String[] args) {

    int low = 999, high = 99999;

    for(int number = low + 1; number < high; ++number) {
      int digits = 0;
      int result = 0;
      int originalNumber = number;

      // number of digits calculation
      while (originalNumber != 0) {
        originalNumber /= 10;
        ++digits;
      }

      originalNumber = number;

      // result contains sum of nth power of its digits
      while (originalNumber != 0) {
        int remainder = originalNumber % 10;
        result += Math.pow(remainder, digits);
        originalNumber /= 10;
      }

      if (result == number)
        System.out.print(number + " ");
      }
  }
}

Q2. Find the Separators in the following code.

public class AsciiValue {

    public static void main(String[] args) {

        char ch = 'a';
        int ascii = ch;
        // You can also cast char to int
        int castAscii = (int) ch;

        System.out.println("The ASCII value of " + ch + " is: " + ascii);
        System.out.println("The ASCII value of " + ch + " is: " + castAscii);
    }
}

Conclusion

Hope you find this post helpful. We have discussed Punctuators and Separators in JAVA and how to implement it. We hope that you found it easy to understand.

Stay tuned for more posts and updates. Until then, Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *