Table of content

What are Tokens in java?

A token is the smallest element of a program that is meaningful to the compiler. Java supports 5 types of tokens which are:

  1. Keywords
  2. Identifiers
  3. Constants
  4. Operators
  5. Special Symbols

Keywords

Keywords are predefined or reserved words that have special meaning to the Java compiler. Each keyword is assigned a special task or function and cannot be changed by the user.

You cannot use keywords as variables or identifiers as they are already reserved. A keyword should always be written in lowercase as Java is a case-sensitive language.

Some of the keywords are listed below -

boolean, byte, break, class, case, catch, continue, static, throw, new, switch, this, finally, return, for, int, void, try, short, const, etc.

Identifiers are the user-defined names of variables, methods, classes, arrays. Once you assign an identifier in the Java program, you can use it to refer to the value associated with that identifier in later statements. Following rules must be followed while naming an identifier -

  • Identifiers must begin with a letter, dollar sign, or underscore.
  • Identifiers in Java are case sensitive i.e. arr and Arr will be considered as two different identifiers
  • Java Identifiers can be of any length.
  • Identifier names cannot contain white spaces.
  • Any identifier name must not begin with a digit but can contain digits within.
  • Most importantly, keywords can’t be used as identifiers in Java.
//some valid identifiers
name, Name, myArray, price1, $value, sum_of_num, _amount, b
//some invalid identifiers
my name       //(contains white space)
123val          //(begins with a digit)
amount-val   //(hyphen is not an alphanumeric character)
a+b              //(plus sign is not an alphanumeric character)
sum&value   //(ampersand is not an alphanumeric character)

Literals/Constants

Literals or constants in Java are similar to normal variables but their values cannot be changed once assigned. They have fixed values. Constants may belong to any of the data type.

int val1 = 11;     // Int literal 
float val2 = 13.17;     // Float literal 
char ch = 'R' // char literal 
String name = "Riya"; // String literal 
boolean value = true; // Boolean literal 

Operators

Operators are used to perform some specific mathematical or non-mathematical operations on one or more operands. Some operators are-

Arithmetic operators -

They are used to perform simple arithmetic operations on two or more operands -

  • Multiplication (*)
  • Division (/)
  • Modulo (%)
  • Addition (+)
  • Subtraction (-)
Unary Operators -

Unary operators need only one operand. They are used to increment, decrement or negate a value.

  • Unary minus is used for negating the values.
  • Unary plus indicates the positive value (numbers are positive without this, however). It performs an automatic conversion to int when the type of its operand is a byte, char, or short.
  • The increment operator (++) is used for incrementing the value by 1. There are two types of increment operators.
    • Post-Increment: Value is first used for computing the result and then incremented.
    • Pre-Increment: Value is incremented first and then the result is computed.
  • The decrement operator (--) is used for decrementing the value by 1. There are two types of decrement operators.
    • Post-decrement: Value is first used for computing the result and then decremented.
    • Pre-Decrement: Value is decremented first and then the result is computed.
  • Logical not operator (!) is used for inverting or giving the opposite of a boolean value.
Assignment Operator -

The assignment operator (=) is used to assign a value to any variable. General format of assignment operator is, variable_name= value;

public static void main(String[] args) {
  int a = 5; // "=" is an assignment operator
  System.out.println(a);
  a += 2; //"+=" is a shorthand assignment operator(a=a+2)
  System.out.println(a);
  a -= 1; //"-=" is a shorthand assignment operator(a=a-1)
  System.out.println(a);
}

Output-

5
7
6
Relational Operators -

These operators are used to check for relations like equality, greater than, less than. They return boolean results after the comparison. Some relational operators are-

  • ==, Equal to returns true if the left-hand side is equal to the right-hand side.
  • !=, Not Equal to returns true if the left-hand side is not equal to the right-hand side.
  • <, less than returns true if the left-hand side is less than right-hand side.
  • <=, less than or equal to returns true if the left-hand side is less than or equal to the right-hand side.
  • >, Greater than returns true if the left-hand side is greater than the right-hand side.
  • >=, Greater than or equal to returns true if the left-hand side is greater than or equal to the right-hand side.
Logical Operators -

Logical operators are-

  • logical AND (&&) which returns true when both conditions are true
  • logical OR (||) which returns true if at least one condition is true.

Ternary operators -

The ternary operator is a shorthand version of the if-else statement. It has three operands and hence the name ternary.

syntax-

condition ?( true_statement : false_statement);

The above statement means that if the condition evaluates to true, then execute the statements after the '?' else execute the statements after the ':'

Bitwise Operators -

These operators are used to perform manipulation of individual bits of a number.

  • &, Bitwise AND operator returns bit by bit AND of input values.
  • |, Bitwise OR operator returns bit by bit OR of input values.
  • ^, Bitwise XOR operator returns bit by bit XOR of input values.
  • ~, Bitwise Complement Operator is a unary operator which returns the one’s complement representation of the input value, i.e. with all bits inversed.
Shift Operators -

Shift operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. They can be used when we have to multiply or divide a number by two.

syntax-

number shift_operator number_of_places_to_shift;
int number = 2;
 // 2 bit left shift operation 
 int result = number << 2;
 System.out.println(result);    // prints 8

Special symbols in java are a few characters that have a special meaning known to the Java compiler.

Below are some special symbols along with their meanings-

  • Brackets[] : Opening and closing brackets are used as array element references. These indicate single and multidimensional subscripts.
  • Parentheses(): These special symbols are used to indicate function calls and function parameters.
  • Braces{} : These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.
  • comma (, ) : It is used to separate more than one statement like for separating parameters in function calls.
  • semicolon (;) : It is an operator that essentially invokes something called an initialization list.
  • asterisk (*) : It is used to create a pointer variable.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions