Java
CptS 355 - Programming Language Design Washington State University |
|
Java Features
The Java Virtual Machine (JVM)A Java source program (e.g., Foo.java) is compiled to a class file (Foo.class) containing machine instructions called byte codes for the Java Virtual Machine. Compilation is performed by thejavac program -- the Java compiler.
Class files are (supposed to be) portable across different implementations
of the JVM. To run a class file use the java program to start
the JVM executing your class file.
There are different implementations of JVM for different computer architectures and operating systems. Different implementors of JVMs have different goals for performance as well. The advantages of compiling code for the JVM instead of native code for a particular computer include:
Java's approach, using JVM, can also be contrasted to the Python approach which
also uses byte codes. Unlike Java, Python does not separate the compiler and virtual
machine into separate programs: there is just the C/C++ features not in Java and what to do instead
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 JavaJava code is complied into JVM (Java Virtual Machine) code by the Java compilier (javac).
javac Helloworld.javawill 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 Helloworldwill start with main in the Helloworld class.
CommentsTwo varieties // I am one line comment
/* I am a
multi-line comment
*/
Classes
public class Name {
public int method1(arg_type arg, ...) { }
public static int method2(arg_type arg, ...) { }
...
}
JavadocJavadoc is the Java API Documentation system. It can produce HTML output of the documentation in your program.
/**
*
Data Types
Variable Declarations
Java OperatorsC-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 ControlC-like
if (x == 34) {... } else {...}
for (i = 0; i < length; i++) {...}
switch (month) {
case 1: System.out.println("January"); break;
...
default: ....
}
while (x != 23) {...};
ArraysArrays are objects
int[] arrayOfInts = new int[10]
for (int j = 0; j < arrayOfInts.length; j ++) {
arrayOfInts[j] = j;
System.out.println("[j] = " + arrayOfInts[j]);
}
StringsA string is an object.
I/O - output
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 - input
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
|
|