Lecture 3
Lecture 3
Unit 1
Lecture 3
Lecture 3
• Tokens of Java:
• Variables
• Operators
• Data types
• Comments
Java Tokens
In Java, Tokens are the smallest elements of a program
that is meaningful to the compiler. They are also known
as the fundamental building blocks of the program.
Tokens can be classified as follows:
• Keywords
• Identifiers
• Constants
• Special Symbols
• Operators
• Comments
• Separators
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.
abstract assert boolean
break byte case
catch char class
const continue default
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. Identifier
names must differ in spelling and case from any
keywords.
Examples of valid identifiers:
MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
3. Constants/Literals:
Constants are also like normal variables. But the
only difference is, their values cannot be
modified by the program once they are defined.
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.
[] () {}, ; * =
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. Some of the types are-
• Arithmetic Operators
• Unary Operators
• Assignment Operator
• Relational Operators
• Logical Operators
• Ternary Operator
• Bitwise Operators
• Shift Operators
6. Comments:
In Java, Comments are the part of the program which
are ignored by the compiler while compiling the
Program. They are useful as they can be used to
describe the operation or methods in the program.
The Comments are classified as follows:
• Single Line Comments
• Multiline Comments
// This is a Single Line Comment
/*
This is a Multiline Comment
*/
7. Separators:
Separators are used to separate different parts of the
codes. It tells the compiler about completion of a
statement in the program. The most commonly and
frequently used separator in java is semicolon (;).
Datatypes in java
Datatypes in java
Operators in java
Operators in java
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 (Increment & Decrement)
• Arithmetic Operator (+,-,*,/,%)
• Relational Operator(<,>,<=,>=,==,!=)
• Assignment Operator. (=,+=,-=,*= etc.)
• Logical Operator(&&, ||)
• shift Operator (<<,>>)
• Bitwise Operator (&,|,^)
• Ternary Operator (?:)
Java Unary Operator Example: ++ and --
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Output:
10
12
12
10
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}}
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Java AND Operator Example:
Logical && and Bitwise &
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//
15*2^4=15*16=240
}}
Java Shift Operator Example: Right Shift
class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}}
Java Variable Types