Professional Documents
Culture Documents
Chapter 1
Chapter 1
11
INTRODUCTION TO JAVA
1.1 Advantages of Java
1.2 Features of Java
1.3 Data types, variables and array
1.4 Operators
1.5 Overview of control statements
1.6 Input and output in Java
1.4 Operators
Operator (Definition): An Operator is a one type of symbol that instruct
computer to perform certain operations on the operands.
Operators are always associated with operands. Based on the operands
operators can be classified into following three categories:
(1)Unary Operator:
Operator having only one operand is known as Unary operator.
For Example: -a
(2)Binary Operator:
Operator having two operands is known as Binary Operator.
For Example: +, *, / etc…
(3)Ternary Operator:
Operator having three operands is known as Ternary Operator.
For Example: ? :
Based on the type of the operation that is performed by the operators it can
be classified into following seven categories:
(1)Arithmetic Operator:
Arithmetic Operators are used to perform arithmetic calculations on its
operands.
Following are the list of Arithmetic Operators:
(1) Unary Plus (+):
It multiplies its operand by +1.
It is Unary Operator.
Example:
int a = 5;
System.out.println (“+a:”+ (+a));
It display +a: +5
(2) Unary Minus (-):
It multiplies its operand by -1.
It is Unary Operator.
Example:
int a = 5;
System.out.println (“-a:”+ (-a));
It display -a: -5
(3) Binary Plus (+):
It will add the value of operand to its right to the value of
operand to its left.
It is Binary Operator.
Example:
int a=5,b=6;
System.out.println (“a + b:”+ (a + b));
It display a + b: 11
(4) Binary Minus (-):
It will subtract the value of operand to its right from the value
of operand to its left.
It is Binary Operator.
Example:
int a=5,b=6;
System.out.println (“a - b:”+ (a - b));
It display a - b: -1
(5) Multiplication (*):
It will multiply the value of operand to its right with the value of
operand to its left.
It is Binary Operator.
Example:
int a=5,b=6;
System.out.println (“a * b:”+ (a * b));
It display a * b: 30
(6) Division (/):
It will divide the value of operand to its left by the value of
operand to its right.
It is Binary Operator.
If both the operands are of type integer then it will ignore the
portion after the decimal point of the result. If any of the
operand is of type floating point then it display the result with
decimal portion.
Example:
int a=5,b=6;
System.out.println (“a / b:”+ (a / b));
It display a / b: 0
int a=5;
float b=6;
System.out.println (“a / b:”+ (a / b));
It display a / b: 0.8333333
(7) Modulo (%):
It will divide the value of operand to its left by the value of
operand to its right and return the remainder of the division.
It is Binary Operator.
It can be applied to both integer as well as floating point
numbers.
Example:
int a=5,b=6;
System.out.println (“a % b:”+ (a % b));
It display a % b: 5
int a=5;
float b=6;
System.out.println (“a % b:”+ (a % b));
It display a % b: 5.0
Example:
public class ArithmeticDemo1
{
public static void main(String[] args)
{
int a = 5, b = 6;
System.out.println("+a :"+ (+a));
System.out.println("-a :"+ (-a));
System.out.println("a + b :"+ (a+b));
System.out.println("a - b :"+ (a-b));
System.out.println("a * b :"+ (a*b));
System.out.println("a / b :"+ (a/b));
System.out.println("a % b :"+ (a%b));
}
}
Output:
(2)Relational Operator:
Relational Operators are used to determine relation between its two
operands.
Normally they are used to test condition in the program. If the
condition is true then it returns true otherwise it returns false.
Following are the list of Relational Operators:
(1) Equal To (==):
It compares it’s to operands for equality.
If both operands are equal then it returns true otherwise it
returns false.
Example:
int a = 5, b = 6
System.out.println ((a==b));
It returns false.
(2) Not Equal To (! =):
It compares it’s to operands for equality.
If both operands are not equal then it returns true otherwise it
returns false.
Example:
int a = 5, b = 6
System.out.println ((a!=b));
It returns true.
(3) Less than (<):
It compares it’s operands to test weather the value of operand
to its left is less than the value of operand to its right.
If the value of operand to its left is less than the value of
operand to its right then it returns true otherwise it returns
false.
Example:
int a = 5, b = 6
System.out.println ((a<b));
It returns true.
(4) Greater than (>):
It compares it’s operands to test weather the value of operand
to its left is greater than the value of operand to its right.
If the value of operand to its left is greater than the value of
operand to its right then it returns true otherwise it returns
false.
Example:
int a = 5, b = 6
System.out.println ((a>b));
It returns false.
(5) Less than or Equal To (<=):
It compares it’s operands to test weather the value of operand
to its left is less or equal to the value of operand to its right.
If the value of operand to its left is less than or equal to the
value of operand to its right then it returns true otherwise it
returns false.
Example:
int a = 5, b = 6
System.out.println ((a<=b));
It returns true.
(6) Greater than or Equal To (>=):
It compares it’s operands to test weather the value of operand
to its left is greater than or equal to the value of operand to its
right.
If the value of operand to its left is greater than or equal to the
value of operand to its right then it returns true otherwise it
returns false.
Example:
int a = 5, b = 6
System.out.println ((a>=b));
It returns false.
Example:
public class RelationalDemo1
{
public static void main(String[] args)
{
int a =5, b=6;
System.out.println("a equal to b:" + (a==b));
System.out.println("a not equal to b:" + (a!=b));
System.out.println("a less than b:" + (a<b));
System.out.println("a greater than b:" + (a>b));
System.out.println("a less than or equal to b:" + (a<=b));
System.out.println("a greater than or equal to b:" + (a>=b));
}
}
Output:
(3)Logical Operator:
Logical Operators are operates on Boolean values. You can use Logical
Operator to combine two Boolean operands to represent a single
Boolean expression.
Logical Operators are used when we want to combine two conditions
and produce the result based on that combined condition.
Following are the list of Logical Operators:
(1)Logical NOT (!):
It will complement (inverts) the value of its operand.
It is Unary Operator.
Truth Table for Logical NOT:
Operand Result
true False
false True
(2)Logical OR (|):
It will combine its two operand and returns true if any of its
operand is true. If its both operands are false then it returns
false.
It is Binary Operator.
Truth Table for Logical OR:
Operand1 Operand2 Result
False False False
False True True
True False True
True True True
(3)Logical AND (&):
It will combine its two operand and returns true if and only if
both of its operand is true. If any of its operand is false then it
returns false.
It is Binary Operator.
Truth Table for Logical OR:
Operand1 Operand2 Result
False False False
False True False
True False False
True True True
(4)Logical XOR (^):
It will combine its two operand and returns true if and only if
it’s both operand is not equal. If it’s both operands is equal
then false.
It is Binary Operator.
Truth Table for Logical OR:
Operand1 Operand2 Result
False False False
False True False
True False False
True True True
(4)Assignment Operator:
Assignment Operators are used to assign the value of the operand or
expression to its right to the operands to its left.
Following are the list of Assignment Operators:
(1)Assignment (=):
It will assign the value of the operand or expression to its right
to the operand to its left.
It is Binary Operator.
The value to the right of the = operator must compatible with
the type of the operand to the left of the = operator.
Example:
int a=5,b=5;
int c = a + b;
If the operand to the both side of the assignment operator is
same in the expression then you can write them in short as
shown below:
a = a + 5;
a += 5;
Here += is known as short hand assignment operator.
Following are other short hand assignment operators supported
by Java:
-=, *=, /=, %=
Example:
public class AssignmentDemo1
{
public static void main(String[] args)
{
int a = 5, b=5, c=6;
int d;
d = a+=(b-c);
System.out.println("D="+d);
}
}
Output:
(6)Bitwise Operator:
Bitwise Operators are used to manipulate operands at bit level.
Bitwise Operators perform operation individually on each bit of the
operands.
Following are the list of Bitwise Operators:
(1)Bitwise NOT (~):
Bitwise NOT operator complements (inverts) each individual bit
of the operand to its right.
It is Unary Operator.
Example:
22 = 00010110
~22 = 11101001 = -23
(2)Bitwise OR (|):
Bitwise OR operator performs OR operation on each individual
bits of its two operands. It produces 0 if both bits at the same
position are equal to 0. If any of the bits is 1 then it produces
1.
It is Binary Operator.
Example:
22 | 23 = 23
23 = 00010111
22 = 00010110
23 | 23 = 00010111 = 23
(3)Bitwise AND (&):
Bitwise AND operator performs AND operation on each
individual bits of its two operands. It produces 1 if both bits at
the same position are equal to 1. If any of the bits is 0 then it
produces 0.
It is Binary Operator.
Example:
22 & 23 = 22
23 = 00010111
22 = 00010110
23 & 22= 00010110 = 22
(4)Bitwise XOR (^):
Bitwise XOR operator performs XOR operation on each
individual bit of its two operands. It produces 1 if both bits at
the same position are not equal. If both bits at the same
position are equal then it produce 0.
It is Binary Operator.
Example:
23 ^ 22 = 1
23 = 00010111
22 = 00010110
23 ^ 22 = 00000001 = 1
(5)Bitwise Shift left (<<):
Bitwise Shift left operator shifts each bit specified number of
position to left side.
It is unary operator.
Example:
22<<2
22 = 00010110
22<<2 = 01011000 = 88
It will shift each bit of the operands two position to the left
side. So the left most two bits from the number are shifted out
and it will be replaced by two zeros at right side.
(6)Bitwise Shift right (>>):
Bitwise Shift right operator shifts each bit specified number of
position to right side.
It is unary operator.
Example:
22>>2
22 = 00010110
22>>2 = 00000101 = 5
It will shift each bit of the operands two position to the right
side. So the right most two bits from the number are shifted
out and it will be replaced by two zeros at left side.
(7)Ternary Operator (? :):
It is also known as the conditional operator.
It is called ternary operator because it has three operands.
The general syntax of the Ternary Operator is given below:
(Expression1)? Expression2: Expression3;
Expression1 should contain the condition to be checked using
relational operators.
First it will check the Expression1. If the Expression1 is true then it will
executes the Expression2 otherwise it will executes the Expression3.
It can be useful in the place of If … Else Statement.
Example:
public class TernaryDemo1
{
public static void main(String[] args)
{
int a = 5, b=6;
int max;
max = (a>b)? a: b;
System.out.println (max + " Is maximum");
}
}
Output:
Operator Precedence:
Highest
() [] .
++ –– ~ !
* / %
+ –
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= op=
Lowest
1.5 Overview of control statements
Control statements in JAVA can be classified into two categories:
(1) Conditional Control Statements:
Conditional Control Statement first test for the condition or expression
and based on the outcome of that condition or expression allows you
to control execution of the different portion of the code at run time.
Following are the list of Conditional Control Statements:
(1) Simple if
(2) if … else
(3) Nested if statement
(4) if … else if ladder statement
(5) switch case statement
(2) Looping Control Statements:
Looping control statements allows you to execute certain portion of the
code repeatedly based on the condition.
Sometimes it is required to execute some portion of the code
repeatedly for specific number of times or until some condition is
satisfied at that time you can use Looping Control statements.
Following are the list of Looping Control Statements:
(1) for
(2) while
(3) do … while
1.5.1 Conditional Control Statement
Conditional Control Statement first test for the condition or expression and
based on the outcome of that condition or expression allows you to control
execution of the different portion of the code at run time.
Following are the list of Conditional Control Statements:
(1) Simple if Statement:
The general syntax of simple if statement is given below:
if (condition)
{
Statement block;
}
Statement-x;
In simple if statement first condition is checked.
If the condition is true then it will executes the statement block and then
executes the statement-x that is followed by simple if statement.
If the condition is false then it will not execute the statement block and
executes the statement-x that is followed by simple if statement.
If the statement block contains only one statement inside it then there is no
need to use curly brackets. But if statement block contains more then one
statements inside it then you must have to write them inside curly brackets.
Flowchart:
Statement Block Conditio
n
Statement-X
Example:
public class SimpleIfDemo
{
public static void main (String[] args)
{
True False
Statement-X
Example:
public class IfElseDemo
{
public static void main(String[] args)
{
int a = 5, b = 6;
if (a>b)
System.out.println (a + " Is Maximum");
else
System.out.println (b + " IS Maximum");
}
}
Output:
Ststement3
Conditio
n1
True False
Statement-X
Example:
public class NestedIfDemo
{
public static void main(String[] args)
{
int temp=38;
if (temp>40)
{
if (temp>45)
System.out.println("Very Hot");
else
System.out.println("Hot");
}
else
System.out.println("Average");
}
}
Output:
False
Condition
1
True
False
Statement1 Condition
2
True
False
Statement2 Condition
N
True
StatementN
Default
Statement
Statement X
Example:
public class IfElseIfLadderDemo
{
public static void main(String[] args)
{
int per=66;
if (per >=66)
System.out.println("Distinction");
else if (per <66 && per >=60)
System.out.println("First Class");
else if (per <60 && per >=50)
System.out.println("Second Class");
else if (per <50 && per >=40)
System.out.println("Pass Class");
else
System.out.println("Fail");
}
}
Output:
Expression
Value N No Match
Value 1 Value 2
Example:
public class SwitchCaseDemo
{
public static void main(String[] args)
{
float a=5,b=6,c;
int ch=3;
switch (ch)
{
case 1:
System.out.println("a + b = " + (a+b));
break;
case 2:
System.out.println("a - b = " + (a-b));
break;
case 3:
System.out.println("a * b = " + (a*b));
break;
case 4:
System.out.println("a / b = " + (a/b));
break;
default:
System.out.println("Wrong Choice");
}
}
}
Output:
False
Condition
True
Body of Loop
Example:
public class WhileDemo
{
public static void main(String[] args)
{
int i=1,sum=0;
while (i<=10)
{
sum+=i;
i++;
}
System.out.println ("Sum of 1 to 10 is:"+sum);
}
}
Output:
(2) do … while:
do …while loop is used to repeatedly perform some task until condition
specified within do … while loop is satisfied. It is also known as
iterative statement.
The general syntax of do … while loop is given below:
do
{
Body of Loop
} while (condition);
In do … while loop first statements written inside body of the loop are
executed. After executing statements inside body of the loop condition
is checked. If condition is true then the statements written inside body
of loop are executed again.
This process is repeated until condition specified within do … while loop
becomes false. As the condition becomes false the statements inside
body of loop is not executed and the statements followed by the do …
while loop is executed.
It is also known as exit controlled loop because the condition is
checked at last and then body of the loop is executed based on the
condition. So even if condition is false at the first trial the body of loop
executes at least once.
Flowchart:
Body of Loop
Condition
True False
Example:
public class WhileDemo
{
public static void main(String[] args)
{
int i=1,sum=0;
do
{
sum+=i;
i++;
} while (i<=10);
System.out.println ("Sum of 1 to 10 is:"+sum);
}
}
Output:
(3) for:
for loop is used to repeatedly perform some task for specific number of
times. It is also known as iterative statement. It is useful when you
know in advance how many times you want to repeat the task.
The general syntax of for loop is given below:
for (initialization; condition; iteration)
{
Body of loop
}
Here,
Initialization: It indicates the starting point for the loop. Starting
point specifies from which value the loop will start the iteration.
Condition: It indicates control condition for the loop. Control condition
determines when loop will stop.
Iteration: It indicates how the value of the counter variable that holds
the starting value will vary. You can either increment or decrement the
value of the counter variable based on your requirement.
In for loop first the counter variable is initialized with the starting value
in the initialization part. After initialization the condition is checked. If
the condition is true then the statements written inside body of loop
will executes. After executing statements inside body of loop the
control goes to the Iteration part where the value of the counter
variable is incremented decremented. After changing the value of the
counter variable it again check the condition.
This process is repeated until condition specified within for loop
becomes false. As the condition becomes false the statements inside
body of loop will not executes and control transfer immediately to the
statement followed by for loop.
Flowchart:
Initialization
False
Condition
True
Body of Loop
Iteration
(Increment or
Decrement)
Example:
public class ForLoopDemo
{
public static void main(String[] args)
{
int a=0,b=1,c;
int i;
System.out.println ("Fibonacci Series :");
System.Out.print (a+" ");
System.Out.print (b+" ");
for (i=1;i<=5;i++)
{
c=a+b;
System.Out.print (c+" ");
a=b;
b=c;
}
}
}
Output:
1.5.3 break and continue:
break:
break statement allows you to:
(1)Transfer the control of the program immediately after the loop
either conditionally or unconditionally.
It allows you to transfer the control of the program outside the loop in
which it exist.
You can break the execution of the loop either conditionally or
conditionally.
If you write break statement inside the nested loop then it will transfer
program control to the outside loop in which it exist.
Consider following example:
public class breakDemo
{
public static void main(String[] args)
{
int i;
for (i=1;i<10;i++)
{
if (i%5==0)
break;
else
System.Out.print(i+" ");
}
}
}
Output:
Now consider the following program without writing break in each case
block:
public class breakDemo
{
public static void main(String[] args)
{
int ch=1;
switch (ch)
{
case 1:
System.out.println ("Blue");
case 2:
System.out.println ("Green");
case 3:
System.out.println ("Red");
}
}
}
Output:
Here, it will execute each case block because you don’t specify break
statement in the case blocks.
(3) Transfer the control of the program after specific loop in nested
loop.
It allows you to specify label with break statement. Using this statement
you can transfer the control of the program outside the specified loop.
It is useful while dealing with nested loop. When you write break
statement inside the inner loop it will transfer the control of the program
only after that inner loop. However sometimes it is required to transfer
program control outside all the loops or outside specific loop.
This form of break statement is also known as labeled loop.
The label must be specified at the starting of loop and it must ends with
colon.
Consider following example:
public class breakDemo
{
public static void main(String[] args)
{
int i,j;
outerloop:
for(i=1;i<10;i++)
{
innerloop:
for(j=1;j<=i;j++)
{
if (i%j==0)
System.out.print(j);
else
break outerloop;
}
System.out.println(" ");
}
}
}
Output: