1/16
Java Programming
Prof. Moheb Ramzy Girgis
Department of Computer Science Faculty of Science
Minia University
1. Introduction to Programs and Java
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Programs
Computer
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.
2/16
Programming Languages
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
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Machine Language Assembly Language High-Level Language
Programming Languages
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
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
3/16
Programming Languages
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;
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Machine Language Assembly Language High-Level Language
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
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)
C# (developed by Microsoft for developing applications
based on the Microsoft .NET platform)
Java (developed by Sun Microsystems)
Popular High-Level Languages
Java Programming - Prof. Moheb Ramzy Girgis
Dept. of Computer Science - Faculty of Science Minia University
4/16
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 then linked with other supporting
library code to form an
executable file
, which can be
run (executed) on the machine.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Why Java?
Java is a general purpose programming language.
It 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 programs can be run from a Web browser. Such
programs are called applets. Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons, and so on, to interact with users on the Web and process their requests.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
5/16
Why Java? (Continued)
Java can be used to develop Web applications.
Java can also be used to develop applications for
hand-held devices such as cell phones.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Java Development Tools
Java comes in three editions: Java Standard Edition (Java
SE), Java Enterprise Edition (Java EE), and Java Micro Edition (Java ME).
Java SE can be used to develop client-side standalone
applications or applets.
Java EE can be used to develop server-side applications,
such as Java servlets and JavaServer Pages.
Java ME can be used to develop applications for mobile
devices, such as cell phones.
Java Development Tools (Continued)
In this course we will use Java SE to introduce Java
programming.
There are many versions of Java SE. The latest is Java SE 8. Sun releases each version with a Java Development Toolkit
(JDK). For Java SE 8, the Java Development Toolkit is called JDK 1.8 (JDK 8).
JDK consists of a set of separate programs, each invoked
from a command line, for developing and testing Java programs.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
6/16
Besides JDK, you can use a Java development tool (e.g., NetBeans, Eclipse, and TextPad)—software that provides an integrated development environment (IDE) for rapidly developing Java programs.
Editing, compiling, building, debugging, and online help
are integrated in one graphical user interface. Just enter source code in one window or open an existing file in a window, then click a button, menu item, or function key to compile and run the program.
In this course we will use NetBeans (Open Source by Sun).
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Java Development Tools (Continued)
A Simple Java Program
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Line 1 defines a class. 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.
Line 2 defines the main method. In order to run a class, the
class must contain a method named main. The program is executed from the main method.
Output
7/16
A Simple Java Program (Continued)
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Line 3 is a comment that documents what the program is and
how it is constructed. Comments help programmers to communicate and understand the program.
In Java, comments are preceded by two slashes (//) on a line,
called a line comment, or enclosed between /* and */ on one or several lines, called a block comment.
A block groups the program’s components. In Java, each
block begins with an opening brace ({) and ends with a closing brace (}).
Every class has a class block that groups the data and
methods of the class. Every method has a method block that groups the statements in the method.
Blocks can be nested, meaning that one block can be placed within another.
Anatomy of a Java Program
Comments
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The
main
method
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
8/16
Reserved Words
Reserved words or 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.
Other reserved words in Listing 1.1 are
public
,
static
,
and
void
. Their use will be introduced later.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Modifiers
Java uses certain reserved words called
modifiers
that specify the properties of the data, methods,
and classes and how they can be used.
Examples of modifiers are
public
and
static
. Other
modifiers are
private
,
final
,
abstract
, and
protected
.
A public datum, method, or class can be accessed
by other programs.
A private datum or method cannot be accessed by
other programs.
Modifiers are discussed later.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
9/16
Statements
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 (;).
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Classes
The class is the essential Java construct. A class is a template or blueprint for objects.
To program in Java, you must understand classes and be
able to write and use them.
The mystery of the class will continue to be unveiled
throughout this course. For now, though, understand that a program is defined by using one or more classes.
Methods
What is System.out.println?
It is a method: a collection of statements that
performs a sequence of operations to display a
message on the console.
It can be used even without fully understanding the
details of how it works.
It is used by invoking a statement with a string
argument. The string argument is enclosed within
parentheses. In this case, the argument is
"Welcome to Java!“.
You can call the same println method with a different
argument to print a different message.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
10/16
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;
}
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Programs
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Welcome.java
11/16
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.
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 below.
Java Virtual Machine is a software that interprets Java
bytecode.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
from the Command Window
Set path to JDK bin directory
Set Path=C:\Program Files\Java\jdk1.8.0_60\bin
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
On Microsoft Windows s
et the PATH variable as follows: Click Start, then Control Panel, then System. Click Advanced, then Environment Variables.
Add the location of the bin folder of the JDK installation
C:\Program Files\Java\jdk1.8.0_60\bin for the PATH variable in System Variables.
12/16
Creating, Compiling, and Running Java
from the Command Window
Create a source file
that will contain the Java code.
You can use any text editor, such as Notepad, to
create and edit source files.
Compile the source file:
javac Welcome.java
Run
the bytecode:
java Welcome
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
I. Create an IDE Project
To create an IDE project:
1.
Launch the NetBeans IDE.
2.
In the NetBeans IDE, choose File | New Project....
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE
13/16
3. In the New Project wizard, expand the Java category and select Java Application as shown in the following figure:
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE (Continued)
4. In the Name and Location page of the wizard, do the following (as shown in the figure below):
In the Project
Name field,
type
Welcome
.
In the Create
Main Class
field, the name
welcome.Welcome
will appear.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE (Continued)
14/16 5. Click Finish.
The project is created and opened in the IDE. You should see the following components:
The Projects window, which contains a tree view of the
components of the project, including source files, libraries that your code depends on, and so on.
The Source Editor window with a file called
Welcome.java open.
The Navigator window, which you can use to quickly
navigate between elements within the selected class.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE (Continued)
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE (Continued)
15/16
II. Add Code to the Generated Source File
When you created this project, you left the Create Main
Class checkbox selected in the New Project wizard. The
IDE has therefore created a skeleton class for you. You can add the code for displaying the "Welcome to Java!" message to the skeleton code by replacing the line:
// TODO code application logic here with the line:
System.out.println("Welcome to Java!");
III. Save your changes by choosing File | Save.
IV. Compile the Source File into a .class File
Choose Run | Build Project (Welcome) from the IDE's
main menu.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE (Continued)
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE (Continued)
The Output window opens and displays output similar to
16/16
If the build output concludes with the statement BUILD
SUCCESSFUL, congratulations! You have successfully
compiled your program!
If the build output concludes with the statement BUILD
FAILED, you probably have a syntax error in your code. Errors are reported in the Output window as hyperlinked text. You double-click such a hyperlink to navigate to the source of an error. You can then fix the error and once again choose Run | Build Project.
When you build the project, the bytecode file
Welcome.class is generated.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University
Creating, Compiling, and Running Java
using the NetBeans IDE (Continued)
V. Run the Program
From the IDE's menu bar, choose:
Run | Run Project (Welcome).
The next figure shows what you should now see.
Java Programming - Prof. Moheb Ramzy Girgis Dept. of Computer Science - Faculty of Science
Minia University