Professional Documents
Culture Documents
Object-Oriented Programming (2) Oop2: Variables, Constants and Built-In Data Types Java Program Structure
Object-Oriented Programming (2) Oop2: Variables, Constants and Built-In Data Types Java Program Structure
Object-Oriented Programming (2) Oop2: Variables, Constants and Built-In Data Types Java Program Structure
Introduction Variables, Constants and Built-in Data Types Java Program Structure
Outline
What a computer is What a computer program is The Programmers Algorithm How a program that you write in Java is changed into a form that your computer can understand Characteristics of Java Variables, Constants and Built-in Data Types Java Program Structure
Page 2 OOP2
What Is a Computer?
Computer Executes statements (computations/logical decisions) Hardware :Physical devices of computer system Software: Programs that run on computers
Central Processing Unit
Memory Unit
My data
My Progam
Page 3
OOP2
Computer Organization
Six logical units of computer system Input unit (Mouse, keyboard) Output unit (Printer, monitor, audio speakers) Memory unit (Retains input and processed information) Central processing unit (CPU) which consists of:
Control unit (Supervises operation of other devices) Arithmetic and logic unit (ALU) (Performs calculations)
tasks (i.e. print what grade a student got on an exam), it must be given instructions to do the task.
computer program
Page 5
OOP2
Page 6
OOP2
The programmers algorithm: Define the problem Plan the problem solution Code the program. Compile the program. Run the program. Test and debug the program.
Page 7 OOP2
Page 8
OOP2
Page 9
OOP2
Radius PI Processing Area = PI * Radius * Radius Perimeter = 2 * PI * Radius Output Area Perimeter
Page 10
OOP2
Page 11
OOP2
Programming Language : A set of rules, symbols and special words used to write statements.
steps into a programming language statements. The written program is called Source code and it is saved in a file with .java extension.
Algorithm Pseudocode Translating Source Code (The .java)
OOP2
Coding
Program
Page 12
written in programming languages such as C++ and Java Programs must first be converted into machine code that the computer can run A Software that translates a programming language statements into machine code is called a compiler
Program Compiling Source code Translating
Translates the source code into a machine code if no errors were found.
Page 14
OOP2
architectures along with the operating systems (Windows, Macs, Unix), require different m a c h in e c o d e , y o u m u s t c o m p i l e m o s t programs separately for each platform .
Page 15
OOP2
file) not machine code from the source code (the Java Interpreter
Source Code
Bytecode
Page 16
OOP2
Hello.java
Hello.class
Page 17
OOP2
Page 18
OOP2
Running
Bytecode Verifier
Hardware
Page 19
OOP2
Bytecode Verifier
ensures bytecodes do not violate security requirements
Bytecode Interpreter
translates bytecodes into machine language
Page 20
OOP2
Page 21
OOP2
Phase 1
Editor
Disk
Program is created in an editor and stored on disk in a file ending with .java.
Phase 2
Compiler
Compiler creates bytecodes and stores them on disk in a file ending with .class.
Phase 3
Class Loader
Disk
Class loader reads .class files containing bytecodes from disk and puts those bytecodes in memory. .. . .. .
Primary Memory
Phase 4
Bytecode Verifier
Bytecode verifier confirms that all bytecodes are valid and do not violate Javas security restrictions. .. . .. .
Primary Memory
Phase 5
Interpreter
Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes.
.. . .. .
Page 23
OOP2
Platform-independent
Portable Architecture neutral Write-once, run-anywhere
Secure
The bytecode verifier of the JVM :
checks untrusted bytecode controls the permissions for high level actions.
Page 24 OOP2
types for numerical data. Write arithmetic expressions in Java. Program Structure.
Page 25
OOP2
Keyboard
Screen
The data to be processed is stored in a temporary storage in the computer's memory: space memory.
Identifier Data Type State
Page 26
OOP2
Cannot be changed.
In this case the space memory is called constant.
Page 27
OOP2
Identifier Rules
It cannot begin with a digit (0 9).
It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. No spaces or punctuation, except the underscore symbol, _, are allowed.
Page 28 OOP2
Page 30
OOP2
Data Type
The data type defines what kinds of values a space memory is allowed to store. All values stored in the same space memory should be of the same data type. All constants and variables used in a Java program must be defined prior to their use in the program.
Page 31
OOP2
Numeric
Character
Boolean
Integer
Floating-point
char
String
boolean
float double
Page 32
OOP2
float double
32 bits 64 bits
Page 33
Page 34
OOP2
Variable/Constant Declaration
When the declaration is made, memory space
is allocated to store the values of the declared variable or constant. The declaration of a variable means allocating a space memory which state (value) may change. The declaration of a constant means allocating a space memory which state (value) cannot change.
Page 35 OOP2
Constant Declaration
final dataType constIdentifier = literal | expression; final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060;
literals.
expression.
Page 36
OOP2
Variable Declaration
A variable may be declared: With initial value. Without initial value. Variable declaration with initial value;
dataType variableIdentifier = literal | expression;
Salam Program
// import section: Empty public class MySalamProgram {
// main method public static void main( String args[] ){ // Declaration section: Empty // Input section: Empty // Processing section: Empty
} // end class
Page 39 OOP2
Page 40
OOP2
and aid reading your code. The Java compiler ignores them. Comments are made using
//, which comments to the end of the line, or /* */, everything inside of it is considered a comment (including multiple lines). The comment begins after the first /*. It ends just before the first */.
Examples: /* This comment begins at this line. This line is included in this comment It ends at this line. */
// This comment starts here and ends at the end of this line.
Page 41 OOP2