Java Tokens
Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions in question. The language and assertion can be thought of as a collection of tokens, to put it another way. Tokens are the little pieces of a Java program that are significant to the Java compiler. These two parts also include variables, constants, and operators.
Java token
Java tokens are the text that is produced when the Java compiler splits a line of code. The Java program's simplest component is this. These words were recognized as tokens by the Java compiler. The delimiters divide these tokens separately.
public class TokenJava
{
public static void main(String args[])
{
System.out.println("tutorial and example");
}
}
In the above Java code public, class, TokenJAva, args {, void, static, main, (, String, [, ], ), System, ., out, println, tutorial, and, example. s
The smallest component of a program that the compiler can understand is called a token. The following categories apply to tokens:
- Keywords
- Identifiers
- Literals
- Operators
- Comments
Keywords: In a computer language, keywords are terms that are pre-defined or reserved. Each keyword in a programme is designed to carry out a particular task. As referred names for a compiler, keywords cannot be used as variable names because doing so would attempt to give the term a new meaning, which is against the rules. These keywords are supported by the Java language:
abstract | continue | for | new | switch |
assert | default | goto | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extends | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const* | float | native | super | while |
Identifiers: The names of a variable, constant, function, class, and array are given by the identifiers. Typically, the user defines it. It starts with a letter, an underscore, or a dollar symbol. The identifier name needs to be distinct from the reserved terms, keep in mind. Some guidelines for declaring identifiers are as follows:
- An identifier's first letter must be an underscore, a letter, or a dollar sign. Although it can contain digits, naming should not be started with digits.
- The identifier is not allowed to contain whitespace.
- Identifiers must match exactly, i.e., they are case sensitive.
Examples for identifiers:
name – allowed
@name – not allowed
STUDENT1 – allowed
1Student – not allowed
abc – allowed
-javaIdent – not allowed
1234 – not allowed
a123 – allowed
_5avc – allowed
$JavIdentify – allowed
Literals: Literal is a notation used in programming that denotes a fixed value (also known as a constant) in the source code. It could be a string literal, integer literal, boolean literal, etc. The programmer defines it. It cannot be modified once it has been defined. The five literal kinds that Java offers are as follows:
- Boolean – bool a = 5
- Integer – int a = 20
- Floating Point - float a = 20.3
- Character - char a = ‘s’
- String – String a = "India."
Operators: Operators are the special symbol used in programming that instructs the compiler to carry out a specific operation. Java offers a variety of operators that can be categorized based on the features they offer. In Java, there are eight different categories of operators, as follows:
- Arithmetic Operators – + , - , / , * , %
- Relational Operators – = =, ! = , < , >, < = , > =
- Bitwise Operators – & , | , ^ , ~
- Assignment Operators – = , + = , - = , * = , / = , % = , ^ =
- Logical Operators – && , | |
- Ternary Operators – (Condition) ? (Statement1) : (Statement2);
- Unary Operators – ++ , - - , !
- Shift Operators – << , >> , >>>
Comments: We can provide program details inside our Java code by using comments. The Java compiler sees these comments as tokens but doesn't handle them further. Whitespaces are treated as comments by the Java compiler. The two comment kinds that Java offers are:
- Single Line comments or line comments (It begins with a pair of forwarding slashes (//).)
- Block comment or multi-line comments (It begins with /* and continues until it founds */.)