0% found this document useful (0 votes)
6 views5 pages

(Rev) Tokens in Java

Uploaded by

veernarwani27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

(Rev) Tokens in Java

Uploaded by

veernarwani27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ganesh Yashwanth Kumar

Tokens (Lexical Units): In Java programming token is the smallest unit of a program. In other
words, we can say each individual character used in Java program is termed as a token. It is the
fundamental unit of a Java.

Java mainly supports following types of tokens:

a. Keywords
b. Identifiers
c. Literals
d. Punctuators
e. Operators

a. Keywords: These are pre-defined or pre-compiled words available with the software. By the
help of keywords we can perform various tasks in our program very easily without writing their
definitions. The definitions of the keywords are available in Java library class files.

i. All the keywords are always in lower-case.


ii. Execution speed of keywords is very fast because they are pre-compiled.
iii. We cannot change the definitions of the keywords.
iv. Also known as reversed words

b. Identifiers: In Java program, we need identifiers to set name or represent variables, objects,
classes, methods and arrays. By specifying identifiers, we can guide compiler to locate the
element in memory or to identify the element for its execution.

Rules for naming a variable:

i. It may contain alphabets, digits, _ and $.


ii. It must start with an alphabet including _ and $
iii. Blank spaces are not allowed.
iv. Keywords are not allowed.
v. It is purely case-sensitive.
vi. The name must be short and meaningful.

Some important Notes:

 Variables: These are the elements available in program to store values. By declaring a variable,
we can occupy a memory space to store data of different types. The value of a variable is
changeable (It means we can change the value of a variable during execution of a program).

 In Java, to declare a variable we need data types. E.g. int, float, double, char etc.

 Constant: These are the items available in a program to hold a fixed value. It means, we cannot
change the value of a constant. (In Java final keyword is used to declare a constant.)

 Declaration: It is a process to introduce a variable to the compiler without any initial value.

 Assignment: It is a process to store or assign a value to a declared value.

 Initialization: It is the combination of declaration and assignment i.e. to give a value to a


variable during the time of its declaration.

We can set initialization under two categories:


Ganesh Yashwanth Kumar
o Static Initialization: It is a process to initialize a variable during its declaration.
o Dynamic initialization: It is a process to initialize a variable during execution of program logic.
It means to assign a value to a variable by solving any arithmetic or logical function during
execution of program is known as dynamic initialization.

c. Literals: They are the constants in Java programs mainly available in expressions with a direct
fixed value (Also Known as Nameless Constants).

We can set literals under the following categories:

i. Integer literals: The numbers which are represented without decimal points.
e.g.1, 2, 3, 4…..

ii. Real literals: The numbers with decimal parts.


e.g. 1.5, 2.8, 7.98 etc.

iii. Character literals: Under this category we can use all characters.
e.g. a-z, A-Z, 0-9, symbols etc.

iv. String literals: It is a set of alpha-numeric characters and symbols enclosed within a pair of
double quotes.
e.g. “apple12”, “abcdefg”, “!A”, “”(null string-length zero)

v. Boolean literals: They can be represented by true of false.

vi. Null literals: This is a special purpose literal which is represented as ‘\0’.

d. Punctuators: These are the symbols that we need in our source code to perform any specific
action or to represent any specific element. Some punctuators are ?, ; , . etc.

 Semicolon (;) is used to terminate a statement so it is called statement terminator.

 Dot (.) is used to separate a function from object. It is also used to separate a class name and a
method. e.g. Math.pow(3,2);

 Separator is a part of punctuator and these special characters are used in Java programs to
perform special and very useful actions. These are also known and delimiters.
o () – Braces – It is used to write an expression.
o {} – Curly braces – It is used to enclose a group of statements.

(Special NOTE: Statements written inside curly braces are called compound statements)

o [] – Square bracket– It is used to declare an array.


o , - Comma – In Java program, comma is used as a variable separator.

e. OPERATORS/OPCODES:
These are symbolic instructions/special, which are used in Java programs to perform different
types of arithmetical and logical operations on data items (operands)
We can divide all the Java operators into the following groups:
1) Arithmetic operators
2) Relational operators
Ganesh Yashwanth Kumar
3) Logical operators
4) Assignment operator
5) Increment and Decrement operators
6) Unary and Binary operators
7) Ternary or conditional operator
8) Bitwise operators
9) Compound assignment operators
10) Special operators (Misc. Operators)

1. Arithmetic operators:
Arithmetic operators are used in mathematical expressions in the same way that they are used in
algebra.
e.g. * , / , % , + , -

2. Relational operators:
Relational operators are used to compare values of the operands. These operators return a
Boolean value (true or false) based on the state of the operands.
e.g. > , < , == , ! = , > = , < =

3. Logical operators:
These operators are used to combine conditional expressions to set complex condition(s). It
returns a Boolean value.
e.g. AND – && ( “&” is known as ampersand )
OR – || ( “|” is known as pipe )
NOT – ! ( “!” is known as exclamatory mark)

Truth table/Logic:
AND

Expression 1 Expression 2 Result


F F F
F T F
T F F
T T T
Logical AND returns true only if both the original expressions are true.
OR

Expression 1 Expression 2 Result


F F F
F T T
T F T
T T T
Ganesh Yashwanth Kumar
Logical OR returns true if either of its original expressions are true.
NOT

Expression Result
T F
F T
Logical NOT negates or reverses the true value of the expression.

4. Assignment operator:
This operator is used to assign value of the right side variable to the left side variable
(operand).e.g. = (equal to sign)

5. Increment and Decrement operators:


Increment(++) / Decrement(--) operator is used to increase/decrease value of the specified
operand by one(1) respectively.
* Prefix Increment/ Decrement (Change before action)
* Postfix Increment/ Decrement (Change after action)

6. Unary and binary operators:

Unary operators demand only one operand to perform action.( e.g. ++ , -- , ! )


Binary operators demand two operands to perform action. ( e.g. +,-, * ,> , < ,= , == etc.)

7. Ternary/conditional operator:
This operator requires three operands and it is used to store a value in a variable depending upon
a condition(s). It is a replacement of if-else statement of Java. It is also called conditional
assignment operator.
Syntax:
Variable= Expression1 ? Expression2 : Expression3

Condition(s) (value if true) (value if false)

8. Bitwise operators:
• These operators perform operations on bit level of the operands. These are binary
operators.
• These operators use byte, short, int and long type operands.
e.g. & (AND) , ^ (XOR) , | (OR) , ~ (Compliment)
<< (Left Shift), >> (Right Shift)
Ganesh Yashwanth Kumar

9. Compound assignment operators:


These operators are used to perform arithmetic and assignment operations on the specified
operand(s) simultaneously. (Also known as Java shorthand)
e.g. += , -= , *= , %= , /=
int a=200;
a = a+ 50 ; a+=50; 250
a= a – 75 ; a -= 75; 125
a= a * 3 ; a *= 3; 600
a= a/30 ; a/=30 ; 6
a= a% 30; a%=30; 20

10. Special operators (Misc. Operators):


• Java provides few special operators to perform some useful operations.
e.g. new operator
NOTE: new operator is used to create a class object or an array. It allocates memory and returns
the reference of memory at run time.

VERY IMPORTANT NOTES:


Operator Precedence & Associativity:
Operator precedence determines the order in which expressions are evaluated. (Precedence can
be changed by placing parentheses around the expressions that need to be evaluated first.)

Operator associativity determines the order of operation when an expression contains operators
of same precedence.

******* OUTPUT QUESTIONS BASED ON OPERATORS GIVEN IN: OPERATORS OUTPUT QUESTIONS ******

You might also like