KEMBAR78
OOPM Lecture2 | PDF | Java (Programming Language) | Java Virtual Machine
0% found this document useful (0 votes)
4 views39 pages

OOPM Lecture2

oopm lecture 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

OOPM Lecture2

oopm lecture 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Subject Name: Skill Based Lab OOPM

Unit No:1
Unit Name: Introduction to C++ and Java

Faculty Name : Mr.Gajendrasingh Rajput


Index

Lecture 3 – Features of Java Language, JDK environment 3

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

• A general purpose object oriented


programming language developed by
Sun Microsystems in 1991.
• Originally called Oak by James
Gosling, the first designer of Java and
implemented its original compiler and
virtual machine.
• Renamed as JAVA in 1995
• Versions
– Java 2 Micro Edition (J2ME)
– Java 2 Standard Edition (J2SE)
– Java 2 Enterprise Edition (J2EE)

4 Lecture 3- Features of Java Language, JDK environment


Features of Java

5 Lecture 3- Features of Java Language, JDK environment


Features of 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.

6 Lecture 3- Features of Java Language, JDK environment


Features of Java

• 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.

7 Lecture 3- Features of Java Language, JDK environment


Features of Java

• Robust:
➢ Strong memory management.
➢ No usage of pointers.
➢ Exception handling and type checking mechanisms.

“An Exception is the condition that is caused by run time


error in the program. If it occurred java interpreter creates
an exception object and throws it.”

8 Lecture 3- Features of Java Language, JDK environment


Features of Java

• 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.

9 Lecture 3- Features of Java Language, JDK environment


Features of Java

• Distributed and Network Oriented


Java grew up in the days of the Internet
➢ Inherently network friendly
➢ Original release of Java came with Networking libraries
➢ Newer releases contain even more for handling distributed
applications
- RMI, Transactions

10 Lecture 3- Features of Java Language, JDK environment


Features of Java

• Compiled and Interpreted


➢ Java works in two stage
– Java compiler translate the source code into byte code.
– Java interpreter converts the byte code into machine level
representation.
➢ Byte Code:
- A highly optimized set of instructions to be executed by the java
runtime system, known as java virtual machine (JVM).
- Not executable code.
➢ Java Virtual Machine (JVM):
- Need to be implemented for each platform.
- Although the details vary from machine to machine, all JVM
understand the same byte code.

11 Lecture 3- Features of Java Language, JDK environment


Java Virtual Machine
✓ Java compiler produces an intermediate code known as byte code for a
machine, known as JVM.
✓ It exists only inside the computer memory.

Java Program Virtual Machine


Java Compiler .class file (byte code)
.java file

✓ Machine code is generated by the java interpreter by acting as an


intermediary between the virtual machine and real machine.

Virtual machine Real machine


Java Interpreter
Bytecode Machine Code

12 Lecture 3- Features of Java Language, JDK environment


Module No 1: Introduction to C++ and Java

Lecture No: 4
Basic programming constructs: variables, data
types, operators, unsigned right shift operator,
expressions, branching and looping
Simple Java Program

// Program to print Hello message


public class Hello
{

public static void main (String[ ] args)


{

System.out.print (“Hello”);
}

Lecture 4- Basic programming constructs: variables, data types, operators,


14
unsigned right shift operator, expressions, branching and looping
Java Program Structure

// comments about the class


public class MyProgram
{
class header
// comments about the method

public static void main (String[ ] args)


{ class body
method header
method body
}

Lecture 4- Basic programming constructs: variables, data types, operators,


15
unsigned right shift operator, expressions, branching and looping
Compile and Execute Java Program

Java program Type it in with the text editor of your


choice
filename.java

Java compiler

javac

Java byte code


filename.clas
To compile the program at s
the command line type
"javac filename.java" )
Java Interpreter

java

To run the interpreter, at


the command line type
"java filename"
Lecture 4- Basic programming constructs: variables, data types, operators,
16
unsigned right shift operator, expressions, branching and looping
Basic Constructs of Java

▪ Constants
▪ Variables
▪ Data Types
▪ Operators and Expression
▪ Revision of Branching and Looping

Lecture 4- Basic programming constructs: variables, data types, operators,


17
unsigned right shift operator, expressions, branching and looping
Constants

▪ The variable whose value is always constant (does not change).


▪ The reserved word final is used in constant declaration.
▪ Syntax:
final data_type constant_name=constant_value;

Lecture 4- Basic programming constructs: variables, data types, operators,


18
unsigned right shift operator, expressions, branching and looping
Variables

▪ A variable can be thought of as a container which holds value for you,


during the life of a Java program.
▪ Every variable is assigned a data type which designates the type and
quantity of value it can hold.
▪ All variables must be declared before its use.
▪ Variables can be declared almost anywhere (scope rules apply)
▪ Variables have default initialization values
o Integers: 0
o Reals: 0.0
o Boolean: False
▪ Variables can be initialized in the declaration.
▪ Syntax:
[access_modifier] [static] [final] type name [= value];

Lecture 4- Basic programming constructs: variables, data types, operators,


19
unsigned right shift operator, expressions, branching and looping
Data Types

Lecture 4- Basic programming constructs: variables, data types, operators,


20
unsigned right shift operator, expressions, branching and looping
Data Types

▪ Primitive Data Types


o byte (1), -128 to 127
o short (2), -32768 to 32767
o int (4), -2147483648 to 2147483647
o long (8), -9223372036854775808 to 9223372036854775807
o float (4), -3.4E38 to 3.4E38, 7 digit precision
o double (8), -1.7E308 to 1.7E308, 17 digits precision
o char (2), unicode characters
o boolean (true, false), discrete values

▪ Non-primitive/ User-defined Data Types


o Array
o Class
o interface

Lecture 4- Basic programming constructs: variables, data types, operators,


21
unsigned right shift operator, expressions, branching and looping
Keywords in Java

▪ 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

Lecture 4- Basic programming constructs: variables, data types, operators,


22
unsigned right shift operator, expressions, branching and looping
Java Operators

▪ Operators/Order of Precedence

Operator Operation Precedence

() or [ ] or . or ++ or - - Parens, array indices, object 1


invocation, increment, decrement
+ or – or ~ Unary plus, unary minus, complement 2

* or / or % Multiplication, division, modulus 3


+ or - Addition, subtraction 4
<<,>> Bitwise shifts 5
& Bitwise AND 6
^ Bitwise XOR 7
| Bitwise OR 8

Lecture 4- Basic programming constructs: variables, data types, operators,


23
unsigned right shift operator, expressions, branching and looping
Java Operators

 Example Usage

int anInt1 = 2, anInt2 = 1, anInt3;


double aDbl1;

anInt3 = anInt1 + anInt2 * 4; // anInt3 gets 6


anInt3 = (anInt1 + anInt2) * 4; // anInt3 gets 12
anInt3 = ++anInt2 * 2; // anInt3 gets 4
anInt2 = 1;
anInt3 = anInt2++ * 2; // anInt3 gets 2
anInt2 = 1;
anInt3 = anInt1 & anInt2; // anInt3 gets 0
anInt3 = anInt1 ^ anInt2; // anInt3 gets 3
anInt3 = anInt2 << 1; // anInt3 gets 2
anInt3 = 1 / 2; // anInt3 gets 0
aDbl1 = 1 / 2; // aDbl1 gets 0.0
aDbl1 = 1.0 / 2.0; // aDbl1 gets 0.5

Lecture 4- Basic programming constructs: variables, data types, operators,


24
unsigned right shift operator, expressions, branching and looping
Java Operators…..

▪ 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;

Lecture 4- Basic programming constructs: variables, data types, operators,


25
unsigned right shift operator, expressions, branching and looping
Control Statements in Java

1. Conditional Statements
▪ if-else statements
▪ switch case statements

2. Iterative Statements
▪ for loop
▪ while loop
▪ do-while loop

Lecture 4- Basic programming constructs: variables, data types, operators,


26
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

 “if” Statements

◦ if with code block

if (boolean_expr)
{
statements
}

◦ if with single statement

if (boolean_expr)
statement;

Lecture 4- Basic programming constructs: variables, data types, operators,


27
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

 if” Statements (Continued)

◦ if-else

if (boolean_expr) if
true false
{
statements for true
} statements statements
else
{
statements for false
}

Lecture 4- Basic programming constructs: variables, data types, operators,


28
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

▪ 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

> or < Greater than, less than

>= or <= Greater than or equal, less than or equal

! Unary negation (NOT)

& or && Evaluation AND, short circuit AND

| or || Evaluation OR, short circuit OR

Lecture 4- Basic programming constructs: variables, data types, operators,


29
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

 Boolean Expression Examples

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

Lecture 4- Basic programming constructs: variables, data types, operators,


30
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

▪ if-else” Statement Example


class Example
{
static public void main(String args[])
{ if
// A very contrived example
true false
int i1 = 1, i2 = 2;
System.out.print(“Result: “);
if (i1 > i2)
{ statements statements
System.out.println(“i1 > i2”);
}
else
{
System.out.println(“i2 >= i1”);
}
}
}

Lecture 4- Basic programming constructs: variables, data types, operators,


31
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

 The Switch Statement


true
switch (integer_expression) if statements
{
false
case int_value_1:
statements true
if statements
break;
case int_value_2: false
statements
break; true
if statements

case int_value_n: false
statements
break; statements
default:
statements
}

Lecture 4- Basic programming constructs: variables, data types, operators,


32
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

 Don’t forget the “break”


true
switch (integer_expression) if statements
{
false
case int_value_1:
statements true
if statements
// No break!
case int_value_2: false
statements
break; true
if statements

case int_value_n: false
statements
break; statements
default:
statements
}

Lecture 4- Basic programming constructs: variables, data types, operators,


33
unsigned right shift operator, expressions, branching and looping
Java Conditional Constructs

 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;
}

Lecture 4- Basic programming constructs: variables, data types, operators,


34
unsigned right shift operator, expressions, branching and looping
Java Looping Constructs

▪ 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

Lecture 4- Basic programming constructs: variables, data types, operators,


35
unsigned right shift operator, expressions, branching and looping
Java Looping Constructs

▪ while loop
false if
while (boolean_expr)
true
{
statements statements
}

▪ do loop
statements
do
{
statements if true
}
false
while (boolean_expr)

Lecture 4- Basic programming constructs: variables, data types, operators,


36
unsigned right shift operator, expressions, branching and looping
Java Looping Constructs

▪ for loop

for (init_stmnt; bool_expr; update_stmnt)


{
statements init
}

false
if

true

statements update

Lecture 4- Basic programming constructs: variables, data types, operators,


37
unsigned right shift operator, expressions, branching and looping
Java Looping Constructs

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

Lecture 4- Basic programming constructs: variables, data types, operators,


38
unsigned right shift operator, expressions, branching and looping
Thank You

You might also like