Basics of Java Programming
Unit-II
22PLC25C
[Link]
Arithmetic Operators
[Link]
// Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
[Link]("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("c = " + c);
[Link]("d = " + d);
[Link]("e = " + e);
// arithmetic using doubles
}
} [Link]
[Link]("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
[Link]("da = " + da);
[Link]("db = " + db);
[Link]("dc = " + dc);
[Link]("dd = " + dd);
[Link]("de = " + de);
}
}
[Link]
Output
Integer Arithmetic
a=2
b=6
c=1
d = -1
e=1
Floating Point Arithmetic
da = 2.0
db = 6.0
dc = 1.5
dd = -0.5
de = 0.5
[Link]
The Bitwise Operators
[Link]
The Bitwise Logical Operators
[Link]
// Demonstrate the bitwise logical operators.
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
[Link](" a = " + binary[a]);
[Link](" b = " + binary[b]);
[Link](" a|b = " + binary[c]);
[Link](" a&b = " + binary[d]);
[Link](" a^b = " + binary[e]);
[Link]("~a&b|a&~b = " + binary[f]);
[Link](" ~a = " + binary[g]);
}
}
[Link]
Output
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
[Link]
The Left Shift
The left shift operator, << shifts all of the bits in a value to the
left a specified number of times.
// Left shifting a byte value.
class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
[Link]("Original value of a: " + a);
[Link]("i and b: " + i + " " + b);
}
}
[Link]
Original value of a: 64
i and b: 256 0
a= (0100 0000)
i= (1 0000 0000)
The value in b contains 0 because after the shift,
the low-order byte is now zero. Its only 1 bit
has been shifted out
[Link]
The Right Shift
The right shift operator, >>, shifts all of the bits
in a value to the right a specified number
of times.
value >> num
int a = 32;
a = a >> 2; // a now contains 8
[Link]
Bitwise Operator Assignments
• All of the binary bitwise operators have a
shorthand form similar to that of the
algebraic operators, which combines the
assignment with the bitwise operation. For
example, the following two statements,
which shift the value in a right by four bits,
are equivalent:
• a = a >> 4;
• a >>= 4;
[Link]
Relational Operators
• The relational operators determine the
relationship that one operand has to the
other.
[Link]
Ex
int a = 4;
int b = 1;
boolean c = a < b;
In this case, the result of a<b (which is false) is
stored in c.
[Link]
The Assignment Operator
• .The assignment operator is the single equal
sign, =. The assignment operator works in
Java much as it does in any other computer
language.
• var = expression;
[Link]
The ? Operator
• Java includes a special ternary (three-way)
operator that can replace certain types of if-
then-else statements.
• expression1 ? expression2 : expression3
• ratio = denom == 0 ? 0 : num / denom;
[Link]
// Demonstrate ?.
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
[Link]("Absolute value of ");
[Link](i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
[Link]("Absolute value of ");
[Link](i + " is " + k);
}
}
[Link]
output
Absolute value of 10 is 10
Absolute value of -10 is 10
[Link]
Operator Precedence
[Link]
Using Parentheses
• Parentheses raise the precedence of the
operations that are inside them. This is often
necessary to obtain the result you desire.
• a >> (b + 3) This expression first adds 3 to b
and then shifts a right by that result.
• However, if you want to first shift a right by b
positions and then add 3 to that result
• (a >> b) + 3
[Link]
Control Statements
• Java’s Selection Statements
if (condition) statement1;
else statement2;
• Nested ifs
if(i == 10) {
if(j < 20)
a = b;
if(k > 100)
c = d; // this if is
else a = c; // associated with this else
}
else
a = d; // this else refers to if(i == 10)
[Link]
If else if
// Demonstrate if-else-if statements.
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
[Link]("April is in the " + season + ".");
}
}
[Link]
output
April is in the Spring.
[Link]
switch
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
[Link]
output
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.
[Link]
// A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
[Link]("i is zero.");
break;
case 1:
[Link]("i is one.");
break;
case 2:
[Link]("i is two.");
break;
case 3:
[Link]("i is three.");
break;
default:
[Link]("i is greater than 3.");
}
}
}
[Link]
Output using switch
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more
[Link]
// In a switch, break statements are optional.
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
[Link]("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
[Link]("i is less than 10");
break;
default:
[Link]("i is 10 or more");
}
}
}
[Link]
output
April is in the Spring.
[Link]
class Switch {
public static void main(String args[]) {
int month = 4;
String season;
switch (month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
[Link]("April is in the " + season + ".");
}
}
[Link]
Iteration Statements
• While
while(condition)
{ // body of loop
}
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
[Link]("tick " + n);
n--;
}
}
}
[Link]
// The target of a loop can be empty.
class NoBody {
public static void main(String args[]) {
int i, j;
i = 100;
j = 200;
// find midpoint between i and j
while(++i < --j) ; // no body in this loop
[Link]("Midpoint is " + i);
}
}
O/P: Midpoint is 150
[Link]
do-while
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
[Link]("tick " + n);
n--;
} while(n > 0);
}
}
[Link]
for
// Demonstrate the for loop.
class ForTick {
public static void main(String args[]) {
int n;
for(n=10; n>0; n--)
[Link]("tick " + n);
}
}
[Link]
// Test for primes.
class FindPrime {
public static void main(String args[]) {
int num;
boolean isPrime = true;
num = 14;
for(int i=2; i <= num/2; i++) {
if((num % i) == 0) {
isPrime = false;
break;
}
}
if(isPrime) [Link]("Prime");
else [Link]("Not Prime");
}
}
[Link]
Using the Comma
// Using the comma.
class Comma {
public static void main(String args[]) {
int a, b;
for(a=1, b=4; a<b; a++, b--) {
[Link]("a = " + a);
[Link]("b = " + b);
}
}
}
[Link]
..........
.........
........
.......
......
.....
....
...
..
.
[Link]
Nested Loops
// Loops may be nested.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
[Link](".");
[Link]();
}
}
}
[Link]
This program generates the following output:
Pass 0: 0 1 2 3 4 5 6 7 8 9
Pass 1: 0 1 2 3 4 5 6 7 8 9
Pass 2: 0 1 2 3 4 5 6 7 8 9
Loops complete.
[Link]
// Jump Statements Using break with nested loops.
class BreakLoop3 {
public static void main(String args[]) {
for(int i=0; i<3; i++) {
[Link]("Pass " + i + ": ");
for(int j=0; j<100; j++) {
if(j == 10) break; // terminate loop if j is 10
[Link](j + " ");
}
[Link]();
}
[Link]("Loops complete.");
}
}
[Link]
Using continue
// Demonstrate continue.
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
[Link](i + " ");
if (i%2 == 0) continue;
[Link]("");
}
}
}
[Link]
01
23
45
67
89
[Link]
return
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
[Link]("Before the return.");
if(t) return; // return to caller
[Link]("This won't execute.");
}
}
[Link]