|
CS 596 Java Programming
Fall Semester, 1998
Java Intro
|
|
|
To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 31-Aug-98
|
|
Contents of Doc 1, Java Intro
References
Various
web pages listed in text
What
is Java?
- A
Language
- A
Platform
- An
OS
- A
Chip
Java
- The Language
C/C++
basic syntax
C/C++
basic data types
- int,
char, float, double, long, short, byte
“Pure” OO language
-
- No
stand alone functions
- All
code is part of a class
No
explicit pointers - uses references
- Uses
garbage collection
Uses
standard C/C++ control structures
Java
is strongly typed
Java
- The Language
Java
is normally compiled to a bytecode
Java
bytecode is a machine language for an abstract machine
Each
platform (or browser) that runs Java has a Java Virtual Machine (JVM or
sometimes VM)
The
JVM executes Java bytecodes
JVMs
with Just-In-Time (JIT) compilers will:
- Compile
the bytecodes to native machine code
- This
is done for each method the first time is executed
- The
native machine code is cached
- The
JVM then runs the native machine code for the method
Java
1.2 will have a JVM (Hotspot) that runs Java code much faster than simple JIT
JVMs
Other
languages can be compiled to Java's bytecode
An
Ada compiler exists that compiles Ada to Java bytecode
Java
- The Platform
Java
has a large API (application programming interface) covering a wide range of
areas
The
following list of Java APIs and applications from Sun show the range of
applications of Java
Java
Foundation Classes (JFC) - GUI
Java
Interface Definition Language (IDL) - CORBA
JDBCTM
Database Access
JavaBeansTM
- componentware
JavaTM
Communications API - serial & parallel port
JavaHelpTM
JavaMail
Java
Media
- JavaTM
2D, Java 3D
TM
, Java
TM
Sound, Java
TM
Speech
- JavaTM
Advanced Imaging
- JavaTM
Media Framework
JavaTM
Web Server
TM
PersonalJavaTM
- Java on personal consumer devices
EmbeddedJavaTM
- Java on embedded devices
JavaTM
Card
TM
- Java on a smart card
JavaTM
Naming and Directory Interface
TM
(JNDI)
Java
Message Service (JMS)
Java
Transaction API (JTA)
Java
- The OS
Sun
and IBM are implementing the Java OS which is an operating system that executes
the Java environment directly on hardware platforms
Java
- The Chip
Some
companies are implementing hardware that uses the Java bytecode as native
machine code
Versions
of Java
JDK
= Java Development Kit
JDK
1.0.2
- First
widely used version
- All
browsers with Java support this version
JDK
1.1.X
- Current
release is JDK 1.1.6
- Made
changes to the language from JDK 1.0.X
- Add
inner classes
-
- API
is about 3 times larger than 1.0.2
- Event
model for GUI components changed from 1.0.2
JDK
1.2
- Currently
in beta 4
- Now
due out in November
- API
is about 3 times larger than 1.1.X, so lots of new stuff
- Should
contain major performance increases
- Major
additions to GUI components
Levels
of Java API
Each
JDK defines a standard set of API classes and methods
Sun
provides standard extensions to the standard API
Others
can and do provide Java APIs
Sun
also provides standard subsets of the JDK for platforms with space restrictions
PersonalJavaTM
- Java
on personal consumer devices
- See:
http://java.sun.com/products/personaljava/spec-1-1/pJavaSpec.html
for more information
EmbeddedJavaTM
- Java
on embedded devices
- See:
http://java.sun.com/products/embeddedjava/spec/eJavaSpec.html
for Draft 0.1 of the Embedded Java specification
JavaTM
Card
TM
- Java
on a smart card
- See:
http://java.sun.com/products/javacard/index.html
for more information about Java
TM
Card
TM
Starting
Java
Compiling
and Executing a Java Program
The
following instructions assume that you are on Rohan, the campus Sun used for
student accounts
Rohan's
address is rohan.sdsu.edu
Rohan
runs Solaris OS, a version of UNIX, and supports telnet and X sessions
Location
of Java Compilers/JVM on Rohan
To
compile and execute a Java program you need access to two programs:
- javac
- compiles java programs
- java
- runs the JVM to execute your programs
The
location of these programs are:
Java
Version
|
Location
|
JDK
1.1.6
|
/opt/java/bin
|
JDK
1.2beta2
|
/opt/jdk1.2beta2/bin
|
JDK
1.2beta4
|
/opt/jdk1.2beta4/bin
|
/opt/java/bin
will always point to the latest stable version of Java on Rohan
JDK
1.2beta2 is currently on Rohan, it will soon be replaced by JDK 1.2beta4
Since
we will be discussing JDK 1.2 in the class and JDK1.2 is still beta I recommend
that:
- you
add /opt/java/bin to your path
-
- I
do this in my .cshrc file with:
set path = ( . bin /opt/java/bin /usr/local /usr/local/bin /stuff/removed/here)
- you
create aliases for /opt/jdk1.2beta2/bin/javac and /opt/jdk1.2beta2/bin/java
- I
created the aliases
jc2
&
j2
by adding the following lines to my .cshrc file:
alias jc2 '/opt/jdk1.2beta2/bin/javac \!*'
alias j2 '/opt/jdk1.2beta2/bin/java \!*'
This
way I can use both JDK 1.1.6 and JDK 1.2beta
To
compile a java program with JDK1.1.6 compiler use:
javac JavaSourceFile.java
To
compile a java program with JDK1.1.6 compiler use:
jc2 JavaSourceFile.java
Note
that there are more programs than java and javac in the java/bin directory
Compiling
& Running HelloWorld
public class HelloWorldExample
{
public static void main( String args[] )
{
System.out.println("Hello World");
}
}
To
compile the above program put it in a file named HelloWorldExample.java
Compile
the program using javac (or
jc2
)
This
will produce the file HelloWorldExample.class
This
file contains the Java bytecode for the program
Run
the program by the command java HelloWorldExample
Rohan
Session
rohan 13-> ls
HelloWorldExample.java
rohan 14-> javac HelloWorldExample.java
rohan 15-> ls
HelloWorldExample.class HelloWorldExample.java
rohan 16-> java HelloWorldExample
Hello World
Java
Source Code Naming Conventions
- All
java source file should end in .java
- Each
.java file can contain only one public class
- The
name of the file should be the name of the public class plus ".java"
- Do
not use abbreviations in the name of the class
- If
the class name contains multiple words then capitalize the first letter of each
word
CLASSPATH
Java
uses the environment variable CLASSPATH to locate class libraries
On
UNIX machines javac and java know where the standard Java class libraries
(APIs) are located, so there is no need to set the classpath for the standard
libraries
On
Rohan there is a useful class library - the SDSU Java Library
To
use this class library you need to add it to your classpath
Here
is how I set my classpath to access this library
setenv CLASSPATH '.:/opt/local/lib/java'
JDK
Tools
The
JDK contains a number of useful tools
You
should look at the options for using java and javac
JDK
1.1.x verses JDK1.2
There
have been a few changes in java and javac from 1.1.x to 1.2
Most
of the changes involve advanced features, which are not for beginners
On
important change is that the -cs option has been dropped in java
This
option allowed the command "java" to also act like a compiler and a make file
Downloading
a JDK
Warning:
Installing a development environment for a language you don't know can be a
frustrating process
Depending
on your:
- platform,
- experience
installing software on your platform
- experience
with Java
you
may wish to gain experience using Java on Rohan before installing a JDK on your
local machine
The
read me files contain installation instructions
Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.
visitors since 31-Aug-98