Tokens in Java
Java tokens are smallest elements of a program which are identified by the compiler.
Tokens can be classified as follows:
- Keywords
- Identifiers
- Constants
- Special Symbols
- Operators
1. Keyword: Keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. Since keywords are referred names for a compiler, they can’t be used as variable names because by doing so, we are trying to assign a new meaning to the keyword which is not allowed.
2. Identifiers: Identifiers are used as the general terminology for naming of variables, functions and arrays. These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character.
3. Constants/Literals: Constants are also like normal variables. But, the only difference is, their values can not be modified by the program once they are defined. Constants refer to fixed values. They are also called as literals.
Constants may belong to any of the data type.
Syntax:-
final data_type variable_name;
4. Special Symbols: The following special symbols are used in Java having some special meaning and thus, cannot be used for some other purpose.
[] () {}, ; * =
Brackets[]: Opening and closing brackets are used as array element reference. 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 marks the start and end of a block of code containing more than one executable statement.
comma (, ): It is used to separate more than one statements like for separating parameters in function calls.
semi colon : It is an operator that essentially invokes something called an initialization list.
asterick (*): It is used to create pointer variable.
assignment operator: It is used to assign values.
5. Operators: Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide.
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Bitwise Operators
Separator in java
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.
Type Casting/type conversion
Converting one primitive datatype into another is known as type casting (type conversion) in Java. You can cast the primitive datatypes in two ways namely, Widening and, Narrowing.
Widening − Converting a lower datatype to a higher datatype is known as widening. In this case the casting/conversion is done automatically therefore, it is known as implicit type casting. In this case both datatypes should be compatible with each other.
Example
public class WideningExample {
public static void main(String args[]){
char ch = 'C';
int i = ch;
System.out.println(i);
}
}
Output
Integer value of the given character: 67
Narrowing − Converting a higher datatype to a lower datatype is known as narrowing. In this case the casting/conversion is not done automatically, you need to convert explicitly using the cast operator “( )” explicitly. Therefore, it is known as explicit type casting. In this case both datatypes need not be compatible with each other.
Example
import java.util.Scanner;
public class NarrowingExample {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer value: ");
int i = sc.nextInt();
char ch = (char) i;
System.out.println("Character value of the given integer: "+ch);
}
}
Output
Enter an integer value:
67
Character value of the given integer: C
0 Comments