|
|
|
Features
- object-oriented, imperative
- C/C++-like (C++ ++ --)
- safe, robust, multithreaded, architecture independent
- no pointers
- small, easy, no obfuscation
- no undefined or architecture dependent features
- static scoping, static typing, C-like precedence and associativity,
short circuit evaluation,
no operator overloading (but does have method overloading),
eager (left to right) evaluation of parameters,
strongly typed, hybrid implementation with a virtual machine,
run-time memory mangament, explicit allocation and
implicit deallocation, heap-dynamic objects (only), reference types (all objects),
no dangling pointers,
no memory leaks.
Hello World
import java.io.*
// Helloworld.java
// compile using 'javac Helloworld.java'
// This is a hello world example!
class HelloWorld {
// The main method prints "hello world"
// args are command line args
public static void main (String args[]) {
// I/O is done using streams
System.out.println("Hello World!");
return; // optional
}
}
Note that every program must have at least one class. By default
the main method is called when Java first runs.
main must be static and public.
"static" means it stays with the class (not an object method, but
a class method). static methods can be invoked using a class
qualification, e.g., the following calls the main method in the
Helloworld class.
Helloworld.main()
By convention, the class Helloworld must be in the
file Helloworld.java.
Compiling and Running Java
Java code is complied into JVM (Java Virtual Machine) code by the
Java compilier (javac).
javac Helloworld.java
will produce Helloworld.class.
If Helloworld.java imports another class, that class
will be automatically compiled as well.
Standard libraries are automatically imported (and are precompiled).
In the above program, java.io.* is a standard library.
To run the resulting program, start the JVM on the class containing
the main method, e.g.,
java Helloworld
will start with main in the Helloworld class.
Comments
Two varieties
// I am one line comment
/* I am a
multi-line comment
*/
Classes
- a class is a prototype
- typical example
public class Name {
public int method1(arg_type arg, ...) { }
public static int method2(arg_type arg, ...) { }
...
}
static methods remain with class
Javadoc
Javadoc is the Java API Documentation system.
It can produce HTML output of the documentation in your program.
/**
* TauZamanSystem is the entrance point for
* TauZaman services. It contains methods, which provide
* services to interact with further Calendar related
* services. It contains all repositories, which will be
* used as database for all services.
*
* @author Curtis Dyreson and Bedirhan Urgun
* @version 0.1 03/03/02
* @see tauzaman.calendricsystem.CalendricSystemRepository
* @see tauzaman.calendar.CalendarRepository
* @see tauzaman.field.FVSupportRepository
* @see tauzaman.property.PropertyRepository
* @status design complete, implementation complete
*/
public class TauZaman {
/**
* Create a determinate granule at the default Granularity
* @param p - count of granules
*
* @throws TauZamanException if any abnormal condition occurs
* when setting this Granule's default Granularity
**/
public Granule createGranule(TimeValue p)
throws TauZamanException {
....
}
Data Types
Variable Declarations
Java Operators
C-like
This table summarizes Java's binary arithmetic operations:
Operator Use Description
+ op1 + op2 Adds op1 and op2
- op1 - op2 Subtracts op2 from op1
* op1 * op2 Multiplies op1 and op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing
op1 by op2
Flow of Control
C-like
if (x == 34) {... } else {...}
for (i = 0; i < length; i++) {...}
switch (month) {
case 1: System.out.println("January"); break;
...
default: ....
}
while (x != 23) {...};
Arrays
Arrays are objects
int[] arrayOfInts = new int[10]
for (int j = 0; j < arrayOfInts.length; j ++) {
arrayOfInts[j] = j;
System.out.println("[j] = " + arrayOfInts[j]);
}
Strings
A string is an object.
I/O
import java.io.*;
class PersonalHello {
public static void main (String args[])
{
byte name[] = new byte[100];
int nr_read = 0;
System.out.println("What is your name?");
try {
nr_read = System.in.read(name);
System.out.print("Hello ");
System.out.write(name,0,nr_read);
}
catch (IOException e) {
System.out.print("I'm Sorry. I didn't catch your name.");
}
}
}
I/O
static int getNextInteger() {
String line;
DataInputStream in = new DataInputStream(System.in);
try {
line = in.readLine();
int i = Integer.valueOf(line).intValue();
return i;
}
catch (Exception e) {
return -1;
}
} // getNextInteger ends here
|