0% found this document useful (0 votes)
22 views

Lec2 - Basics and Overview

The document provides an overview of object-oriented programming and covers key concepts like identifiers, variables, operators, statements, data types, and more. It discusses the basics of identifiers, variables, and operators. It describes the different types of operators like arithmetic, bitwise, relational, and logical operators. It also covers data types in Java, including primitive types like numbers, characters, and booleans, as well as reference types like arrays, classes and interfaces.

Uploaded by

Faiz Abaas
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)
22 views

Lec2 - Basics and Overview

The document provides an overview of object-oriented programming and covers key concepts like identifiers, variables, operators, statements, data types, and more. It discusses the basics of identifiers, variables, and operators. It describes the different types of operators like arithmetic, bitwise, relational, and logical operators. It also covers data types in Java, including primitive types like numbers, characters, and booleans, as well as reference types like arrays, classes and interfaces.

Uploaded by

Faiz Abaas
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/ 32

Object oriented Programming

Lecturer: Dr. Wedad Al-Sorori


Object oriented Programming

Basics and Overview


Outlines


Identifiers

Variables

Operators

Statements’ type

08/27/2022 3
Identifiers
• Identifiers are used for class names, method names, and variable names.
• An identifier may be any sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters.
• Identifiers must not begin with a number.
• Java Identifiers are case-sensitive.
• Some valid identifiers are ATEST, count, i1, $Atest, and this_is_a_test
• Some invalid identifiers are 2count, h-l, and a/b
Operators

• An operator performs a function on one, two, or three operands.

• Unary operators (example: ++) (postfix and prefix)


• Binary operators (example: +) (infix)
• Ternary operators ( example: ?:) (infix)

08/27/2022 5
Operators

Java operators can be grouped into the following four groups:

Arithmetic,

Bitwise,

Relational, and

Logical.

08/27/2022 6
Arithmetic Operators
Th
e

Operator Result nu ope
m
bo eric rand
+ Addition o le s
an type. of th

- Subtraction (unary minus) ty p Y e


es, ou c arith

bu ann me

* Multiplication ty
ou ot us tic op

/ Division c an e a er a
use rithm tors

% Modulus the etic mu
m s
on oper t be o
++ Increment ch a t o
r fa

ar
+= Addition assignment typ s on

es
.

-= Subtraction assignment

*= Multiplication assignment

/= Division assignment

%= Modulus assignment

-- Decrement

08/27/2022 7
Bitwise Operators
Jav

Operator Result lon a bit

~ Bitwise unary NOT the g, int wise
i nd , s h o pe

& Bitwise AND ivid ort, rato
ua

| Bitwise OR l bi char, rs ca
ts n
^ Bitwise exclusive OR of byte. be a

the
ir o Bitwi pplie

>> Shift right pe s e d
ran Op to th

>>> Shift right zero fill ds. era e i
tor nte
<< Shift left s a ge

ct r
up t y p e

&= Bitwise AND assignment on s:

|= Bitwise OR assignment

^= Bitwise exclusive OR assignment

>>= Shift right assignment

>>>= Shift right zero fill assignment

<<= Shift left assignment

08/27/2022 8
Relational Operators
Th
e
rel rela
Operator Result ati t
on iona

sh lo
== Equal to ip p
be erat

tw ors

!= Not equal to ee
nt d
wo eterm

> Greater than op i
era ne th

< Less than nd e
s.

>= Greater than or equal to

<= Less than or equal to

08/27/2022 9
Operators

• Other operators:
Operator Description

?: Shortcut if-else statement


example : a == 1 ? true : false;
[] Used to declare arrays, create arrays, and access
array elements
. Used to form qualified names

{ params } Delimits a comma-separated list of parameters

( type ) Casts (converts) a value to the specified type


Operators

• Other operators:

• The new operator creates a new object or a new array.


• Example: creating a new Integer object from the Integer class in the java.lang package:
• Integer anInteger = new Integer(10);

• The instanceof operator tests whether its first operand is an instance of


its second.
• op1 instanceof op2
• op1 must be the name of an object and op2 must be the name of a class. An object is considered to be an
instance of a class if that object directly or indirectly descends from that class.
Operators
Operators precedence:
 [] . (params) expr++ expr--  ^
 ++expr --expr +expr -expr ~ !  |
 new (type)expr  &&
 */%  ||
 +-  ?:
 << >> >>>  = += -= *= /= %= &= ^= |= <<=
>>= >>>=
 < > <= >=
 == !=
 &
Data Types
• Three kinds of data types are supported by Java.
• Primitive data types
• a number, a character, or a boolean value

• Reference data types

Arrays, classes, and interfaces are reference types.

The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an
address of) the value or set of values represented by the variable.

A reference is called a pointer, or a memory address. The Java does not support the explicit use of
addresses like other languages do. You use the the variable's name instead


The special null data type

{that is we may write if (obj!= null)}
Primitive Data Types in Java
Type Kind Memory Range

byte integer byte 1 to 127 128-

short integer bytes 2 to 32767 32768-

int integer bytes 4 to 2147483647 2147483648-

to 9223372036854775808-
long integer bytes 8
    -9223372036854775807

x 1038 to ±3.40282347
float floating point bytes 4
  ±3.40282347 x 10-45

x 10308 to ±1.76769313486231570
double floating point bytes 8
     ±4.94065645841246544 x 10-324

char single character bytes 2 all Unicode characters

boolean true or false bit 1  

There is no unsigned integer in java .

08/27/2022 14
Try this….


/** This program demonstrates how Java

* adds two integers. */

public class BigInt

{

public static void main(String args[])

{

int a = 2000000000; //(9 zeros)

int b = 2000000000;

System.out.println ( “This is how Java adds integers”);

System.out.println ( a + “+” + b + “ = ” + (a+b) );

} // end of main
Output:

This is how Java adds integers

}// end of class 2000000000 + 2000000000 = -294967296

08/27/2022 15
Try this….


public class Significant

{

public static void main (String args[])

{

final float PI = 3.141519265359f;

float radius = 1.0f;

float area;

area = PI * radius * radius;

System.out.println (“The area of the circle = ” + area);

}// end of main

}// end of class

Output:
area of the circle = 3.1415193

08/27/2022 16
Variable
• The value of a final variable cannot change after it has been initialized. Such
variables are similar to constants in other programming languages.
• To declare a final variable, use the final keyword in the variable declaration
before the type:
final int aFinalVar = 0;

• It is possible declare the local variable and initialize it later (but only once):
final int blankfinal;
...
blankfinal = 0;
Declaration of variable

A variable is defined by an identifier, a type, and an optional initializer.

The variables also have a scope(visibility / lifetime).

It must be unique within its scope.

It must not be a keyword, a boolean literal (true or false), or the reserved word null.

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is :


type identifier [ = value][, identifier [= value] ...] ;

Java allows variables to be initialized dynamically. For example: double c = 2 * 2;

08/27/2022 18
:Scope and life of a variable


Variables declared inside a scope are not accessible to code outside.

Scopes can be nested. The outer scope encloses the inner scope.

Variables declared in the outer scope are visible to the inner scope.

Variables declared in the inner scope are not visible to the outside scope.

08/27/2022 19
Try this….


public class Main

{ public static void main(String args[])

{ int x; // known within main

x = 10;

if (x == 10)

{ int y = 20;

System.out.println("x and y: " + x + " " + y);

x = y + 2; }

System.out.println("x is " + x);

}// end of main Output:
x and y: 10 20

}// end of class x is 22

08/27/2022 20
Try this….


public class Main2

{ public static void main(String args[])

{ if (true)


{ int y = 20;

System.out.println("y: " + y);

} // end of if Output
D:\>jav
:
 ac Main
locatio .java M
n: clas
s Main ain.java:9: ca
nnot fin

y = 100; y = 1 00
d symb
ol s y m
; // Erro bol : va
^ r! y not riable y
 known
1 error here


}// end of main

}// end of class

08/27/2022 21
Try this….


public class Main3

{ public static void main(String args[])

{ int i = 1;

{int i = 2;

}

}
Output

} Results
:
in compil
ation e
rror.
‘i‘ is alr
eady d
efined…

08/27/2022 22
Control Flow Statements

Statement type Keyword


looping while, do-while , for

decision making if-else, switch-case

exception handling try-catch-finally, throw

branching break, continue, label:, return


While, do-while and for

while (expression) { for (initialization; termination;


statement(s) increment) {

} statement
}

do { for ( ; ; ) {

statement(s) // infinite loop

} while (expression); ... }


If, if-else and switch
if (expression) switch (intVariable) {
{ statement(s) case 1:
} System.out.println(„1");
break;
default:
System.out.println(„The
if (expression) { number is wrong !");
// code to perform break;
true
}
} else {
// code to perform
false
}
Try, catch and finally

try {
statement(s)
}
catch (exceptiontype name) {
statement(s)
}
finally {
statement(s)
}
Try catch Example


try

{

Scanner in = new Scanner(new File("input.txt"));

String input = in.next();

process(input);

}

catch (IOException exception)

{

System.out.println("Could not open input file");

}
Break, continue and return
Break and continue:
1) break; || continue; Return:

2) break label; || continue label; 1) return value;

Example (by analogy is continue): 2) return;

...
search:
for ( ; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length; j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}}}
Break Example
Control Flow Statements
Question:
What's wrong with the following code :
if (i = 1) {
/* do something */
}

Answer:
Condition is ALWAYS true. Typical mistake of programmers:
= is an assignment == is a comparison
Ussually compiler warns about it.
Control Flow Statements(3)
Question:
What is an output if aNumber = 2?

if (aNumber >= 0)
if (aNumber == 0) System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");

Answer:
second string
third string


The end

08/27/2022 32

You might also like