Introduction To Java Programming Language
Introduction To Java Programming Language
programming Language
main() is the first method that is run.
main() is the first method that is run.
class HelloWorld {
public static void main (String[] args) {
System.out.println(“Hello World!”);
}
}
The notation class.method or Syntax is similar to C braces for
package.class.method is how to blocks, semicolon after each
refer to a public method (with statement.
some exceptions).
Java Applet
• Java applets are classes written in Java that are
not intended to run as stand-alone programs (like
applications) but as subprograms of a browser
that is already managing a window.
• Applets are not trusted by default, so they have
several restrictions in running on the client
machine
• Applets should NOT have a main() method.
Instead they have init(), start(), paint(), etc.
for displaying on the browser window
– no printing or file I/O
– cannot connect through the network to any machine but
its own server
– any new windows created by the applet have a warning
label
Architecture of Java Applets
• Browsers (IE, Netscape, HotJava etc)
supporting Java allow arbitrarily sophisticated
dynamic multimedia applications inserts called
Applets, written in Java, to be embedded in the
regular HTML pages and activated on each
exposure of a given page.
web server
web client, running browser Java code
such as Netscape or IE
is compiled
to produce
Internet
applet codes,
part of web
executes (restricted) document
applet code to display collection
in browser window
An Simple Example, again
• Java applets can call methods to display on a
screen (within the browser window). One way
is to call the method drawString() from the
standard method paint().
The import statement (similar to an include) allows
the use of methods from the Graphics class .
import java.awt.Graphics; Makes this a subclass of Applet.
public class HelloApplet extends java.applet.Applet {
public void paint (Graphics g) {
g.drawString(“Hello World!”, 5, 25);
}
}
The paint() method displays a graphics object on
the screen one of the standard methods that takes
the place of main() for applets.
On the Web
• Name the file HelloWorldApplet.java. Run
the compiler, javac, to get a byte code file
HelloWorldApplet.class. Put this in a web
directory.
<html><head>
<title>Simple Hello Page</title>
</head>
<body> Name of your applet class.
My Java applet says:
<applet code=“HelloWorldApplet.class” width=150 height=25>
</applet>
</body></html>
The browser will use a rectangle of width 150 pixels and
height 25 pixels to display the applet within the other html.
Java vs. JavaScript
• JavaScript is a different language from Java,
albeit with some similarities.
• A JavaScript program is written in the HTML
page, and executed by the JavaScript
interpreter, so also allows dynamic web page
content in the browser window.
• JavaScript is special purpose - it is an object-
based language that deals directly with browser
entities like windows, text fields, forms, frames
and documents.
• JavaScript can respond to browser events like
mouse clicks and user-typed text.
• JavaScript is fast to write, but not as powerful as
Java.
Some Key Java Features
◆ First we discuss original Java base language features
as discussed in Java: A White Paper by Sun
Microsystems—October 1995 draft by James Gosling
and Henry McGilton—enumerates the original
design goals of Java:
Object-oriented Robust
Architecture-neutral Secure
Portable High performance
Somewhat Interpreted Multi Threaded
Simple and Familiar Dynamic
Distributed
Java Features—It's Simple and
Familiar
• Familiar as it looks like C++, but simpler to
program.
– omits several confusing features of C++ including
operator overloading, multiple inheritance, pointers
and automatic type coercions
• Adds automatic garbage collection to make
dynamic memory management much easier
than in C or C++.
– No more frees or deletes. No more memory leaks.
• Adds Interface construct, similar to Objective C
concept, to compensate for the lack of
multiple inheritance.
• Small kernel is suitable for Java ports to
consumer electronic devices.
Java Features—It's Object-oriented
• Java model is sometimes viewed as a C++
subset, with some elements imported from
other languages.
– This is arguable. In many ways Java and C++ are
very different, and many of the similarities that do
exist are at a fairly superficial syntactic level.
• Structures, Unions and Functions are absorbed
into data and methods of Java classes—Java is
simple.
• The strength of Java object-oriented model is
in simplicity and the extensive class library
associated with the system.
Java Features—It's Architecture-
Neutral
• C/C++ programming in a heterogeneous network
environment demands compatibility across several vendor
platforms and their compilers.
• Solved in Java by designing platform-independent binary
representation called Java bytecode—comparable to P-
code in UCSD Pascal.
• Core Java
– Gary Cornell and Cay S. Horstmann
• Java Tutorial
• http://www.ibiblio.org/javafaq/course/index.html
• http://java.freehosting.co.kr/tutorial/
• http://aspen.csit.fsu.edu/it1spring01/