Upload
buddy-banks
View
213
Download
0
Embed Size (px)
Citation preview
Java
Lecture 16:
Dolores Zage
WWW
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
Java 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
What 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 (Sun’s browser) was completely
written in Java
Applets and Applications
Applets- Java programs that are downloaded over the WWW and executed by a Web browser on the reader’s 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.
Platform-Independent
Most significant advantages that Java has over other programming languages
at both the source and the binary level Java’s 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
Bytecodes
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
Traditional versus Java compilation
-------
-------
-------
-----
------
----
Traditional CodeCompiler(Pentium)
Compiler(Power PC)
Java Compiler(SPARC)
Java Compiler(Pentium)
JavaCompiler
(PowerPC)
Compiler(SPARC)
Binary File
(Pentium)
Binary File
(PowerPC)
Binary File
(SPARC)
Java Bytecode(Platform
Independent)
JavaInterpreter(Pentium)
JavaInterpreter(PowerPC)
JavaInterpreter(SPARC)
Java Code
-----
-----
--
-----
---
------
Bytecodes
Programs can run on any platform and any operating or window system as long as the Java interpreter is available
Disadvantage - execution speed.
Creating the Hello World Java Application
Class HelloWorld
{
Public static void main (String args[])
{
System.out.println(“Hello World!”);
}
}
All of the program is
enclosed in a class
definition!
Main is the first routine that is run when the program is executed
Creating 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
To 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
Creating the Hello World Java Applet
import java.awt.Graphics;
Class HelloWorldApplet extends java.applet.Applet
{
Public void paint (Graphics g)
{
g.drawString(“Hello World!”, 5, 25);
}
}
Enables this applet to interact with the JDK classes for creating applets and drawing graphics on the screen
Applets use several standard methods to take the place of main, which include init(), start() and paint()
Creating 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
Using the Hello World Java Applet
<HTML>
<HEAD><TITLE> Hello to Everyone! </TITLE>
</HEAD> <BODY>
<P>My Java applet says:
<APPLET CODE=“HelloworldApplet.class” WIDTH=150 HEIGHT=25>
</BODY>
</HTML>