An example program Test.java and documentation.
javadoc -author -private -d doc Test.java
native2ascii -reverse Hello.javaIf the character is not found: sun.io.UnknownCharacterException
Fortunately most (all?) character encoding standards are supersets of ASCII allowing Java keywords to be encoded.
But the conversion can be done by the compiler. The Java compiler uses the "platform default converter" unless the -encoding option is used:
javac -encoding name SourceThe supported encodings supported encodings are found in the Java documentation at Sun.
abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const* for new switch continue goto* package synchronized* not all the reserved words are actually used in the Java language at present. "This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs."
0 2 0372 0xDadaCafe 1997 0X00FF00FF 0xCAFE_BABE 0b0010_0101Leading 0's indicate an integer base 8. So 010 is actually 108 and so equals 810.
0l 0777L 0x1000L 2145L 0xCOBOLThe data type int is the default, i.e., you get an int if you omit the "l" or "L".
1e1f 2.F .3f 0f 2F 3f 3.14f 6.02E23f
0XabaceP-1F 0Xabace.abcdeP+3F 0Xabace.abcdeP99F 0xABACE.ABCDEp99f
1E1 2. .3 0.0 3.14 1e-9d 1e135The data type double is the default, ie., you get an double if you omit the "d", "D", "f", or "F".
'a' '%' '\t' '\\' '\'' '\u03a9' '\uFFFF' '\177' '' ''
``Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n' (§3.10.6). Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.''
Escape sequences are described in §3.10.6 Escape Sequences for Character and String Literals
Note interning
String.class java.lang.String.class boolean.classMore information about this unusual literal can be found at the section on reflection.
( ) { } [ ] ; , .
= > < ! ~ ? : == >= <= != && || ++ -- + - * / & | ^ % << >> >>> += -= *= /= &= |= ^= %= <<= >>= >>>=
class ClassName { // Contents of class }The class is the major part of a compilation unit (the construct given to the compiler) All code is grouped into methods (or static initializers). Every method (and static initializers) must belong to some class. Generally a Java class is defined in a file by itself (with the same name) and this is the unit of compilation (or translation).
attributes type name (parameters) { // code }For the some time, we focus specifically on static methods, those defined using the static attribute.
attributes static type name (parameters) { // code }Generally speaking, objects are the entities manipulated by the program as in the objects of computation. In Java classes have a second very different role than the one mentioned above. Classes are used as templates for new objects. So there are two kinds of objects in Java: primitive values (like numbers) and instances of classes. In object-oriented languages like Java, we usually refer to instances of classes when we say objects. (This leaves no good word for all objects of computation.)
class HelloWorld { public static void main (String[] args) { System.out.println ("Hello World!"); } }
The class, HelloWorld in this case, must be in a file with a matching name HelloWorld.java
Tip: Every class may have a main program. This is useful for unit testing.
javac HelloWorld.java ;; compile source to byte code java HelloWorld ;; execute "main" of this classFor bigger programs Java normally finds all the other classes and libraries it needs. (No make file is needed.)
Java is a big improvement (over C/C++ compiling) as there is no preprocessor and no problem in locating the program pieces. Not only that, but the resulting class files can be run on any platform (with a Java interpreter) without need for recomplication. This make is possible to deploy and distribute application much easier in a heterogeneous environment.
class Main { public static void main (String args[]) { Point p = new Point (); } }
class Point { int x=0, y=0; }
javac Main.java ;; will cause Point.java to be compiled java -verbose:class Main ;; will cause Point.class to be loaded dynamically [Opened /software/java/jdk1.2.2/jre/lib/rt.jar in 32 ms] [Opened /software/java/jdk1.2.2/jre/lib/i18n.jar in 4 ms] [Loaded java.lang.NoClassDefFoundError from /software/java/jdk1.2.2/jre/lib/rt.jar] [Loaded java.lang.Class from /software/java/jdk1.2.2/jre/lib/rt.jar] [Loaded java.lang.Object from /software/java/jdk1.2.2/jre/lib/rt.jar] etc., etc., etc., etc., etc., etc., ... [Loaded Main] [Loaded Point]The execution of the program depends crucially on the fact that both class reside in the same directory (the current working directory). How does Java "know"? The main program needed the class Point, the class Point must be found in the file Point.class, Java looks in the current directory for a file Point.class, if it finds it loads it (dynamically). If the file were missing
java Main Exception in thread "main" java.lang.NoClassDefFoundError: Point at Main.main(Main.java, Compiled Code)
Tip: -verbose:class is occasionally useful in complex programs to determine exactly which class is loaded.
java -verbose:class BigProject | grep MyClass [Loaded project.package.MyClass from not_the_expected_file.jar]
Each Java class (ignoring inner classes) should be in a file by itself. The file name should be the same as the class. Doing anything else is just asking for trouble.
A compilation may, in fact, have multiple class definitions. The Java compiler produces a class file for each class defined. A compilation unit may have at most one public class (a public class is one marked by the keyword public); it must have the same name as the file. If your operating system, ie., DOS/Windows, is lax about the names of files, then you must be vigilant about the file names. Otherwise your files might not compile when transferd elsewhere.
class_name.member_name
Inside the class the prefix can be omitted, leaving just
member_name
A member may refer to an "instance of a class" (introduced later). It's members are referred to using another "dot"
class_name.static_member1.member2
System.out.println
Packages (introduced much later) can add names to the beginning.
package_name.class_name.static_member1.member2
Ulitmately there are six kinds of names (six namespaces).
(In this example we purposely violate the style guideline which rightly suggests that class names be capitalized to avoid confusing them with the other five kinds of names.)javac Args.java java Args "command line argument 1" 2 etc. args[0] = command line argument 1 args[1] = 2 args[2] = etc.
Local variables, like args, are accessed by their name.
Long l = new Long ("3.14159"); byte b = l.byteValue(); short s= l.shortValue();
Long l = Long.valueOf ("3.14159");
long l = Long.parseLong ("3.14159");