Notes on Java. **************************************************************** Hello world. With most development systems, each public Java class must be defined in its own file. The name of that file must be the name of the class, followed by the suffix ".java". For example, the following class should be defined in a file named "Hello.java". class Hello { public static void main(String[] args) { System.out.println("Hello world!"); } } % javac Hello.java % java Hello Hello world! % The program above is a Java application. To write a Java program that can run inside a browser, you would write an applet instead. In this course we will write applications, not applets. **************************************************************** Syntax of Java (by example). package points; /* Neither of these import statements is necessary. */ import java.lang.*; import java.util.Vector; public class Point { private double x = 0.0; private double y = 0.0; public Point (double x, double y) { this.x = x; this.y = y; } public double distance (Point p) { double dx = x - p.x; double dy = y - p.y; return java.lang.Math.sqrt (dx * dx + dy * dy); } } **************************************************************** Strong typing = static typing + type safety. For example, C++ is statically typed but is not strongly typed: float & kaboom () { char * s = "Goodbye cruel world"; int * p = (int *) s; float * * q = (float * *) p; p[0] = 1115252000; cout << s << endl; q[3][4] = 0.0; cout << "Returning from kaboom" << endl; return q[3][4]; } **************************************************************** Primitive types of Java. byte (8 bits) float (32 bits, 24-bit significand) short (16 bits) double (64 bits, 53-bit significand) int (32 bits) char (16 bits, UCS-2) long (64 bits) boolean **************************************************************** [from Gosling, Joy, and Steele] Java's reference types are the class types, the interface types, and the array types. The reference types are implemented by dynamically created objects that are either instances of classes or arrays. Many references to each object can exist. All objects (including arrays) support the methods of the standard class Object, which is the (single) root of the class hierarchy. A predefined String class supports Unicode character strings. Standard classes exist for wrapping primitive values inside of objects. **************************************************************** There are two fundamentally different kinds of values in Java. basic values (byte, short, int, long, float, double, char, boolean) references (null, references to objects) In C++, there are at least five different kinds of stuff, not even counting references: basic values (char, unsigned char, signed char, signed short int, unsigned short int, signed int, unsigned int, signed long int, unsigned long int, float, double, bool) POD structures pointers to POD structures non-POD structures pointers to non-POD structures ****************************************************************