You are on page 1of 11

Java …….…………...………Lecture1…….……….…… A.L.

Ali Hakem

Lecture outline:

1. Introduction to programming
2. Planning for the Computer Programs.
3. What is Java?
4. Structure of Java Program
5. Data type in Java
6. Variables in Java
7. Operators in Java
8. Output command in Java
9. Java Comments

Suggested Readings:

1. Liang, Y. Daniel. Introduction to Java programming: Introduction


to java programming. Pearson, 2015.

2. Malhotra, Sachin, and Saurabh Choudhary. Programming in


JAVA. Oxford University Press, Inc., 2011.

3. Pawlan, Monica. "Essentials of the Java Programming


Language." Sun Developers Network Tutorials & Code Camps
(1999).

1
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

1.1 Introduction to programming


‎ The computer is now an essential part of our daily life, and also an
important factor in science and technology. A computer system consists of
hardware (H/W) and software(S/W). The H/W is mechanical and electronic
devices that are capable of computing and manipulating information. The
software, i.e., programs that carry out predefined instruction to complete a
given task. Computer software communicates with the hardware to perform
useful tasks. The software organizes the control sequences, and the hardware
carries out the instructions defined by the software. The software can be
classified into two categories – system software and application software.
System software prepares computer programs that designing to control the
operations and extend the processing capability of a computer system, for
example, Microsoft Windows, macOS, Linux. Application software prepares
computer programs that designed to solve a specific problem or to do a
specific task such Microsoft Office, Winamp, Tomb Raider, etc.

The functions of programmers are translating the solutions or tasks


into a language the computer can understand. As we write programs, we
must keep in mind that the computer will only do what we instruct it to do.
Because of this, we must be very careful and thorough with our instructions.
The language programming can be classified into high-level language and
low-level language. A high-level language lets you write high level construct
to express abstract ideas, instead of low-level machine instructions.
However, computer hardware can only understand machine language,
therefore a high-level construct for low-level machine language translator, or
compiler, is needed. A high-level language can address memory cell by
names, rather than address. There exist more than 2500 programming

2
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

languages in the world some of them are known by only their developers
such as FORTRAN, C/C++, JAVA, Pascal, COBOL, ADA… etc.

1.2 Planning for the Computer Programs

There are some tools recommend using one of them before write
programs. Algorithm The tools are, Flowchart, and Pseudocode.

1.2.1 Algorithm

The name of algorithm derives from the name of mathematician


Mohammed ibn-Musa al-Khwarizmi. The algorithm refers to the logic of a
program and step by step description of how to arrive at the solution of given
problem. In order to qualify as an algorithm, a sequence of instructions must
have the following characteristic:
1- Each and every instruction should be precise and unambiguous.
2- Each instruction should be such that can be performed in finite
time.
Example1:
Write an algorithm to input a series of number and output their sum.
Solution:
1- Start.
2- Read numbers
3- Add a number to another (sum).
4- Print sum of input numbers.
5- End

Example2:
Write an algorithm to find the maximum and minimum number in the
list.
Solution:
1- Start.
2- Enter numbers.
3- Find maximum and minimum number.
4- Print maximum and minimum number.
3
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

5- End

1.2.2 Flowchart
The flowchart is a pictorial representation of an algorithm uses
symbols (boxes of different shapes) that have standardized meaning to
denote different types of instructions. The instructions within flowchart
are write in boxes. The boxes are connected by solid lines having arrow
marks to indicate the exact sequence in which the instructions are to be
executed. The process of drawing a flowchart for an algorithm called
flowcharting. Figure 1.1 shows the shapes (symbols) that use in flowchart

Figure 1.1 flowchart symbols

Example 3

Draw a flowchart to read two numbers x and y and print the


maximum one.

4
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

1.2.3 Pseudocode
Pseudocode is a detailed yet readable description of what a computer
program or algorithm must do, expressed in a formally-styled natural
language rather than in a programming language. Pseudocode is sometimes
used as a detailed step in the process of developing a program. It allows
designers or leads programmers to express the design in great detail and
provides programmers a detailed template for the next step of writing code in
a specific programming language.

1.3 What is Java?

Java is a high-level programming language originally developed by


company Sun Microsystems in 1995 and the creator is Jamesh Gosling. Java
runs on a variety of platforms, such as Windows, Mac OS, and the various
versions of UNIX. This tutorial gives a complete understanding of Java.

1.4 Structure of Java Program


Figure 1.2 explains the main structure of Java program.

5
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

Figure 1.2 Structure of Java program

1.5 Data type in Java


The major data in any language are numerical, textual, or logical, Java
language define these type of data under bellow classes:

1- The numerical data type either integer or floating point. The Integers
numbers are defined as a byte (8-bit), short(16-bit), int(32-bit), and
long(64-bit). The floating-point numbers are defined as float(32-bit)
and double(64-bit).
2- The textual data type either Characters or staring. The Characters are
defined as char. The string is defined as String.
3- Boolean is a special type for representing true/false values define as
Boolean
1.6 Variables in Java

The variable is the basic unit of storage in a Java program. A variable


is defined by the combination of an identifier, a type, and an optional
initializer. In addition, all variables have a scope, which defines their

6
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

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

Type Identifier;

The type is one of Java„s atomic types or the name of a class or interface.
The identifier is the name of the variable. You can initialize the variable by
specifying an equal sign and a value. To declare more than one variable of
the specified type, use a comma separated list.

Here are several examples of variable declarations of various types. Note that
some include an initialization

int a, b, c; // Note:-declares three integers, a, b, and c.


int d = 3, e, f = 5; // declares three more integers
byte z = 22; // Note:-initializes z is 22.
double pi = 3.14159; // Note:-declares an approximation of π.
char x = 'x'; // Note:-the variable x has the value 'x'.
String name =” Ali”; // Note:-the variable name has the value “Ali”

Note:

The identifiers must begin with a letter or an underscore can be following by


number(s) or later(s). Any other symbol, such as a number, is not valid.
Furthermore, an identifier cannot have a special symbol such as (+,-,/,..etc.)
and cannot be Java's reserved words. (For a list of keywords and literals
reserved for use as identifiers, see "3.9 Keywords" from The Java Language
Specification.).

7
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

1.7 Operators in Java


Java provides a rich set of operators to manipulate variables. We can
divide all the Java operators into the following groups:

1- Arithmetic Operators 2- Relational Operators


3- Logical Operators 4- Assignment Operator
1.7.1 Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the
same way that they are used in algebra. The following table lists the
arithmetic operators.
Example:
Assume integer variable A holds 10 and variable B holds 20

1.7.2 Relational Operators


There are following relational operators supported by Java language.

8
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

1.7.3 Logical Operators


The following table lists the logical operators. Assume Boolean
variables A holds true and variable B holds false.

1.7.4 Assignment Operators


Following are the assignment operators supported by Java
language

9
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

1.8 Output command in Java


The most basic output function is System.out.print(x), where x can be a
value or expression of any type.

Examples about output command in Java

System.out.print(x); // This command will print the value of x

String a= “Ali”;
System.out.print (“Name=” + a); // print the value of a
Display the result.
Name=Ali

11
Java …….…………...………Lecture1…….……….…… A.L. Ali Hakem

1.9 Java Comments

The java comments are statements that are not executed by the
compiler and interpreter. The comments can be used to provide information
or explanation about the variable, method, class or any statement. It can also
be used to hide program code for a specific time. There are two types of
comments in java Single Line Comment and multi-Line Comment.

1) Java Single Line Comment:


The single line comment is used to comment only one line after //.
Example

2) Java Multi-Line Comment


The multi-line comment is used to comment multiple lines of code
between /* and */.
Example

11

You might also like