Computer Programming Using
Java
1
Introduction
2
Overview
Computer programming
Machine language
Assembly language
High level languages
Compilers and interpreters
What is Java?
Advantages of Java
Java deliverables
Editions of Java
Your first Java program
Java Integrated Development Environment
3
The Basics:
What does a Computer Store and Process?
Electronic devices can represent 0’s and 1’s.
Binary world
Basic unit is 0 or 1 i.e. Binary Digit Bit
Combination of many bits are used to represent
and store information
00101001 can be used to represent:
Decimal Value 41
The Letter ‘A’
Command to perform “Add”
The meaning varies from one hardware design to
another
7
The Basics:
Telling a Computer What to Do
Programming: is telling the computer what to do.
Computers understand only 1’s and 0’s
A.K.A Machine Language
Machine language is the only language computers
know
To instruct a computer to do something, you need to do it
using the machine language for that computer.
Very difficult for “Humans” to deal with it.
10111010 00001100 00000001 10110100
00001001 11001101 00100001 10111000
00000000 01001100 11001101 00100001
01001000 01100101 01101100 01101100
01101111 00101100 01110111 01101111
01110010 01101100 01100100 00100001
00100100
8
The Basics:
Machine Language
Example:
A programs that display “Hello, world!” on the screen
10111010 00001100 00000001 BA 0C 01
10110100 00001001 B4 09
11001101 00100001 CD 21
10111000 00000000 01001100 B8 00 4C
11001101 00100001 CD 21
01001000 48
01100101 65
01101100
01101100
01101111
= 6C
6C
6F
00101100 2C
01110111 77
01101111 6F
01110010 72
01101100 6C
01100100 64
00100001 21
00100100 24
9
The Basics:
Programming Languages
Are needed so that humans a.k.a “Programmers” can
easily write/read programs.
A program written in any programming language MUST be
converted to machine language to be executed.
Assembly language
English-like abbreviations represent computer operations
Translator programs (assemblers) convert it to machine
language
High-level language
Allows for writing more “English-like” instructions
Compiler converts programs to machine language
Once compiled, can run on one platform only.
e.g. Programs made for PC can not run on Mac
Java avoids this problem
10
The Basics:
Programming Languages
Assembly Program:
HelloWorld.asm
Assembly Language
Specific to the processor
Assembler
(CPU)
The lowest level of
programming
Used in embedded systems Machine Code
(e.g. for Pentium)
The assembler converts the
program to machine code
execute
specific to a processor.
PC
(e.g. CPU is Pentium)
11
The Basics:
Assembly Language
Example:
A programs that display “Hello, world!” on the screen
Assembly Language Machine Code
in binary in HEX
10111010 00001100 00000001 BA 0C 01
org 100h 10110100 00001001 B4 09
11001101 00100001 CD 21
mov dx, msg 10111000 00000000 01001100 B8 00 4C
mov ah, 09 11001101 00100001 CD 21
01001000 48
int 21h 01100101 65
mov ax, 4c00h
int 21h
01101100
01101100
01101111
= 6C
6C
6F
00101100 2C
msg db "Hello,world!$" 01110111 77
01101111 6F
01110010 72
01101100 6C
01100100 64
This is specific to PC computers 00100001 21
i.e. can not be executed on a Mac 00100100 24
12
The Basics:
High-Level Languages
Allows for writing more “English-like”
instructions
Must be converted to machine language using a
compiler or an interpreter.
Compiler: reads the entire program before converting
it.
Interpreter: converts and executes one command at a
time.
Once compiled or interpreted, a program can run
only on one platform.
e.g. Programs made for PC can not run on Mac
13
The Basics:
High-Level Languages
Compilers vs. Interpreters
Compiler Interpreter
Operation Operation
Program Program
Interpreter
Reads the entire
program and read
compiler
program then Reads one
Go back to the
compiles it at line at a time.
once
a new line
Machine code
Machine code for one line
execute
execute
Windows In both cases, the code is executed Windows
PC PC
ONLY on a specific computer type.
14
The Basics:
High-Level Languages
Java is different…..
Same programs can be executed on different platforms
Java is PORTABLE
15
Java is Portable
Java Program:
HelloWorld.java
The Java Compiler
Java compiler
Reads files with extensions .java
Checks syntax/grammar
Creates a .class file which contains
byte code (machine code)
independent of any machine
Java Byte Code
Java Byte Code for Java Virtual Machine
Is portable: independent of any HelloWorld.class
machine
execute
The Java Virtual Machine (JVM) Windows Mac OS Linux
JVM
Translates byte code into JVM JVM
instructions for a particular
processor which will run the Windows Mac OS Linux
program
16
Java is Portable
C++ Program Java Program:
HelloWorld.cpp HelloWorld.java
Java compiler
compiler
Mac OS
Java Byte Code
Machine code Machine code Machine code for Java Virtual Machine
for windows for Mac OS for Linux HelloWorld.class
execute
execute
execute
execute
Windows Mac OS Linux
Windows Mac OS Linux JVM JVM JVM
Windows Mac OS Linux
17
What is Java?
A set of technologies for developing applications
across a wide variety of platforms.
Originally for intelligent consumer-electronic devices
Then used for creating web pages with dynamic
content
Now also used to:
Develop large-scale enterprise applications
Enhance web server functionality
Provide applications for consumer devices (cell
phones, etc.)
A development languages created to deal with
shortcomings in C and C++
Object-Based Programming Language
It is not JavaScript
18
Advantages of Java
Simple Object-oriented
Portable Distributed
Robust Secure
Interpreted Multithreaded
Dynamic
19
Most Common Deliverables of Java
Stand alone application
Standard software applications
Text-based applications
GUI applications
Applets
Applications that run from a Web browser
Servlets
Applications that run on Web servers to
create dynamic content.
20
Editions of Java
Java SE (J2SE)
Java 2 Standard Edition
currently at version 6.0
For desktop applications
Java ME (J2ME)
Java 2 Micro Edition
For gadgets such as smart phones
Java EE (J2EE)
Java 2 Enterprise Edition
For large scale applications that run on
multiple servers and used by large number of
users.
21
What is in J2SE?
JRE, Java Runtime Environment
This is the software that allows you to run
Java programs on your computer
Realization of the Java Virtual Machine (JVM)
Called either “the JRE” or “the runtime”
JDK, Java Development Kit
The is the software that allows you to create
and run Java applications on your computer
When you install JDK, you get JRE along with
it.
22
Java Development Kit (JDK)
Main Components
javac
Java compiler that is used to create Java Byte
Code
java
Java Interpreter that allows the Java Byte Code
to be executed on the platform where it (i.e.
java) runs.
javadoc
Used for documentation
appletviewer
Used to view applets
others
23
Let’s Get Started:
Your First Java Program
Compile and run the following program
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
1- Type the above code in a new Notepad file called HelloWorld.java and save it.
2- From the command prompt, compile your program by the following command:
javac HelloWorld.java
3- If there are no errors, run your program using:
java HelloWorld
Note: Make sure you name the source file with the same name of the
class i.e. HelloWorld.java
25
Understanding Your First Java Program
1: public class HelloWorld
The HelloWorld class 2: {
begins here with { 3: public static void main(String[] args)
4: {
The HelloWorld class 5: System.out.println("Hello, World!");
ends here with } 6: }
7: }
Java has building blocks that are used to create programs. A class is
one of the most important building blocks of Java. We will talk more
about it later.
Your entire program is defined as a class.
Line 1 declares the Java class that contains your program
o public: means that the element following it (i.e. class HelloWorld)
should be made available to other Java elements.
o class: means that the element being defined by this line is a class.
o HelloWorld: this is the name of the class being defined. This can be any
name you want but it must be the same name of the file containing it.
So, this line means: We are defining a class that is called HelloWorld
and should be made available to other Java elements.
27
Understanding Your First Java Program
1: public class HelloWorld
The main method 2: {
begins here with { 3: public static void main(String[] args)
4: {
The main method 5: System.out.println("Hello, World!");
ends here with } 6: }
7: }
Another building block of Java programs is called methods. Again,
we will talk more about this later.
Line 3 declares a method called main
o public: means that the method declared here should have public access.
This means Java classes other than HelloWorld can use it.
o static: We will talk about this later. For now, just take my word that the
Java language requires that you specify static when you declare the main
method.
o void: this means the main method does not return any values to the Java
element that called it.
o main: this is the name of the method being declared.
o (String[ ] args): This is the parameters list. We will explain it later.
Java programs (except applets) MUST have one method called main.
This is where program execution begins. 28
Understanding Your First Java Program
1: public class HelloWorld
2: {
3: public static void main(String[] args)
4: {
5: System.out.println("Hello, World!");
6: }
7: }
This is the actual code of your program.
Line 5 causes “Hello, World!” to be displayed on the screen.
It does that by sending the text to the output console of your computer
system, i.e. System.out object, and asks it to perform the method
called println which will print the text passed to it. i.e. the text that
is between “(“ and “)”
29
That was your first Text-Based Java program
Are you ready for your first
GUI Java program?
30
Let’s Get Started:
Your First GUI Java Program
Compile and run the following program
import javax.swing.JOptionPane;
public class HelloWorld
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,"Hello,World!"
,"Hello",JOptionPane.INFORMATION_MESSAGE);
}
}
Note: Make sure you name the source file with the same name of the
class i.e. HelloWorld.java
31
Are you ready for your first
Java Applet?
32
Let’s Get Started:
Your First Java Applet
1- Compile the following program
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello, World!", 50, 25);
}
}
Note: Make sure you name the source file with the same name of the
class i.e. HelloWorldApplet.java
Did you notice that applets do not have a main method?
33
Let’s Get Started:
Your First Java Applet
2- Open the Notepad and create the following HTML file
Hello.html
<HTML>
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my Java Applet:
<APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
3- Save Hello.html
4- Go to the folder where Hello.html file exist and
double click on it.
34
Integrated Development Environment (IDE)
Java Integrated Development Environment
(IDE) is a software that makes it easy to:
Write Java programs
Compile them
Run and test the programs
Examples of Java IDEs
JBiulder
JCreator
Eclipse
netBeans
BlueJ
35
What Did We Learn Today?
Computer programming Languages
Machine language
Assembly language
High level languages
Compilers and interpreters
Java 101
What is Java?
Advantages of Java
Java deliverables
Editions of Java
Downloading and installing Java
Your first Java program (Text, GUI, and Applet)
Java Integrated Development Environment
36
Questions ?
38