Date post: | 12-Jan-2016 |
Category: |
Documents |
Upload: | corey-summers |
View: | 220 times |
Download: | 1 times |
Chapter 1 Introduction to
Java
04/21/23
1
Objectives:Part (1):•Programs.•The relationship between Java and the World Wide.•To know Java’s advantages .
•To write a simple Java program.•To create, compile, and run Java programs.
•To understand the Java runtime environment.•To know the basic syntax of a Java program.•To display output on the console and on the dialog box .
04/21/23Lecture 1
2
:ProgramsComputer programs, known as software, are
instructions to the computer. You tell a computer what to do through
programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them.
Programs are written using programming languages.
04/21/23Lecture 1
3
Programming Languages:Machine Language Assembly Language High-Level Language
04/21/23Lecture 1
4
Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this:
1101101010011010
Programming Languages:Machine Language Assembly Language High-Level Language
04/21/23Lecture 1
5
The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5:
area = 5 * 5 * 3.1415;
Programming Languages:Machine Language Assembly Language High-Level Language
04/21/23Lecture 1
6
Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3
… ADDF3 R1, R2, R3 …
Assembly Source File
Assembler
… 1101101010011010 …
Machine Code File
Popular High-Level Languages:• COBOL (COmmon Business Oriented Language)• FORTRAN (FORmula TRANslation) • BASIC (Beginner All-purpose Symbolic Instructional
Code) • Pascal (named for Blaise Pascal) • Ada (named for Ada Lovelace) • C (whose developer designed B first) • Visual Basic (Basic-like visual language developed by
Microsoft) • Delphi (Pascal-like visual language developed by
Borland) • C++ (an object-oriented language, based on C)• Java (We use it in this Semester )
04/21/23Lecture 1
7
Compiling Source Code:A program written in a high-level language is
called a source program. Since a computer cannot understand a source program. Program called a compiler is used to translate the source program into a machine language program called an object program. The object program is often then linked with other supporting library code before the object can be executed on the machine.
04/21/23Lecture 1
8
Compiler Source File Object File Linker Excutable File
Example for compiling source code in C :++
04/21/23Lecture 1
9
Compiling Java Source Code:You can port a source program to any machine with
appropriate compilers. The source program must be recompiled, however, because the object program can only run on a specific machine. Nowadays computers are networked to work together. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine, as shown in Figure 1.5. Java Virtual Machine is a software that interprets Java bytecode.
04/21/23Lecture 1
10
Java Bytecode
Java Virtual Machine
Any Computer
04/21/23Lecture 1
11
Example for compiling source code in Java:
04/21/23Lecture 1
12
Example for compiling source code in Java:
Object Oriented Programming(OOP):
Is a new method used in the programming as it works to analyze and design applications in the form of objects .. And contain data controlled by a set of processes.Example/ Java language.
Structured programming:
Is a technique that based on building the structure of the data without paying attention to the processes applied to the data.Example/ C++ language.
:Characteristics of Java•Java Is Simple• Java Is Object-Oriented•Java Is Distributed •Java Is Interpreted •Java Is Robust •Java Is Secure •Java Is Architecture-Neutral •Java Is Portable •Java's Performance •Java Is Multithreaded •Java Is Dynamic•Java Is Inheritances •Java Is Encapsulation
04/21/23Lecture 1
14
:Java IDE Tools
•Borland JBuilder•NetBeans Open Source by Sun •Sun ONE Studio by Sun MicroSystems
•Eclipse Open Source by IBM
04/21/23Lecture 1
15
:A Simple Java Program//This program prints Welcome to Java !
public class Welcome{ public static void main(String[] args)
{ System.out.println("Welcome to Java!");
} }
04/21/23Lecture 1
16
Creating, Compiling, and Running Programs:
04/21/23Lecture 1
17
Source Code
Create/Modify Source Code
Compile Source Code i.e., javac Welcome.java
Bytecode
Run Byteode i.e., java Welcome
Result
If compilation errors
If runtime errors or incorrect result
public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
… Method Welcome() 0 aload_0 … Method void main(java.lang.String[]) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return
Saved on the disk
stored on the disk
Source code (developed by the programmer)
Byte code (generated by the compiler for JVM to read and interpret, not for you to understand)
Trace a Program Execution:04/21/23Lecture 1
18
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}
Enter main method
:Trace a Program Execution04/21/23Lecture 1
19
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}
Execute statement
Trace a Program Execution:04/21/23Lecture 1
20
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}
print a message to the console
: content of a Java Program
•Comments•Statements•Blocks•Classes•Methods•The main method
04/21/23Lecture 1
21
:CommentsIn Java, comments are preceded by two slashes (//) in a line, or enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text
between /* and ./*
04/21/23Lecture 1
22
A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!" Every
statement in Java ends with a semicolon.(;)
Statements:
Blocks: 04/21/23Lecture 1
23
A pair of braces in a program forms a block that groups
components of a program.
public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Class block
Method block
Classes:Is the most important term in the object-oriented programming (OOP) .. It can be defined as a template or outline, which made him the objects .. The classification determines what data and methods that will be included in the object .. And classification is the basic encapsulation unit in the Java language.we can using one or more classes .
04/21/23Lecture 1
24
04/21/23Lecture 1
25
Object Is the case for the class.. Because it represents the real situation with the specifications contained in class.. This means that if the class of several objects is equal for each object, including its data separate.
Object:
Methods:Contains the code Executive for the class.. It is defined as Determines the behavior of objects .. The methods can be defined as the code that controls the data .
It is a term of actual work in Java and corresponding to a work function in C.++
04/21/23Lecture 1
26
main Method:The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method .
The main method looks like this:
public static void main(String[] args) {
// Statements;}
04/21/23Lecture 1
27
04/21/23Lecture 1
28
04/21/23lecture 2
29
Objectives:
To understand objects and classes and use classes to model objects
Part (2):•To understand Basic structure of the Java program.
•To use identifiers to name variables, constants, methods, and classes .•To use variables to store data.•To program with assignment statements and assignment expressions .•To use constants to store permanent data.•To declare Java primitive data types: byte, short, int, long, float, double, and char .•To use Java operators to write expressions.
•To understand output statement.•To write a simple Java program.
Fundamentals of programming structures in java:
04/21/23lecture 2
30
When you write a program in Java it is design or create a group of Classes , then objects are created , which takes the form of Classes .. The Java language includes a set of ready-made Classes , called ( Class Library ), and by which Classes can be created easily.Class Library :It is a set of ready-made Classes and can be used in various programs in Java.
Basic structure of the Java :program
04/21/23lecture 2
31
To deal with the classes in the Java language, it must know two important things:Declaring the Class.Body of Class.
Classes in java:
Declaring the Class:
04/21/23lecture 2
32
Modifiers Class Class name extends name of super class
حالة في نكتبهامن classالتوريث
classإلى فقط
Structural form of the declaring for Class:
1- Modifiers: they are contain of set of the Different types, It is the most famous:
public: Means that it can create any number of objects belonging to this class under any other class, whether this class in the same package or in any other package. Example: public class AAFinal: Means that the class does not contain subclasses , which can not use any of his properties by another class. Example: final class AAAbstract: Means that this class is a Super Class to a group of other classes so that they can be used for all variables and functions in the abstract class. Example: abstract class AA
2 – Class Name:Taken into account when choosing the name of the class as follows:
04/21/23lecture 2
33
Declaring the Class:
1- The name of letters and numbers. 2- The name must begin with a letter the rest of the name can be letters or numbers. 3- should not be the name of reserved words in the language. 4- Java language is case sensitive letters where distinguish between uppercase and lowercase letters. 5- Each statement in java must be finished with semicolon (;). 6- The code in java must be written between brackets {}.
3– Super Class:That is, it can be certain that the Class inherits and uses all the variables and functions of the Class has been built previously, called this characteristic feature of inheritance.
Example: public class AA extends BBيرث Class AA ال
المتغيرات جميعال في والوظائف
Class BB
Body of Class:
04/21/23lecture 2
34
Modifiers Class Class name extends name of super class
The class contain of a set of data (variables and constants ) and a set of methods, and the structural form of the contents of the class as follows:
Type _ variable 1;Type _ variable 2;Type _ variable 3;Type _ variable 4;Type method _name 1 (parameter_ list){Method1_ body;}Type method _name 2(parameter_ list){Method2_ body;}
Theclass body
}
{
Public static void main (string [ ] args)the java program
Body of Class:1 –Variables:
04/21/23lecture 2
35
Is a collection of variables that are declared within the brackets of the beginning and the end of the Class, These variables can be used by all the methods within the class.Note that the variables that are declared within the methodBe within the knowledge of this method are only, in this case the internal or local variables.access modifier:Each variable access modifier explains how to use this variable, they are as follows:
Public Protected Private Static FinalVariable visible to all the class and subclasses
in the same package and the external
package.
Ex: Public int aa
Variable is visible to all the
class and subclasses in the package
case only.
Ex: Protected int aa
Variable is visible to all
methods in the class basic, but invisible to any other Class or
any subclass of the class basic.
Ex: Private int aa
We can refer to this variable in
terms of the class directly
without resorting to
create the object.
Ex: static int aa
Means that the value of this variable can
not be changed after giving it
the initial value.
Ex: final int aa
04/21/23lecture 2
36
Body of Class: Primitive Types:Java has eight primitive types, each with its own purpose and use:1- boolean 5- int2- byte 6- long3- short 7- float4- char 8- double
Example :Public int R
Final int aa
Private float x
Rules for creating Identifiers (variables):There are several rules that must be obeyed when creating
identifier:1- The first character of in identifier must be letter , After that all
subsequent characters can be letters or numbers .2- should not be the variable of reserved words in the language.3- Java is case sensitive uppercase and lowercase letters are
different, so a1 and A1 are different identifiers.4- we use the underscores ( _ ) in the naming of variables.
04/21/23lecture 2
37
Is the constant value that does not change.
final datatype CONSTANTNAME = VALUE;
Example: final double PI = 3.14159; final int SIZE = 3;
2– constant:
Body of Class:
3 -Operations on constants and variables:
04/21/23lecture 2
38
Mathematical
operations
Description Example
= Assignment operator
=+ Add and assign operator i += 8 i = i+8
=- subtract and assign operator i -= 8 i=i-8
=* Multiply and assign operator i *= 8 i = i* 8
=/ divide and assign operator i /= 8 i = i / 8
=% modulus and assign operator i %= 8 i = i % 8
++variable Increase before the operation ++x
Variable++ Increase after the operation X++--Variable Decreases before the operation --x
Variable-- Decreases after the operation X--
Mathematical operations:
A set of mathematical and logical operations that can be applied to variables and constants in Java:
Body of Class:
04/21/23lecture 2
39
Logical operations:
Body of Class:
logical operations
Description
&& Logical AND
|| Logical OR
! Logical NOT
p !p
true false
false true
Example
!(1 > 2) is true, because (1 > 2) is false.
!(1 > 0) is false, because (1 > 0) is true.
p1 p2 p1 && p2
false false false
false true false
true false false
true true true
Example
(3 > 2) && (5 >= 5) is true, because (3 > 2) and (5 >= 5) are both true.
(3 > 2) && (5 > 5) is false, because (5 > 5) is false.
04/21/23lecture 2
40
Relational operations:
Relational operations
Description
> greater than
< less than
=> greater than or equal to
=< less than or equal to
= = equal to
=! Not equal to
p1 p2 p1 || p2
false false false
false true true
true false true
true true true
Example
(2 > 3) || (5 > 5) is false, because (2 > 3) and (5 > 5) are both false.
(3 > 2) || (5 > 5) is true, because (3 > 2) is true.
Body of Class:
04/21/23lecture 2
41
Is the basic syntax of the Java language program is directed directly to the compiler.
Public static void main (string [ ] args):
Output Statement:Is one of the types of methods in the Java and is considered among the basic printing in Java where you print the output of the program on the screen.
System.out.println( );
Example:System.out.println( “Name’’);System.out.println( nam);
04/21/23lecture 2
42
Class Sample_1{ public static void main (String [ ] args) { System.out.println(“Have a nice day”); }}
Simple program 1:
Output :
04/21/23lecture 2
43
Class Sample_2 { public static void main (String [ ] args) { int a1 = 12; int a2; System.out.println(“value of a1=“+ a1); a2= a1/2; System.out.println(“value of a2=“+ a2); }}
Simple program 1:
Output :