chapter 1.3

20
EXPLAINING THE PROGRAMMING PROCESS & TYPES OF ERRORS Chapter 1.3:

Upload: sotlsoc

Post on 26-May-2015

890 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Chapter 1.3

EXPLAINING THE PROGRAMMING PROCESS &

TYPES OF ERRORS

Chapter 1.3:

Page 2: Chapter 1.3

The Java Programming Language Created by Sun Microsystems, Inc. introduced in 1995 and it's popularity has grown

quickly since Rich library Platform-independent ("write once, run anywhere")

or architecture-neutral

Page 3: Chapter 1.3

Java Translation The Java compiler translates Java source code

into a special representation called bytecode Java bytecode is not the machine language for

any traditional CPU Another Java software tool, called an interpreter

(or Java Virtual Machine (JVM)) , translates bytecode into machine language and executes it

Page 4: Chapter 1.3

Java TranslationJava source

code (program)

Machinecode

Javabytecode

Bytecodeinterpreter

Javacompiler

Page 5: Chapter 1.3

Portability After compiling a Java program into byte-code,

that byte-code can be used on any computer with a byte-code interpreter and without a need to recompile.

Byte-code can be sent over the Internet and used anywhere in the world.

This makes Java suitable for Internet applications.

Page 6: Chapter 1.3

Becoming Familiar with your Computer to use Java Understand files and folders/directories Locate the Java compiler/ Install J2SE Set path & Java class path Write a simple program (later) Save your work Compile & run Use Dos Command Prompt or IDE

Page 7: Chapter 1.3

A DOS Command Window

Page 8: Chapter 1.3

An Integrated Development Environment

Page 9: Chapter 1.3

File Hello.java

1 public class Hello

2 {

3 public static void main(String[] args)

4 {

5 // display a greeting in the console window

6 System.out.println("Hello, World!");

7 }

8 }

Page 10: Chapter 1.3

Java Program Elements A Java program is made up of class definitions. A class definition must contains a header and a

body. A class contains zero or more methods A method is a named section of code that also has

a header & body A method contains program statements

Single-line (starts with //) and multi-line (enclosed by /* and */) comments are used to document the code

Page 11: Chapter 1.3

Java Program Structure

public class Hello{

}

// comments about the class

class header

class body

//Comments can be placed almost anywhere

Page 12: Chapter 1.3

Java Program Structure

public class MyProgram{

}

// comments about the class

public static void main (String[] args){

}

// comments about the method

method headermethod body

Page 13: Chapter 1.3

Compiling and Running Type program into text editor Save (file name must be similar to class name) Open Dos Window Change directory to saved file directory Compile into byte codes

javac Hello.java Execute byte codes

java Hello

Page 14: Chapter 1.3

Processing a Java Program

Page 15: Chapter 1.3

Class Loader

A Java program typically consists of several pieces called classes.

Each class may have a separate author and each is compiled (translated into byte-code) separately.

A class loader (called a linker in other programming languages) automatically connects the classes together and loads the compiled code (bytecode) into main memory.

Page 16: Chapter 1.3

Creating a Java Program…

JVM

Create/modify source code

Source code

Compile source code

Byte code

Run byte code

Output

Syntax errors

Runtime errors or

incorrect results

Page 17: Chapter 1.3

Errors

It is common for programmer to make mistake in a program.

Three kinds of errors Syntax errors Runtime errors Logic errors

Page 18: Chapter 1.3

Syntax Errors

Grammatical mistakes in a program The grammatical rules for writing a program are very strict

The compiler catches syntax errors and prints an error message.

Example: using a period where a program expects a semicolon System.out.print("..."), System.out.print("Hello);

Page 19: Chapter 1.3

Runtime Errors

Errors that are detected when your program is running, but not during compilation

When the computer detects an error, it terminates the program and prints an error message.

Example: attempting to divide by 0

Page 20: Chapter 1.3

Logic Errors

Errors that are not detected during compilation or while running, but which cause the program to produce incorrect results

Example: an attempt to calculate a Fahrenheit temperature from a Celsius temperature by multiplying by 9/5 and adding 23 instead of 32

E.g System.out.print("Hell");