Introduction to Java - Hacettepe Üniversitesiilyas/Courses/BBS... · C#: An object-oriented...

30
BBS514 Structured Programming (with Java) 1 Introduction to Java

Transcript of Introduction to Java - Hacettepe Üniversitesiilyas/Courses/BBS... · C#: An object-oriented...

BBS514 Structured Programming (with Java) 1

Introduction to Java

BBS514 Structured Programming (with Java) 2

Programming Languages and Compilers

• Computer programs, known as software, are instructions to the computer.

– We tell a computer what to do through programs.

– Without programs, a computer is an empty machine.

– Computers do not understand human languages, so we need to use computer

languages to communicate with them.

• A computer program is a set of instructions that enable the computer to solve a problem or perform a task.

– Collectively, these instructions form an algorithm

• Programs are written using programming languages

– A programming language is a special language used to write computer programs.

Programs

BBS514 Structured Programming (with Java) 3

• Machine language is a set of primitive instructions built into every computer.

• Each CPU has its own machine language.

• 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

• Early programmers wrote programs using machine languages

Programming LanguagesMachine Language

BBS514 Structured Programming (with Java) 4

• Assembly languages were developed to make programming easy.

• Assembly language instructions are English-like abbreviations representing

elementary computer operations corresponding machine language instructions. (

• Since the computer cannot understand assembly language, however, a program called

assembler is used to convert assembly language programs into machine code.

• Example:

LOAD BASEPAY

ADD OVERPAY

STORE GROSSPAY

Programming LanguagesAssembly Language

BBS514 Structured Programming (with Java) 5

• The high-level languages are English-like and easy to learn and program.

• High-level languages use mathematical notations.

• High-level languages are relatively easy to use

• Unfortunately, computer hardware does not understand high-level languages.

– Therefore, a high-level language program must be translated into a machine

language with a compiler.

Example:

grossPay = basePay + overTimePay;

Programming LanguagesHigh-Level Language

BBS514 Structured Programming (with Java) 6

Ada: Named for Ada Lovelace, who worked on mechanical general-purpose computers.

BASIC: Beginner’s All-purpose Symbolic Instruction Code.

C: Developed at Bell Laboratories. Combines the power of an assembly language with the

ease of use and portability of a high-level language.

C++: An object-oriented language, based on C

C#: An object-oriented programming language developed by Microsoft.

COBOL: COmmon Business Oriented Language.

FORTRAN: Popular for scientific and mathematical applications.

Java: Developed by Sun Microsystems, now part of Oracle. An object-oriented

programming language, widely used for developing platform-independent Internet

applications.

Pascal: A structured, general-purpose language primarily for teaching programming.

Python: A simple general-purpose scripting language good for writing short programs.

Popular High-Level Languages

BBS514 Structured Programming (with Java) 7

• A compiler translates a program from a high-level language to a low-level language

(mostly to a machine language) the computer can run.

• Most high-level languages need a different compiler for each type of computer and for

each operating system.

• Most compilers are very large programs that are expensive to produce.

• A program written in a high-level language is called a source program or source

code.

– Because a computer cannot understand a source program, a source program must

be translated into machine code for execution.

– The translation can be done using another programming tool called an interpreter

or a compiler.

Compilers

BBS514 Structured Programming (with Java) 8

• A compiler translates source code into a specific target language

• Typically, that target language is the machine language for a particular CPU type

What a typical compiler does

BBS514 Structured Programming (with Java) 9

• Since Java is platform independent, Java compiler do NOT produce machine code.

• Java introduces an intermediate language called bytecode

– Java bytecode is not the machine language for any traditional CPU, but a code for a virtual

machine

– Java compiler translates Java source code (.java files) into bytecode (in .class files)

– Byte code instructions are the machine language of the Java Virtual Machine (JVM) and

cannot be directly executed directly by the CPU.

Java Execution

– To execute a Java program, another piece of software called an interpreter, translates

between bytecode and the machine language

– an interpreter is specific to a specific machine language

– the interpreter understands java bytecode, and then issues instructions in the

machine language for which it is written

Java Compiler

BBS514 Structured Programming (with Java) 10

Java Translation and Execution

BBS514 Structured Programming (with Java) 11

Program Development Process

BBS514 Structured Programming (with Java) 12

• The answer is that Java enables users to develop and deploy applications on the

Internet for servers, desktop computers, and small hand-held devices.

• The future of computing is being profoundly influenced by the Internet, and Java

promises to remain a big part of that future.

• Java is the Internet programming language.

• Java is a general purpose programming language.

Why Java?

BBS514 Structured Programming (with Java) 13

Java Is Simple:

• Simplified version of C++ with fewer negative aspects.

Java Is Object-Oriented:

• Object-oriented programming (OOP) is a popular programming approach that is

replacing traditional procedural programming techniques.

Java Is Interpreted:

• You need an interpreter to run Java programs.

• The programs are compiled into the Java Virtual Machine code called bytecode.

• The bytecode is machine-independent and can run on any machine that has a Java

interpreter, which is part of the Java Virtual Machine (JVM).

Java Is Portable:

• Because Java is architecture neutral, Java programs are portable.

• They can be run on any platform without being recompiled.

Characteristics of Java

BBS514 Structured Programming (with Java) 14

BBS514 Structured Programming (with Java) 15

First Java Program

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

• This program prints to Welcome to Java! our screen when println method

is executed.

First Java Program

BBS514 Structured Programming (with Java) 16

• Every Java program must have at least one class.

• Each class has a name.

– By convention, class names start with an uppercase letter.

– In this example, the class name is Welcome.

• This class named Welcome must be in a file named Welcome.java

First Java Program

Class Name

BBS514 Structured Programming (with Java) 17

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

• In order to run a class, the class must contain a method named main.

• The program is executed from the main method.

First Java Program

Main Method

BBS514 Structured Programming (with Java) 18

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

• A statement represents an action or a sequence of actions.

• The statement System.out.println("Welcome to Java!") in the

program is a statement to display the greeting Welcome to Java!.

• System.out.println("Welcome to Java!") is a method invocation

statement.

First Java Program

Statement

BBS514 Structured Programming (with Java) 19

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

• Every statement in Java ends with a semicolon ; .

– Exception: Block statements { … } do not need semicolon to terminate them.

First Java Program

Statement Terminator

BBS514 Structured Programming (with Java) 20

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

• Keywords are words that have a specific meaning to the compiler and cannot be used

for other purposes in the program.

• For example, when the compiler sees the word class, it understands that the word

after class is the name for the class.

• Keywords in this program are: public class static void

First Java Program

Keywords

BBS514 Structured Programming (with Java) 21

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

• A pair of braces in a program forms a block that groups components of a program.

Class Block Method Block

First Java Program

Blocks

BBS514 Structured Programming (with Java) 22

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

First Java Program

Special Symbols

BBS514 Structured Programming (with Java) 23

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

• Include a summary (comment) at the beginning of the program to explain what the

program does, its key features, its supporting data structures, and any unique

techniques it uses.

• Include your name, date, and a brief description at the beginning of the program.

First Java Program

Appropriate Comments

BBS514 Structured Programming (with Java) 24

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Creating, Compiling, and Running Programs(in Command prompt)

BBS514 Structured Programming (with Java) 25

• Eclipse is a free popular integrated development environments for developing Java

programs.

Creating a Java Project

– Before creating Java programs in Eclipse,

you need to first create a project to hold all files.

1. Choose File, New, Java Project

to display the New Project wizard

2. Type demo in the Project name field.

3. Click Finish to create the Project.

Developing Java Programs Using Eclipse

BBS514 Structured Programming (with Java) 26

Creating a Java Class

• After a project is created, you can

create Java programs in the project.

1. Choose File, New, Class

to display the New Java Class wizard.

2. Type Welcome in the Name field.

3. Check the option

public static void main(String[] args).

4. Click Finish to generate the template

for the source code Welcome.java,

Developing Java Programs Using Eclipse

BBS514 Structured Programming (with Java) 27

Compiling and Running a Class

• To run the program, right-click the class in the project to display a context menu.

Choose Run,

• Java Application in the context menu to run the class.

Developing Java Programs Using Eclipse

BBS514 Structured Programming (with Java) 28

• Another simple Java program to compute a gross pay.

• It prints

Gross Pay is 132

Another Simple Java Program

BBS514 Structured Programming (with Java) 29

// Compute gross pay

public class ComputeExpression {

public static void main(String[] args) {

int hours;

int payRate;

int grossPay;

hours = 12;

payRate = 10;

grossPay = hours * (payRate+1);

System.out.println("Gross Pay is " + grossPay);

}

}

Another Simple Java Program

BBS514 Structured Programming (with Java) 30

// Compute gross pay

public class ComputeExpression {

public static void main(String[] args) {

int hours;

int payRate;

int grossPay;

hours = 12;

payRate = 10;

grossPay = hours * (payRate+1);

System.out.println("Gross Pay is " + grossPay);

}

}

Declaration statements

Assignment statements

here + is a string concatenation