0% found this document useful (0 votes)
18 views

Lecture-02-JAVA

The document provides an overview of tokens in programming, specifically in Java, categorizing them into keywords, identifiers, constants/literals, operators, and separators. It explains the significance of each category, detailing keywords as reserved words, identifiers as user-defined names, and various types of operators including arithmetic, relational, logical, and bitwise operators. Additionally, it covers the ternary operator and shift operators, illustrating their usage with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture-02-JAVA

The document provides an overview of tokens in programming, specifically in Java, categorizing them into keywords, identifiers, constants/literals, operators, and separators. It explains the significance of each category, detailing keywords as reserved words, identifiers as user-defined names, and various types of operators including arithmetic, relational, logical, and bitwise operators. Additionally, it covers the ternary operator and shift operators, illustrating their usage with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

LECTURE-02

Tokens
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:

1.Keywords
2.Identifiers
3.Constants/Literals
4.Operators
5.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. Java language supports the following keywords:
abstract assert boolean
break byte case
catch char class
const continue default
do double else
enum exports extends
final finally float
for goto if
implements import instanceof
int interface long
module native new
open opens package
private protected provides
public requires return
short static strictfp
super switch synchronized
this throw throws
to transient transitive
try uses void
volatile while with
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. You cannot use keywords as
identifiers; they are reserved for special use. Once declared, you can use the identifier in later program statements to
refer to the associated value. A special kind of identifier, called a statement label, can be used in goto statements.
Examples of valid identifiers: Examples of invalid identifiers:
MyVariable
My Variable // contains a space
MYVARIABLE
123geeks // Begins with a digit
myvariable
x a+c // plus sign is not an alphanumeric character
i
variable-2 // hyphen is not an alphanumeric character
x1
sum_&_difference // ampersand is not an alphanumeric character
i1
_myvariable
$myvariable
sum_of_array
geeks123
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. Constants refer to fixed values. They are also called as
literals. Constants may belong to any of the data type. Syntax:

final data_type variable_name;

import java.io.*;
class GFG { public static void main (String[] args)
{
// Here final keyword is used
// to define the constant PI
final double PI = 3.14;
// Use double instead of int
// Example usage of PI
System.out.println("The value of PI is: " + PI);
}
} Output
The value of PI is:
3.14
4. 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
Arithmetic Operators

These operators involve the mathematical operators that can be used to perform various simple
or advanced arithmetic operations on the primitive data types referred to as the operands.
These operators consist of various unary and binary operators that can be applied on a single or
two operands. Let’s look at the various operators that Java has to provide under the arithmetic
operators.
Unary Operators in Java
In Java, the unary operator is an operator that can be used only with an operand. It is used to
represent the positive or negative value, increment/decrement the value by 1,
and complement a Boolean value. In this section, we will discuss the unary operator in Java
with examples and also understand the differences between i++ and i+=1.
The following table describes the short description of the unary operators.

Operator Name Symbol Description Example Equivalent Expression

Unary Plus + It is used to +a a


represent
the positive value
.
Unary Minus - It is used to -a -
represent
the negative valu
e.
Increment ++ It increments the ++a a=a+1
Operator value of a variable or
by 1. a++

Decrement -- It decrements the --a a=a-1


Operator value of a variable or
by 1. a--

Logical ! It inverts the !true -


Complement value of a boolean
Operator variable.

Bitwise Complement(~)
Java Assignment Operators
Assignment operators are used to assign values to
variables.
In the example below, we use the assignment operator
(=) to assign the value 10 to a variable called x:

Example A list of all assignment operators


int x = 10;
Operator Example Same As
The addition assignment operator (+=) adds a value to a variable:
= x=5 x=5
Example += x += 3 x=x+3
int x = 10; x += 5;
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
3. Java Relational Operators
Relational operators are used to check the relationship between two operands. For example,

Operator Description Example

== Is Equal To 3 == 5 returns false

!= Not Equal To 3 != 5 returns true

> Greater Than 3 > 5 returns false

< Less Than 3 < 5 returns true

>= Greater Than or Equal To 3 >= 5 returns false

<= Less Than or Equal To 3 <= 5 returns true


4. Java Logical Operators
Logical operators are used to check whether an expression is true or false. They are used in decision making.

Operator Example Meaning

true only if both expression1


&& (Logical AND) expression1 && expression2
and expression2 are true

true if either expression1 or


|| (Logical OR) expression1 || expression2
expression2 is true

! (Logical NOT) !expression true if expression is false and


vice versa
Ternary Operator in Java

Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner
replacement for the if-then-else statement and is used a lot in Java programming. We can use the
ternary operator in place of if-else conditions or even switch conditions using nested ternary
operators. Although it follows the same algorithm as of if-else statement, the conditional operator
takes less space and helps to write the if-else statements in the shortest way possible.
Example 1:
Below is the implementation of the Ternary Operator:

// Java program to find largest among two

// numbers using ternary operator

import java.io.*;

class Ternary

{ public static void main(String[] args)

{ // variable declaration

int n1 = 5, n2 = 10, max;

System.out.println("First num: " + n1);

System.out.println("Second num: " + n2);

// Largest among n1 and n2

max = (n1 > n2) ? n1 : n2;

// Print the largest number


System.out.println("Maximum is = " + max);

}
}

Output
First num: 5
Second num: 10
Maximum is = 10
Bitwise Operators
Now, let’s look at each one of the bitwise operators in Java:
1. Bitwise OR (|)
This operator is a binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e., if either of the bits is 1, it gives 1,
else it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise OR Operation of 5 and 7


0101
| 0111
________
0111 = 7 (In decimal)

2. Bitwise AND (&)


This operator is a binary operator, denoted by ‘&.’ It returns bit by bit AND of input values, i.e., if both bits are 1, it gives 1, else it
shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise AND Operation of 5 and 7


0101
& 0111
________
0101 = 5 (In decimal)
3. Bitwise XOR (^)
This operator is a binary operator, denoted by ‘^.’ It returns bit by bit XOR of input values, i.e., if corresponding bits are different, it gives
1, else it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7


0101
^ 0111
________
0010 = 2 (In decimal)

4. Bitwise Complement (~)


This operator is a unary operator, denoted by ‘~.’ It returns the one’s complement representation of the input value, i.e., with all
bits inverted, which means it makes every 0 to 1, and every 1 to 0.
Example:
a = 5 = 0101 (In Binary)

Bitwise Complement Operation of 5 in java (8 bits)

~ 00000101
________
11111010 = -6 (In decimal)
Shift Operators in Java.
By shifting the bits of its first operand right or left, a shift operator performs bit manipulation on
data. The shift operators available in the Java programming language are listed below. The shift
operator is a java operator that is used to shift bit patterns right or left.
Types of Shift Operators in Java:

Name of operator Sign Description

Signed Left Shift << The left shift operator moves all bits by a given
number of bits to the left.

Signed Right Shift >> The right shift operator moves all bits by a given
number of bits to the right.

It is the same as the signed right shift, but the


Unsigned Right Shift >>> vacant leftmost position is filled with 0 instead of
the sign bit.

You might also like