Title: Java
1Java
2WWW
- Was a method for distributing passive information
- added forms and image maps
- interaction was only a new way to get at the same
information - enter Java, and the capability for Web pages of
containing Java applets
3Java applet
- is a dynamic and interactive program that can run
inside a Web page displayed by a Java-capable
browser - Small programs that
- create animations
- multimedia presentations
- real-time video games
- multi-user networked games
- ream interactivity
- most anything a small program can do
4What is Java?
- OO programming language developed by Sun
Microsystems - modeled after C
- small, simple, portable across platforms and
operating systems - complete full-fledge programming language
- HotJava (Suns browser) was completely written in
Java
5Applets and Applications
- Applets- Java programs that are downloaded over
the WWW and executed by a Web browser on the
readers machine - applications - more general programs, do not
require a browser to run. - A single Java program can be both, depending on
how you write that program and the capabilities
that program uses.
6Platform-Independent
- Most significant advantages that Java has over
other programming languages - at both the source and the binary level
- Javas foundation class libraries make it easy to
write code that can be moved from platform to
platform without the need to rewrite it to work
with that platform
7Bytecodes
- Binary files are also platform-independent
- use a form called bytecodes
- bytecodes are a set of instructions that looks
like some machine codes, but that is not specific
to any one processor
8Traditional versus Java compilation
Traditional Code
Java Code
Binary File (Pentium)
Compiler (Pentium)
------- ------- ------- ----- ------ ----
Java Compiler (Pentium)
Java Interpreter (Pentium)
----- ----- -- ----- --- ------
Binary File (Power PC)
Java Bytecode (Platform Independent)
Java Interpreter (PowerPC)
Compiler (Power PC)
Java Compiler (PowerPC)
Binary File (SPARC)
Java Interpreter (SPARC)
Java Compiler (SPARC)
Compiler (SPARC)
9Bytecodes
- Programs can run on any platform and any
operating or window system as long as the Java
interpreter is available
- Disadvantage - execution speed.
10Creating the Hello World Java Application
All of the program is enclosed in a class
definition!
Class HelloWorld Public static
void main (String args)
System.out.println(Hello World!)
Main is the first routine that is run when the
program is executed
11Creating and Executing Hello World
- Use your favorite editor
- filename Helloworld.java
- compile the source file using the Java compiler
(Suns -- javac Helloworld.java) - if no errors - end up with a file called
Helloworld.class - this is the bytecode file - then use the Java interpreter to execute
- java Helloworld
12To create an applet
- Creating applets may be more complex than
creating an application because of the special
rules for how they behave in a browser - for example, Hello World as an applet
- need to make space for your message
- use graphics operations to paint the message to
the screen
13Creating the Hello World Java Applet
Enables this applet to interact with the JDK
classes for creating applets and drawing graphics
on the screen
import java.awt.Graphics Class HelloWorldApplet
extends java.applet.Applet Public
void paint (Graphics g)
g.drawString(Hello World!, 5, 25)
Applets use several standard methods to take the
place of main, which include init(), start() and
paint()
14Creating and Executing Hello World
- Use your favorite editor
- filename HelloworldApplet.java
- compile the source file using the Java compiler
(Suns -- javac HelloworldApplet.java) - if no errors - end up with a file called
HelloworldApplet.class - this is the bytecode
file - now use inside of a web page
15Using the Hello World Java Applet
Hello to Everyone!
My Java applet
says WIDTH150 HEIGHT25