Download Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction
Outline #313#2
Programming Languages
Java Programming Language
First Java program
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
4/61
1
Memory
•
•
Computer programs are stored in hard disks
When a computer program is to be run, executable code
is to be loaded into the main memory
Memory address
Memory content
.
.
.
.
.
.
2000
01001010
Encoding for character ‘J’
2001
01100001
Encoding for character ‘a’
2002
01110110
Encoding for character ‘v’
2003
01100001
Encoding for character ‘a’
2004
00000011
Encoding for number 3
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
5/61
Input & Output devices
•
•
They are the interface between the Computer and the user
(Common) input devices:
§ Keyboard
§ Mouse
•
(Common) output devices:
§ Monitor
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
6/61
2
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 a useless
machine.
• Computers do not understand human languages,
so we need an interface between users and the
computers
• The interface is the programming language
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
7/61
Programming Languages
• There are three important classification of
programming languages
1. Machine Language
2. Assembly Language
3. High-level Language
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
8/61
3
1. Machine Language
•
•
•
•
•
•
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.
For example, to add two numbers, you might
write an instruction like this:
1101101010011010
Program with native machine language is a
tedious process.
Moreover machine codes are highly difficult to
read and modify.
First programs were written in machine language
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
9/61
2. Assembly Language
•
•
•
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
...
...
ADDF3 R1, R2, R3
Assembler
...
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
1101101010011010
...
10/61
4
3. High Level Language
•
•
•
To make programming easier, high-level
languages has been developed
High-level languages are English-like and easy
to learn and program
For example, to add two numbers, the following
statement in a high-level language can be used:
a = b + c;
...
...
a = b + c
Compiler
...
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
ADDF3 R1, R2, R3
...
11/61
Program Development
• The mechanics of developing a program include
several activities
§ writing the program in a specific programming language
(such as Java)
§ translating the program into a form that the computer can
execute
§ investigating and fixing various types of errors that can
occur
• Software tools can be used to help with all parts of
this process
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
12/61
5
Program Development
Edit and
save program
errors
errors
Compile program
Execute program and
evaluate results
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
13/61
Compiling a program
• Each type of CPU executes only a particular
machine language
• A program must be translated into machine
language before it can be executed
• A compiler is a software tool which translates
source code of a high language into codes of a
specific target language
• Often, that target language is the machine
language for a particular CPU type
• The Java approach is somewhat different
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
14/61
6
Generating machine code
• Compiler processes the high-level program to
create the machine code which is dependent on
the hardware of the computer to be executed
High level program
Compiler
Instructions
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
15/61
Popular High Level Languages
•
•
•
•
•
•
•
Java
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)
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
16/61
7
Outline
Programming Languages
Java Programming Language
First Java program
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
17/61
Java
• Java programming language was created by Sun
Microsystems, Inc.
• It was introduced in 1995 and it's popularity has
grown quickly since
• http://java.sun.com
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
18/61
8
Why Java?
• Java enables users to deploy applications on:
§ the servers to serve to the internet
§ the desktop computers
§ and small hand-held devices
• The future of computing will continue to be
influenced by the Internet and hand-held devices,
and Java promises to remain a big part of those
future.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
19/61
Java, Web, and Beyond
1. Java can be used on computers to develop:
1. Regular, GUI and Database Applications
2. Java Applets
3. Java Servlets and JavaServer Pages
2. Java can also be used to develop applications
for hand-held devices such as Palm and cell
phones
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
20/61
9
Java envorinments
•
Java SE : Standard Edition
§ can be used to develop client-side standalone
applications or applets.
•
Java EE : Enterprise Edition
§ can be used to develop server-side applications such
as Java Servlets, Java ServerPages, or Java Server
Faces.
•
Java ME : Middlet Edition
§ can be used to develop applications for mobile
devices such as cell phones, personal digital
assistants (PDAs) etc.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
21/61
Java SE Version
Last available version:
• Java SE 6 Update 3
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
22/61
10
Characteristics of Java
•
•
•
•
•
•
•
•
•
•
•
Java Is Simple
Java Is Object-Oriented
Java Supports Internet
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Platform Independent
Java Is Portable
Moderate Performance
Java Is Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
23/61
Characteristics of Java
• Simple
• Object-Oriented
• Distributed Computing
• Interpreted
• Robust
• Secure
• Platform Independent
• Portable
• ModeratePerformance
• Multithreaded
Java is partially
modeled on C++, but
greatly simplified and
improved.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
24/61
11
Characteristics of Java
• Simple
• Java was designed to be
object-oriented from the
• Object-Oriented
start.
• Supports Internet
• Distributed Computing • One of the central issues in
software development is
• Interpreted
how to reuse code.
• Robust
• Object-oriented
• Secure
programming provides
• Platform Independent
great flexibility, modularity,
clarity, and reusability
• Portable
• Moderate Performance through encapsulation,
inheritance, and
• Multithreaded
polymorphism.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
25/61
Characteristics of Java
• Simple
• Java enables people to
develop programs that use
• Object-Oriented
the Internet and World• Supports Internet
Wide Web.
• Distributed Computing
• Java Applets can easily be
• Interpreted
invoked from web browsers
• Robust
such as Internet Explorer
• Secure
• Platform Independent
• Portable
• Moderate Performance
• Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
26/61
12
Characteristics of Java
• Simple
• Distributed computing
involves several
• Object-Oriented
computers working
• Supports Internet
together on a network.
• Distributed Computing • Java is designed to make
• Interpreted
distributed computing
• Robust
easy.
•
Since networking
• Secure
capability is inherently
• Platform Independent
integrated into Java,
• Portable
writing network programs
• Moderate Performance
is like sending and
• Multithreaded
receiving data to and from
a file.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
27/61
Characteristics of Java
• Simple
• You need an interpreter to
run Java programs.
• Object-Oriented
•
The programs are
• Supports Internet
compiled into the Java
• Distributed Computing
Virtual Machine code
• Interpreted
called bytecode.
• Robust
• The bytecode is machineindependent and can run
• Secure
on any machine that has a
• Platform Independent
Java interpreter, which is
• Portable
part of the Java Virtual
• Moderate Performance
Machine (JVM).
• Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
28/61
13
Characteristics of Java
• Simple
• Java compilers can detect
many problems before
• Object-Oriented
running, that would first
• Supports Internet
show up at execution time
• Distributed Computing
in other languages.
• Interpreted
• Java has eliminated
• Robust
certain types of error• Secure
prone programming
constructs found in other
• Platform Independent
languages.
• Portable
• Moderate Performance • Java has a runtime
exception-handling feature
• Multithreaded
to provide programming
support for robustness.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
29/61
Characteristics of Java
• Simple
• Java implements several
security mechanisms to
• Object-Oriented
protect your system
• Supports Internet
against harm caused by
• Distributed Computing
malicious programs.
• Interpreted
• Robust
• Secure
• Platform Independent
• Portable
• Moderate Performance
• Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
30/61
14
Characteristics of Java
• Simple
• Write once, run anywhere
• Object-Oriented
• Java bytecode can be run
on any platform which has
• Supports Internet
Java Virtual Machine
• Distributed Computing
(JVM).
• Interpreted
• Robust
• Secure
• Platform Independent
• Portable
• Moderate Performance
• Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
31/61
Characteristics of Java
• Simple
• Because Java is platform
independent, Java
• Object-Oriented
programs are portable.
• Supports Internet
• They can be run on any
• Distributed Computing
platform without being
• Interpreted
recompiled.
• Robust
• Secure
• Platform Independent
• Portable
• Moderate Performance
• Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
32/61
15
Characteristics of Java
• Simple
• The execution of Java
bytecode is never as fast
• Object-Oriented
as machine language
• Supports Internet
code.
• Distributed Computing
• Interpreted
• Robust
• Secure
• Platform Independent
• Portable
• Moderate Performance
• Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
33/61
Characteristics of Java
• Simple
• Multithread programming
is smoothly integrated in
• Object-Oriented
Java, whereas in other
• Supports Internet
languages you have to call
• Distributed Computing
procedures specific to the
• Interpreted
operating system to
• Robust
enable multithreading.
• Secure
• Platform Independent
• Portable
• Moderate Performance
• Multithreaded
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
34/61
16
Java Program Types
•
In the Java programming language:
§ A program is made up of one or more classes
§ A class contains one or more methods
•
Two types of Java programs:
§ Application always contains a method called main and to be run
as a self-executable file. Application Programs may be either:
• Console application which uses DOS prompt in Windows
environmenti or similar interface in other operating systems
• GUI, Graphical User Interface program which creates a graphical
interface
§ Applet does not contain main(), instead it contains many other
methods. Applet can not run by itself, and to be run within a
web browser
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
35/61
Java Program Types
uses:
main()
Java Programs
Stand-alone
Applications
Applets
viewed in:
.html
Console
Applications
GUI
Program
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
Graphical
programs
36/61
17
Java Program Types
Java Programs
Console
Applications
uses:
main()
Graphical
Applications
GUI
Program
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
Applet
37/61
Outline
Programming Languages
Java Programming Language
Downloading the Software
First Java program
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
38/61
18
Downloading Java Compiler
• In order to build a Java program, you need to
have:
§ Java compiler to compile the program
§ Java Virtual Machine (JVM) to run the program
• In this course, we will edit the program using
notepad, and compile & run using DOS prompt
• You can use a virtual program like eclipse, but I
don’t suggest using them in this course
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
39/61
Java SE download
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
40/61
19
Java SE download
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
41/61
Java SE download
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
42/61
20
Java SE download
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
43/61
Java SE download
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
44/61
21
Java SE download
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
45/61
Outline
Programming Languages
Java Programming Language
First Java program
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
46/61
22
Class
• Each java program includes a Class declaration
public class First
{
}
• First is a valid Java program, and can be compiled
• But, it can not be executed for some reason.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
47/61
First Java Program
• Methods are activity perfoming blocks of Java
classes
• main() method can be declared like this:
public class First
{
public static void main( String args[])
{
}
}
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
48/61
23
main()
• Executable Java programs should contain a
method with the name main()
• Hence, the previous program can not be executed
• Its format is fixed; hence it has to be declared like:
public static void main( String args[])
{
}
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
49/61
Nothing.java
• The following program can be executed, but does
not do anything:
public class Nothing
{
public static void main(String[] args)
{
}
}
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
50/61
24
Welcome.java
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java!");
}
}
D:\My Java> javac Welcome.java
D:\My Java> java Welcome
Welcome to Java!
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
51/61
How to Create a Java File?
• Java document is a text file
• Hence, you can use any text editor to write a Java
program
§ We will use notepad in this course
• You can use format editors which enables saving
as a text file
§ You can use MS Word for example, but in that case you
have to Save As Text File
• You can use a Java environment such as Eclipse
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
52/61
25
Creating and Editing Using NotePad
• To use NotePad, type notepad Welcome.java from
the DOS prompt.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
53/61
Java Translation
Java source
code
Java
compiler
Java
bytecode
Bytecode
interpreter
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
Machine
code
54/61
26
Java is platform independent
Bytecode
Bytecode
JVM
JVM
OS
OS
Computer
Hardware
Computer
Hardware
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
55/61
One program – many computers
• The same program may be run on different
computers with different hardware, each requiring
different machine language codes
High level program
Compiler for machine 1
Instructions for machine 1
Compiler for machine 2
Instructions for machine 2
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
56/61
27
Java Translation (Interpretation)
• Java compiler (P-compiler) translates Java source
code into a special representation called bytecode
• Java bytecode is not the machine language for any
traditional CPU
• Another software tool, called Java Virtual Machine,
translates bytecode into machine language and
executes it
• Therefore the Java compiler is not tied to any
particular machine
• Java is considered to be machine-independent
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
57/61
Executing Java bytecode
Java Bytecode
Java Virtual Machine
any computer
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
58/61
28
Java bytecode execution
• The same Java program may be run on different
computers with different hardware, where each
computer running its own Bytecode interpreter
Java program
Java Compiler
Java Bytecode
Bytecode interpreter
of machine 1
Bytecode interpreter
of machine 2
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
59/61
Syntax and Semantics
• The syntax rules of a language define how we can
put together symbols, reserved words, and
identifiers to make a valid program
• The semantics of a program statement define what
that statement means (its purpose or role in a
program)
• A program that is syntactically correct is not
necessarily logically (semantically) correct
• A program will always do what we tell it to do, not
what we meant to tell it to do
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
60/61
29
Setting the path
• Javac.exe and Java.exe are in the
following directory:
C:\Program Files\Java\jdk1.5.0_05\bin
• You can:
1. write the .java program in that directory
2. go to that directory and compile and run java
program in there
• Or, you can set that path, and do the
previous steps in any directory which is
suitable for you.
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
61/61
Setting the path
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
62/61
30
Does java run?
• Type java –version in DOS prompt to see if
you have installed java environment
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
63/61
main() method
• When a .class file is executed, actually the
main() method is executed
• If a program consists of other methods as
well, they are executed only if they are
called from main()
• The header of main() is very conservative
and should be used as it is
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
64/61
31
main() method
• When, for example main method is
declared without parameters, we will
observe an error if we try to run
public class Hello {
public static void main() {
System.out.println(“Hello!");
}
}
Dr.Vedat Coşkun – Summer School 2006 - Object Oriented Programming with Java
65/61
32