Introduction to Java_chapter1
Introduction to Java_chapter1
INTRODUCTION TO
COMPUTERS,
PROGRAMS, AND JAVA
Lecture notes for computer programming 1
Faculty of Engineering and Information Technology
Prepared by: Iyad Albayouk
Java, World Wide Web, and
Beyond
Java was developed by a team led by James
Gosling at Sun Microsystems.
Originally called Oak, it was designed in 1991 for
use in embedded chips in consumer electronic
appliances.
In 1995, renamed Java, it was redesigned for
developing Internet applications.
Java is simple, object oriented, distributed,
interpreted, robust, secure, architecture neutral,
portable, high performance, multithreaded, and
dynamic.
Java, World Wide Web, and
Beyond, cont.
Java initially became attractive because Java programs can be run
from a Web browser. Such programs are called applets.
FIGURE 1.1 A Java applet for playing TicTacToe is embedded in an HTML page.
Java, World Wide Web, and
Beyond, cont.
Java can also be used to develop applications on the server side.
These applications can be run from a Web server to generate dynamic Web
pages.
Welcome to Java!
Creating, Compiling, and Executing a
Java Program
• You save a Java program in a .java file and compile it into a
.class file. The .class file is executed by the Java Virtual Machine.
FIGURE 1.5 (a) Java source code is translated into bytecode. (b) Java bytecode
can be executed on any computer with a Java Virtual Machine.
Displaying Text in a Message Dialog Box
/* This application program displays Welcome to Java!
in a message dialog box.
*/
import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(null, x);
where x is a string for the text to be displayed.
Appropriate Comments
Naming Conventions
Proper Indentation and Spacing Lines
Block Styles
Appropriate Comments
Include a summary 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.
Constants:
Capitalize all letters in constants, and use
underscores to connect words. For example,
the constant PI and MAX_VALUE
Proper Indentation and Spacing
Indentation
Indent two spaces.
Spacing
Use blank line to separate segments of the
code.
Block Styles
Use end-of-line style for
braces.
Next-line public class Test
style {
public static void main(String[] args)
{
System.out.println("Block Styles");
}
}
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
Programming Errors
Syntax Errors
Detected by the compiler
Runtime Errors
Causes the program to abort
Logic Errors
Produces incorrect result
Syntax Errors
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
int i = 1 / 0;
}
}
Logic Errors
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree
");
System.out.println((9 / 5) * 35 + 32);
}
}