`
Object Oriented Programming using Java Meeting 1
للس تفسار عن لك ما يتعلق ابملادة تواصل مع ا ألس تاذ خال ابلتلجرام عن طريق مسح الباركود
التواصل عرب التلجرام فقط
رمق التواصل -اس تاذ خال+965 50515237 :
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
Meeting 1
Java Revision | Introduction to programming using Java – Part 1
• The aims of the Java language
Simple to learn
Object-oriented: Java programs are structured around objects
Robust: Java programs are strictly checked by software before they run
Secure: Java ensures that programs running over networks cannot
damage your computer files or introduce viruses.
Portable: Java programs can easily be transferred from one
platform to run on another platform.
High performance: Java programs can run fast
Interpreted: a key aspect of Java portability
Threaded: allow a program to do several things at once.
Dynamic: Java programs can adapt to changes in their environment
• Versions & Editions of Java
Java 1.0: The first publicly available version | Java 2: The standard language now
Java editions:
Java 2 Standard Edition (J2SE)
Java 2 Enterprise Edition (J2EE) - for large scale systems
Java 2 Micro Edition (J2ME) - for mobile phones
• Java Background
Block Comment
Main Method
Line Comment or In-line Comment
Method header
Method Body
Statement Termination
2
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
• The System & Getting Java running
Portability: way that programs written in Java can be run on many different
platforms.
Source code is translated by a compiler to
bytecode (intermediate form) which is
translated by an interpreter into the native
code of the computer it runs on which is then
executed.
The interpreter is a program that translates
bytecode into the native code of the
computer it runs on
• The Java Software Development Kit (SDK or JDK)
SDK tools are used for compiling and running Java programs on a variety of platforms
SDK tools include:
Java compiler (javac) translates Java source into bytecode.
Java interpreter (java) translates and executes Java bytecode.
Document generator (javadoc) processes Java source files to produce useful
documentation
Java debugger (jdb) helps to look for errors in programs.
Java disassembler (javap) reads bytecode files and displays the source code.
Applet viewer (appletviewer) allows you to run and debug Java applets without a
web browser
• Java programmers can produce two types of software:
a. Applications are stand-alone programs, which can run independently.
b. Applets are programs that can be included in web documents and are run
using a browser.
3
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
• Data types
There are two categories of data type in Java:
1. Primitive variables store data values.
2. Reference variables do not themselves store data values, and
refers to objects
• Primitive Data Types: Declaration and Initialization
Declaration: Create a variable of the type you want to use
Initialization: Refers to when you first store something useful in a
variable's memory location.
Category Description Primitive Data Type Size (Bits)
byte 8
Whole Numbers
short 16
Numbers (Integral Types) int 32
)(اعداد صحيحة long 64
Real Numbers float 32
)(اعداد عشرية
double 64
Boolean Boolean Values boolean 1
Characters Single Characters char 16
4
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
• The char type
Java has a character data type called char, which allows to represent single
characters. Character literals are denoted by a value enclosed within single quotes.
Character variable can be assigned any value in the Unicode character set.
Escape sequence: It is a sequence of characters standing in place of a single value of
the character type. In Java, they are marked by the use of the backslash character.
Escape Sequence Description
\b backspace
\f formfeed
\n linefeed
\r Carriage return
\t Horizontal tab
\' Single quote
\" Double quote
\\ backlash
• Floating-point types
In order to represent numbers that include fractional parts, we use floating-point
types
Are denoted either by writing them as integerpart.fractionalpart,
as double x = 23.8 or by using scientific notation double y = 1.43E8, z = 1e-9
• Exercise
5
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
• Casting
Convert from one type to another. This can be done either:
Automatically (Implicit Conversion): Java does it for us and known as
promotion where Java automatically converts smaller data types to
larger data types.
int intValue = 42;
double doubleValue = intValue; // Automatic conversion from int to double
Casting (Explicit Conversion): we have to specify a type conversion by writing the
desired type of an expression in parentheses in front of the expression; for
example, write (int) in front of an expression if we wanted it to be converted to
an int.
double doubleValue = 3.14;
int intValue = (int) doubleValue; // Explicit conversion from double to int
Explicit Conversion
Implicit Conversion
• Statements and Scope
A statement is a unit of executable code, usually terminated by a semicolon.
Code block includes a group of statements in enclosed in curly brackets.
Local variables: They are variables that are declared inside a code block, and they
are valid only inside the brackets.
{
{
• Operators
Operators can have single arguments, in which case they are known as unary
operators or two arguments, in which case they are known as binary operators
Name Meaning Example
+ Addition 34 + 1
– Subtraction 34.0 – 0.1
* Multiplication 300 * 30
/ Division 1.0 / 2.0
% Remainder 20 % 3
(Modulus)
6
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
Increment (++) and decrement (--) operators
They have two forms: postfix and prefix
The postfix returns the old value of the variable to which it is applied where
as the prefix form returns the new value.
The ++ operator increments the variable it is associated with by 1
The -- operator decrements the variable it is associated with by 1
• Operators Precedence
Precedence Operators Operations
1 () Parenthesis
* Multiplication
2 / Division
% Remainder
+ Addition
3
− Subtraction
4 = Assignment
Exercise
7
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
• Relational operators
Operator Name Example expression Meaning
== Equal to x == y true if x equals y, otherwise false
!= Not equal to x != y true if x is not equal to y, otherwise false
> Greater than x > y true if x is greater than y, otherwise false
< Less than x < y true if x is less than y, otherwise false
>= Greater than or equal to x > = y true if x is greater than or equal to y, otherwise false
<= Less than or equal to x <= y true if x is less than or equal to y, otherwise false
An expression involving a relational operator is a logical expression, so has a Boolean
value either true of false.
Exercise:
• Logical operators
Used when we want to combine the results from several logical expressions
The arguments for a logical operator must be logical expressions.
Operator Name Expression Meaning
&& And A && B Return true if both A and B are true, otherwise false
|| OR A || B Return false if both A and B are false, otherwise true
! NOT !A Return true if A is false, Return false if A is true
8
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
• if statement
if (logical-expression)
General form: {
statements;
If the logical expression within the parentheses evaluates to true,
then any statements in the following code block are executed.
If the logical expression is false, then any statements in the body
of the if statement are not executed
• if...else statement
General form: if (logical-expression)
statementsA;
else
statementsB;
statementsA are executed if the logical expression is true and statementsB are executed if
the expression is false.
• Nesting if statements
We can write if statements within if statements, which we call nesting
9
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
• switch … case statement
• while statement
• for statement
10
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
Meeting 1 Questions
1. Which of the following Java editions is used in developing smaller systems with limited
resources?
a. JVM
b. J2SE
c. J2EE
d. J2ME
2. Which of these Java editions is intended for large-scale systems?
a. JVM
b. J2SE
c. J2EE
d. J2ME
3. Java is ---------------
a. Interpreted
b. Object-oriented
c. Portable
d. All the above
e. A and B only
4. Java is ------------ since Java programs are strictly checked by software before they run.
a. Robust
b. Secure
c. Portable
d. Interpreted
e. Dynamic
5. When we say Java is ................ , it means that Java programs can run on different
platforms.
a. Secure
b. Robust
c. Threaded
d. Portable
6. Java is -------------- which means it can easily be transferred from one platform to run on
another platform with little or no change
a. Interpreted
b. Object-oriented
c. Robust
d. Portable
e. Secure
11
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
7. What is the value of y after running this code?
int x =(int) 6.6; double y = x/4 + 1.3;
a. 2.8
b. 2.95
c. 2.3
d. 2
8. The value of x after evaluating the following expression
double x = 20.0/2.0 + 5/4;
a. 10.0
b. 11.0
c. 15.0
d. 11
9. The value of x after evaluating the following expression
int x = (int) 9.0/2 % 2 + 5/3 * 5;
a. 1.0
b. 5.0
c. 5
d. 6
10. What is the value of the following expression? 2 + 8 / 5 * 3
a. 6
b. 5
c. 2
d. 0.666666666667
e. none of the above
11. Evaluate the following expression: 3 * 4 + (3 + 1) % 6
a. 2
b. 16
c. 8
d. 12
12. System.out.println((double) 20/4 * 2 % 5 – 9 ); will be evaluated to:
a. 9.0
b. -19.0
c. 19.0
d. -9.0
12
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
13. int x = 90; double y = 50.0; x = (int) y; the value of x is:
a. 50.0
b. 50
c. 90
d. 90.0
14. System.out.println(13%5%9+5-7); will be evaluated to:
a. 1
b. 2
c. 4
d. -9.0
15. Which of the following data types occupies the largest size in memory?
a. char
b. float
c. int
d. double
16. Which one of the following data types is a primitive data type in Java:
a. Array
b. StringBuffer
c. String
d. Long
17. When we say that Java allows a program to do several things at once this means that Java
is:
a. Secure
b. Interpreted
c. Portable
d. Threaded
18. Which of the following data types occupies the least amount of memory?
a. Byte
b. float
c. int
d. double
19. The java ……….. translate and execute java byte code
a. Interpreter
b. Disassembler
c. Compiler
d. Debugger
13
+965 50515237 : التلجرام هذه املذكرة لالستاذ خالد – التواصل فقط عرب
20. The java ------------- helps to look for errors in programs
a. Debugger
b. Compiler
c. Interpreter
d. Disassembler
21. Java is a strongly typed language which means
a. Every variable and expression may have two types
b. You can randomly assign values of one type to another
c. You can not randomly assign values of one type to another
d. A variable can contain different values at different times
14