+ All Categories
Home > Documents > Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

Date post: 12-Jan-2016
Category:
Upload: corey-summers
View: 220 times
Download: 1 times
Share this document with a friend
43
Chapter 1 Introduction to Java 04/28/22 1
Transcript
Page 1: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

Chapter 1 Introduction to

Java

04/21/23

1

Page 2: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 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

Page 3: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

: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

Page 4: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 5: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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;

Page 6: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 7: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 8: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 9: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

Example for compiling source code in C :++

04/21/23Lecture 1

9

Page 10: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 11: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

04/21/23Lecture 1

11

Example for compiling source code in Java:

Page 12: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

04/21/23Lecture 1

12

Example for compiling source code in Java:

Page 13: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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.

Page 14: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

: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

Page 15: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

: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

Page 16: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

: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

Page 17: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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)

Page 18: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 19: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

: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

Page 20: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 21: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

: content of a Java Program

•Comments•Statements•Blocks•Classes•Methods•The main method

04/21/23Lecture 1

21

Page 22: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

: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:

Page 23: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 24: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 25: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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:

Page 26: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 27: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 28: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

04/21/23Lecture 1

28

Page 29: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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.

Page 30: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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.

Page 31: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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:

Page 32: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 33: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 34: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 35: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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

Page 36: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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.

Page 37: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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:

Page 38: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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:

Page 39: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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.

Page 40: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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:

Page 41: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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);

Page 42: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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 :

Page 43: Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

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 :


Recommended