chapter 1.3
TRANSCRIPT
EXPLAINING THE PROGRAMMING PROCESS &
TYPES OF ERRORS
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
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
Java TranslationJava source
code (program)
Machinecode
Javabytecode
Bytecodeinterpreter
Javacompiler
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.
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
A DOS Command Window
An Integrated Development Environment
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 }
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
Java Program Structure
public class Hello{
}
// comments about the class
class header
class body
//Comments can be placed almost anywhere
Java Program Structure
public class MyProgram{
}
// comments about the class
public static void main (String[] args){
}
// comments about the method
method headermethod body
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
Processing a Java Program
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.
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
Errors
It is common for programmer to make mistake in a program.
Three kinds of errors Syntax errors Runtime errors Logic errors
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);
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
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");