Module-1
Tokens in Java
• Tokens are the various elements in the java program that are identified by Java compiler. A
token is the smallest individual element (unit) in a program that is meaningful to the compiler.
• In simple words, a java program is a group of tokens, comments, and white spaces. For example,
• consider the below java statements:
final double p = 3.14 // A constant.
x = a + b; // An expression.
v = [Link](10, 1); // An inbuilt java function.
• Java language contains five types of tokens that are as follows:
1. Keywords
2. Identifiers
3. Literals
4. Special Symbols
5. Operators
Keywords
• Keywords are reserved words in programming languages. These are used to indicate
predefined terms and actions in a program.
• Keywords are not allowed to be used as names of variables or objects.
• Keywords are case-sensitive and always written in lowercase.
• Java has the following keywords:
Identifiers
• A method name, class name, variable name, or label is an identifier in Java. The user
typically defines these.
• The identifier names cannot be the same as any reserved keyword.
• Let's see an example to understand identifiers:
public class Test {
public static void main(String[]args)
{
int num = 10;
}
}
Identifiers present in the above program are:
Test: The name of the class.
main: The name of a method.
String: A predefined class name.
args: A variable name.
num: A variable name.
Cont..
Rules for naming identifiers.
• The characters allowed are [A-Z], [a-z], [0-9], _ and $.
• Identifiers are case-sensitive.
• That is, “java” is not the same as “JAVA”.
• Identifier names should not start with a digit.
• For example, “007IamFine” is an invalid identifier.
• Whitespace is not allowed inside an identifier.
• Keywords can’t be used as an identifier.
Literals
• Literals represent fixed values in a source code. These are similar to standard variables with the difference
that these are constant.
• These can be classified as an integer literal, a string literal, a boolean etc.
• The user defines these mainly to define constants.
• Syntax to define literals:
final data_type variable_name;
• There are five types of literals in Java:
• Integer
• Floating Point
• Boolean
• Character
• String
Example-1:
public class JavaIntegerLiterals {
public static void main (String[]args){
//initialize variables with integer literals
int decimalNum = 25;
int hexaNum = 0xa5;
int binaryNum = 0b1101;
int octalNum = 0172;
//print out the values of the literal
[Link]("Decimal Integer: " + decimalNum);
[Link]("Octal Integer: " + octalNum);
[Link]("Hexadecimal Integer: " + hexaNum);
[Link]("Binary Integer: " + binaryNum);
}
}
Output
Decimal Integer: 25
Octal Integer: 122
Hexadecimal Integer: 165
Binary Integer: 13
Example-2:
public class JavaBooleanLiterals {
public static void main(String[] args) {
boolean isTrue = true;
boolean isFalse =false;
[Link]("The boolean value of isTrue: " + isTrue);
[Link]("The boolean value of isFalse: " + isFalse);
}
}
Output
The boolean value of isTrue: true
The boolean value of isFalse: false
Example-3:
public class JavaFloatLiterals {
public static void main(String[] args) {
double piDoubleValue = 3.1415926535;
float piFloatValue = 3.1415926535F;
double piScientific = 3.1415926535e0;
[Link]("Pi to ten decimal places: " + piDoubleValue);
[Link]("A rounded value of Pi: " + piFloatValue);
[Link]("Pi from a scientific notation: " + piScientific);
}
}
Output
Pi to ten decimal places: 3.1415926535
A rounded value of Pi: 3.1415927
Pi from a scientific notation: 3.1415926535
Example-4:
public class JavaCharLiterals {
public static void main(String[] args) {
char myFirstChar = 'a';
char mySecondChar = 'b';
char myThirdChar = 'c';
char plusInUnicode = '\u002b';
[Link]("First three letters of the alphabet: "
+ myFirstChar + mySecondChar + myThirdChar);
//Escape sequence as character literals
[Link]("The same three characters on separate lines: "
+ "\n" + myFirstChar
+ "\n\t" + mySecondChar
+ "\n" +myThirdChar);
//Unicode character literal
[Link]("Letter a added to letter b: " +
myFirstChar + plusInUnicode + mySecondChar); Output
} First three letters of the alphabet: abc
} The same three characters on separate lines:
a
b
c
Letter a added to letter b: a+b
Operators in Java
• Operator in Java is a symbol that is used to perform operations.
• For example: +, -, *, / etc.
• There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative * / %
additive + -
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ? :
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Java Unary Operator
• The Java unary operators require only one operand. Unary operators are used to perform
various operations i.e.:
• incrementing/decrementing a value by one
• negating an expression
• inverting the value of a Boolean
public class OperatorExample{
public static void main(String args[]){
int x=10;
[Link](x++);//10 (11) Output:
[Link](++x);//12 10
12
[Link](x--);//12 (11) 12
[Link](--x);//10 10
}
}
Cont..
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
[Link](a++ + ++a);
[Link](b++ + b++);
}
}
Output:
22
21
Cont..
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
[Link](~a);
[Link](~b);
[Link](!c);
[Link](!d);
}
}
Output:
2-11
9
false
true
Java Arithmetic Operator
• Java arithmetic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
[Link](a+b);//15
[Link](a-b);//5
[Link](a*b);//50
[Link](a/b);//2
[Link](a%b);//0
[Link](10*10/5+3-1*4/2); // 21
}
}