OOPM Lecture2
OOPM Lecture2
Unit No:1
Unit Name: Introduction to C++ and Java
Lecture 4 – Basic programming constructs: variables, data types, operators, unsigned right shift
7
operator, expressions, branching and looping
2
Module No 1: Introduction to C++ and Java
Lecture No: 3
Features of Java Language, JDK environment
Introduction to JAVA
• Simple:
➢ Easy to learn.
➢ removed many confusing and rarely-used features
e.g., explicit pointers, operator overloading etc.
• Object-oriented:
➢ Organize our software as a combination of different types of
objects that incorporates both data and behaviour.
• Platform Independent:
➢ A platform is the hardware or software environment in which a
program runs.
➢ It has two components:
✓ Runtime Environment
✓ API(Application Programming Interface)
➢ “Write-Once Run-Anywhere”
✓ Java code can be run on multiple platforms e.g. Windows,
Linux, Sun Solaris, Mac/OS etc.
• Robust:
➢ Strong memory management.
➢ No usage of pointers.
➢ Exception handling and type checking mechanisms.
• Multi-threaded
➢ A thread is like a separate program, executing concurrently.
➢ Java programs deal with many tasks at once by defining
multiple threads.
➢ Doesn't occupy memory for each thread.
➢ Shares a common memory area.
Lecture No: 4
Basic programming constructs: variables, data
types, operators, unsigned right shift operator,
expressions, branching and looping
Simple Java Program
System.out.print (“Hello”);
}
Java compiler
javac
java
▪ Constants
▪ Variables
▪ Data Types
▪ Operators and Expression
▪ Revision of Branching and Looping
▪ These are the reserved words in JAVA which cannot be used as variable
names or identifiers.
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
▪ Operators/Order of Precedence
Example Usage
▪ Assignments
▪ General Format: variable = expression ;
Where variable is a previously declared identifier and
expression is a valid combo of identifiers, operators,
and method (a.k.a. procedure or function) calls
▪ Shortcuts:
var *= expr ; // Equivalent to var = var * (expr);
var /= expr ; // Equivalent to var = var / (expr);
var += expr ; // Equivalent to var = var + (expr);
var -= expr ; // Equivalent to var = var – (expr);
var %= expr ; // Equivalent to var = var % (expr);
var++; // Equivalent to var = var + 1;
var--; // Equivalent to var = var - 1;
1. Conditional Statements
▪ if-else statements
▪ switch case statements
2. Iterative Statements
▪ for loop
▪ while loop
▪ do-while loop
“if” Statements
if (boolean_expr)
{
statements
}
if (boolean_expr)
statement;
◦ if-else
if (boolean_expr) if
true false
{
statements for true
} statements statements
else
{
statements for false
}
▪ Boolean Expressions
▪ Boolean expressions use conditional operators such that they result in a
value of true or false
▪ Conditional Operators (Not by order of precedence)
Operators Operations
== or != Equality, not equal
int i1 = 1, i2 = 2, i3 = 3, i4 = 0;
boolean b1 = true, b2 = false, b3 = true;
i2 > i1 // true
(i3 – i2) > i1 // false
(i3 – i2) >= i1 // true
(i2 > i1) & (i2 < i3) // true
(i2 < i1) | (i2 > i1) // true
i2 != i1 // true
(i1 < i2) | ((i1/i4) > 1) // Divide by 0 exception
(i1 < i2) || ((i1/i4) > 1) // true
(i1 < i2) | (i1++ > 1) // true, i1 contains 2
(i1 < i2) || (i1++ > 1) // true, i1 contains 1
b1 && b2 && b3 // false
(b1 || b2) && b3 // true
b1 && (i1 == (i3 – i2)) // true
Example
int n = 5;
true
switch (n) if statements
{
false
case 1:
n = n + 1; true
if statements
break;
case 5: false
n = n + 2;
break; statements
default:
n = n – 1;
}
▪ while loop
▪ Exit condition evaluated at top
▪ do loop
▪ Exit condition evaluated at bottom
▪ for loop
▪ Exit condition evaluated at top
▪ Includes a initialization statements
▪ Includes a update statements for each iteration
▪ while loop
false if
while (boolean_expr)
true
{
statements statements
}
▪ do loop
statements
do
{
statements if true
}
false
while (boolean_expr)
▪ for loop
false
if
true
statements update
class Example
{ static public void main(String args[])
{
int i = 0;
System.out.println("while loop");
while (i < 10)
{
System.out.println(i);
i++;
}
System.out.println("do loop");
do
{
System.out.println(i);
i--;
}
while (i > 0);
System.out.println("for loop");
for (i = 0; i < 10; i++)
{
System.out.println(i);
}
} // End main
} // End Example