UNIT-1 PART-2
BUILDING BLOCKS
TOKENS (Keywords, Identifier, Literals, Operators, Separators)
JAVA Keywords
=> There are 50 reserved keywords currently defined in java language.
=> These keywords, combined with the syntax of the operators and separators, from the foundation of the java language.
=> Keywords have specific meaning in java; we cannot use them as names for variables, classes, methods and so on.
JAVA Identifiers
=> Identifiers are used for class names, method names and variable names.
=> An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers or the underscore and dollar -sign character.
=> he must not begin with a number for fear that they be confused with a numeric literal.
=> Java is case-sensitive, so VALUE is a different identifier than value.
=> Some examples of valid identifiers are:
- AvgTemp, Count, an, $test, this_is_ok.
=> Invalid variable names include here;
- 2count, high-temp, Not/ok
JAVA Literals
=> Literals in java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables.
=> Java language specifies five major types of literals they are:
- Integer Literals
- Floating point Literals
- Character Literals
- String Literals
- Boolean Literals
Operators
=> An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations.
=> Operators are used in programs to manipulate data and variables.
=> Java operators can be classified into a number of related categories as below:
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Assignment operators
5. Increment and decrement operator
6. Conditional operators
7. Bitwise operators
8. Special operators
1. Arithmetic operator
=> Java provides all the basic arithmetic operators.
Operator Meaning
+ Addition or unary plus
_ Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
(ii) Real Arithmetic operator
An arithmetic operation involving only real operands is called real arithmetic.
=> A real operand may assume values either in decimal or exponential notation.
=> Modulus operator % can be applied to the floating point data as well.
(iii) Mixed-mode Arithmetic operator
=> When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression.
=> If either operand is of the real type, then the other operand is converted to real and the real arithmetic is performed. The result will be a real.
15/10.0 = 1.5
15/10 = 1
2. Relational operator
=> For comparing two quantities, and depending on their relation, we take certain decision.
=> For example, we may compare the age of two persons, or the price of two items, and so on. These comparison can be done with the help of relational operators.
Operator Meaning
< is
less than
<= is less than or equal to
> is
greater than
>= is greater than or equal to
== is equal to
!= is not equal to
3. Logical operator
=> Java has three logical operators:
Operator Meaning
&& Logical
AND
|| Logical
OR
! Logical
NO
4. Assignment operators
=> Assignment operators
are used to assign the value of an expression to a variable.
=> Java has a set of
‘shorthand’ assignment operators which are used in the form
var-name op= exp;
which is equivalent to
var-name=var-name op
(exp);
=> For the statement:
x=x+(y+1);
x+=y+1;
Statement with Statement
with
Simple
assignment operators Shorthand
operator
a=a+1 a+=1
a=a*1 a*=1
a=a*(n+1) a*=n+1
a=a/(n+1) a/=n+1
a=a%b a%=b
5. Increment and decrement operator
=> java has two increment
and decrement operators:
++ and - -
=> The operator ++ adds 1
to the operand while - - subtracts 1. both are used in the following format:
++m; or m++;
--m or m--;
Where:
++m is equivalent to m=m+1;
-- m is equivalent to m=m-1;
6. Conditional operators
=> The character pair ? : is a ternary operator available in java. This operator is used to construct conditional expressions of the form
=> The character pair ? : is a ternary operator available in java. This operator is used to construct conditional expressions of the form
Exp1
? Exp2: Exp3
=> Consider the following example:
a=10;
b=15;
x=(a>b) ? a: b;
It
is same as:
if (a>b)
x=a;
else
x=b;
7. Bitwise operators
=> Java has a distinction of supporting special operators known as bitwise operators for manipulation of data at values of bit level.
=> These operators are used for testing the bits, or shifting them to the right or left.
=> Bitwise operators may not be applied to float or double.
=> Java has a distinction of supporting special operators known as bitwise operators for manipulation of data at values of bit level.
=> These operators are used for testing the bits, or shifting them to the right or left.
=> Bitwise operators may not be applied to float or double.


