Week01-02
/*
* HelloWorld.java
* A traditional Hello World program!
*/
// In Java, all programs reside in a class. This class is called HelloWorld,
// and should be saved in a file called HelloWorld.java. This class has a single
// method called main, which is the entry point to the program.
//
// Compiling the class with javac HelloWorld.java will produce a bytecode file
// called HelloWorld.class if compilation is successful. This bytecode can then
// be run on any machine with a java bytecode interpreter. You can run the
// bytecode in a console by typing java HelloWorld.
public class HelloWorld {
public static void main(String[] arg) {
String helloString = "Hello";
String worldString = "World!";
// In Java the System.out.println command displays the argument to the
// console. However the command below doesn't work, because helloWorldAString
// has not been declared. Try compiling this file to see what happens,
// and take a careful look at the error message that is produced.
System.out.println(helloString + " " + worldString);
// In Java, we can use the '+' operator to concatenate strings,
// so to fix this problem, either change the argument passed to the
// System.out.println method from (helloWorldString) to
// (helloString + " " + worldString)
// or declare the variable helloWorldString before it is used by
// System.out.println by inserting
// String helloWorldString = helloString + " " + worldString;
// In Java a variable can be declared anywhere in the code, so it is
// possible to declare a variable just before it is used, which makes for
// code that is easier to read and understand.
// It is conventional to use mixed case for variable names and method
// names in Java, with with the first letter lower case, and then the
// first letter of each internal word in upper case -- e.g. helloString.
// Class names start with a capital letter -- e.g. HelloWorld.java
// details at: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html
}
}
继续阅读 →