The Java platform is not a specific hardware or operating system, but rather
an execution environment called a JRE (Java Runtime Environment) implementing
JVM (Java Virtual Machine) , and a set of standard
libraries (APIs) which provide common functionality.
The platform was called the Java 2 Platform although the "2"
has been dropped, and includes both a Standard Edition or Java SE, an
Enterprise
Edition Java EE), and a Micro Edition or Java ME. The current version of the
Java SE platform is alternatively specified as version 1.5 or version 5 (both
refer to the same version). A good overview of the myriad of technologies that
makes up the Java 2 Platform SE 5 (Java SE 5) can be found on the
JDK Documentation Page.
J2SE 6.0 will become Java SE 6 (code name Mustang), and J2SE 7.0 will become Java SE 7 (code name Dolphin).
new Greeter("World")
new Greeter("World").sayHello()
Greeter worldGreeter = new Greeter("World");
String greeting = worldGreeter.sayHello();
javac -d F:\classes -classpath F:\classes *.javaNote that Greeter.java is compiled and class bytecodes are places in F:\classes
java -cp F:\classes GreeterTest
Putting your program in a JAR allows it to be distributed as a single executable file, saving space and simplifying the download process.
Here is the general procedure for creating an executable JAR:
Manifest-Version: 1.0 Main-Class: name of class containing main
The name of the file should end with the .mf suffix. It is important that the file ends with a blank line.
jar cmf manifest-file jar-file input-files
The input-files must include any class files, images, sounds, etc.
that your program uses. Optionally, you can include the program's
.java
files in the JAR. See below for adding directories ot the JAR.
jar tf jar-file
java -jar jar-file
Let's say we wanted to distribute the simple program GreeterTester.java as a JAR. First, we create a text file named GreeterTester.mf which contains:
Manifest-Version: 1.0 Main-Class: GreeterTester
Then, we create the archive by typing:
jar cmf GreeterTester.mf GreeterTester.jar Greeter.class GreeterTester.class
and run it by typing:
The file GreeterTester.jar can now be downloaded and executed.java -jar GreeterTester.jar
y = Math.sqrt(x);
for (int i = 1; i <= n; i++)
{
. . .
}
// i no longer defined here
Greeter worldGreeter = new Greeter("World");
Greeter anotherGreeter = worldGreeter;
anotherGreeter.setName("Dave");
// now worldGreeter.sayHello() returns "Hello, Dave!"
worldGreeter = null;
if (worldGreeter == null) . . .
public boolean equals(Greeter other)
{
if (this == other) return true;
return name.equals(other.name);
}
public Greeter(String name)
{
this.name = name;
}
public void copyNameTo(Greeter other)
{
other.name = this.name;
}
Greeter worldGreeter = new Greeter("World");
Greeter daveGreeter = new Greeter("Dave");
worldGreeter.copyNameTo(daveGreeter);
public void copyLengthTo(int n)
{
n = name.length();
}
public void copyGreeterTo(Greeter other)
{
other = new Greeter(name);
}
int length = 0;
worldGreeter.copyLengthTo(length); // length still 0
worldGreeter.copyGreeterTo(daveGreeter) // daveGreeter unchanged
java.util
javax.swing
com.sun.misc
edu.ttu.cs.dp.alice
package edu.ttu.cs.dp.alice;
public class Greeter { . . . }
java.util.ArrayList
javax.swing.JOptionPane
import java.util.Scanner;
. . .
Scanner a; // i.e. java.util.Scanner
import java.util.*;
import java.*.*; // NO
import java.util.*;
import java.sql.*;
. . .
java.util.Date d; // Date also occurs in java.sql
edu.ttu.cs.dp.alice.Greeter.java
must be in subdirectory
basedirectory/edu/ttu/cs/dp/alice/alice
cd basedirectory/edu/ttu/cs/dp/alice javac Greeter.java
java -cp F:/classes edu.ttu.cs.dp.alice.Greeter.GreeterTester
String name = null;
int n = name.length(); // ERROR
Exception in thread "main" java.lang.NullPointerException
at Greeter.sayHello(Greeter.java:25)
at GreeterTest.main(GreeterTest.java:6)
public void read(String filename) throws FileNotFoundException
{
FileReader reader = new FileReader(filename);
. . .
}
public void read(String filename)
throws IOException, ClassNotFoundException
public static void main(String[] args)
throws IOException, ClassNotFoundException
try
{
code that might throw an IOException
}
catch (IOException exception)
{
take corrective action
}
exception.printStackTrace();
System.exit(1);
FileReader reader = null;
try
{
reader = new FileReader(name);
...
}
finally
{
if (reader != null) reader.close();
}
"Hello, " + name
int n = 7;
String greeting = "Hello, " + n;
// yields "Hello, 7"
Date now = new Date();
String greeting = "Hello, " + now;
// concatenates now.toString()
// yields "Hello, Wed Jan 17 16:57:18 PST 2001"
String input = "7";
int n = Integer.parseInt(input);
// yields integer 7
ArrayList<String> countries = new ArrayList<String>();
countries.add("Belgium");
countries.add("Italy");
countries.add("Thailand");
An array list is a collection of objects that supports efficient access to all storage locations.
for (String country : countries) . . .
LinkedList<String> countries = new LinkedList<String>();
countries.add("Belgium");
countries.add("Italy");
countries.add("Thailand");
for (int i = 0; i < numbers.length; i++)
numbers = new int[0];
int[][] table = new int[10][20];
int t = table[i][j];
public class Greeter
{
. . .
private static Random generator;
}
public class Math
{
. . .
public static final double PI = 3.14159265358979323846;
}
public static Greeter getRandomInstance()
{
if (generator.nextBoolean()) // note: generator is static field
return new Greeter("Mars");
else
return new Greeter("Venus");
}
Greeter g = Greeter.getRandomInstance();
name
sayHello
Greeter
ArrayList
PI
MAX_VALUE
String getName()
void setName(String newValue)
public boolean isPolite()
public void setPolite(boolean newValue)
public String sayHello()
{
return "Hello, " + name + "!";
}
public String sayHello() {
return "Hello, " + name + "!";
}
public class Greeter
{
private String name;
public Greeter(String aName) { . . . }
. . .
}
Good: if (x > Math.sqrt(y))
Bad: if(x>Math.sqrt (y))
Good: int[] numbers
Bad: int numbers[]
Good: h = HASH_MULTIPLIER * h + val[off];
Bad: h = 31 * h + val[off];