Lecture-02-JAVA
Lecture-02-JAVA
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:
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.
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:
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:
import java.io.*;
class Ternary
{ // variable declaration
}
}
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)
~ 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:
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.