Agile Javaв„ў: Crafting Code with Test-Driven Development

To get started, you will code the "Hello World" program. The hello world program is the prototypical first Java program. When executed, it prints the text "hello world" onto your console. Many programmers venturing into new languages create a hello world program to demonstrate the basic ability to compile and execute code in that new language environment.

The javac Compiler

The basic format of the javac compiler, included in Sun's Java Software Development kit, is

javac <options> <source files>

javac provides numerous command line options. You can obtain a list of the options by entering the command javac and pressing enter. All options are of course optional. Options precede the name of the source file (or files) being compiled. An example javac command:

javac -g:none Hello.java

In this command, the text -g:none is an option that tells the compiler to generate no debugging information (information used to help you decipher execution problems).

You can always issue the command:

java -version

with no source file, to see which version of the Java compiler you are executing.

I include "hello world" here solely as a quick means of feedback. Many of the concepts in it are more advanced topics that you will learn later in the book. This exercise exists to ensure you are able to compile and execute Java programs. For now, type the code below, exactly as it appears, into your IDE or editor.

class Hello { public static void main(String[] args) { System.out.println("hello world"); } }

Save this using the filename Hello.java. Case is important in Java. If you save it as hello.java, you may not be able to compile or execute the code.[2]

[2] Windows is a little less picky about case. You may find that Windows doesn't always complain about case when it should.

Категории