1. "Hello, World!"
  2. Variables and Types
  3. Arrays
  4. While, If, For
  5. ...Problem Set 0
  6. Static Methods
  7. Static Fields
  8. String Conversion
  9. Objects
  10. Threading
  11. Strings
  12. ...Problem Set 1.5
  13. Packages
  14. Complex Numbers
  15. Abstract classes
  16. Interfaces
  17. Autoboxing
  18. ...Problem Set 1
  19. enum
  20. Inner Classes
  21. Polymorphism
  22. Tanks!
  23. Callbacks
  24. Exceptions
  25. File I/O
  26. ...Problem Set 2
  27. Regular Expressions

"Hello, World!"

The first program you need to learn in any language is called, "Hello, World!" and its purpose is to simply print the words "Hello, World!" to the screen. In Java it looks like this.

1public class Hello { 
2    public static void main(String[] args) {
3        System.out.println("Hello, world");
4    }
$ javac Hello.java
$ java Hello
Hello, world

There is quite a lot of strange words and symbols in this code, you don't need to worry too much about what they mean at this point.

Already in this simple program there are some advanced concepts:

  1. The text inside quotes is a string constant. It is effectively just a list of characters.
  2. Method calls: System.out.println("Hello, world") is an example of a call to the println() method. A method is like a task or unit of work. Syntactically, it looks like a word followed by one or more items in parenthesis. The println() method prints an arbitrary string of characters on the screen.
  3. Semi-colons: The semi-colon is used by Java to divide up the individual commands we give to the program. In this example there is only one command, and that command is to print the string "Hello, world" to the screen.
  4. Arguments: The string "Hello, world" is called an argument, and we say that we are passing the argument "Hello, world" to the println() method.
  5. Method definitions: main(String[] args) is an example of a method definition. Note the parentheses and also the curly brackets, i.e. the {}'s signify that we have a method definition. This main() method always prints the specific string "Hello, world" to the screen. To write a java program you must write a static method named "main". By convention, this is the place where java begins execution of your program.
  6. Class: For now, think of a class as just a means of categorizing things. The main() method we are defining lives inside a class named Hello. Your class name should always match your file name, and class names should always begin with a capital letter.
1public class Math1 {
2    public static void main(String[] args) {
3        System.out.println("string concat" + "enation");
4        System.out.println(3);
5        System.out.println("this is a three: "+3);
6        System.out.println("3+7/3-4="+(3+7/3-4));
7        System.out.println("3.0+7.0/3.0-4.0="+(3.0+7.0/3.0-4.0));
8        System.out.println("done");
9    }
$ javac Math1.java
$ java Math1
string concatenation
this is a three: 3

New things here:

  1. The body of our main() function now has four commands in it, instead of just one. Notice that each of them terminates with a semicolon.
  2. Line 3 uses the concept of string concatenation. In other words, we can write two strings and use + to join them together.
  3. Line 4 is our first example of a numeric constant. Specifically it is the integer 3.
  4. Line 5 adds an integer to a string. Java always knows how to convert something to a string, be it an integer, or some more advanced data type. It implicitly makes the conversion, concatenates the strings to one string, and generates output.
  5. Line 6 adds a mathematical expression to a string. The Java compiler will follow normal mathematical ordering -- it will do multiplies before adds, allow grouping of expressions using parentheses, etc. Notice that no decimal points are used. As a result, this is an example of integer math. When doing integer math, remainders are ignored. Thus, 7/3 is just 2.
  6. Line 7 is like line 6 but uses double precision math. The presence of a decimal point is what makes the difference. In double precision math, remainders are not ignored.
1public class Ops {
2    public static void main(String[] args) {
3        System.out.println("integer:");
4        System.out.println("  10/3="+(10/3)); // division
5        System.out.println("  10*3="+(10*3)); // multiplication
6        System.out.println("  10%3="+(10%3)); // remainder of 10 / 3.
7        System.out.println("  10+3="+(10+3)); // addition
8        System.out.println("  10-3="+(10-3)); // subtraction
9        System.out.println("  10 & 3="+(10 & 3)); // bitwise logical and
10        System.out.println("  10 ^ 3="+(10 ^ 3)); // bitwise logical exclusive or
11        System.out.println("  10 | 3="+(10 | 3)); // bitwise logical or
12        System.out.println("floating point:");
13        System.out.println("  10.0/3.1="+(10.0/3.1)); // division
14        System.out.println("  10.0*3.1="+(10.0*3.1)); // multiplication
15        System.out.println("  10.0+3.1="+(10.0+3.1)); // addition
16        // Tricky! Scientific notation
17        System.out.println("  1.0e1-3.1="+(1.0e1-3.1)); // subtraction
18    }
$ javac Ops.java
$ java Ops
  10 & 3=2
  10 ^ 3=9
  10 | 3=11
floating point:

Here we see a more complete enumeration of the mathematical operators available to us for doing both integer and floating point arithmetic. Notice in the last line we've written "10.0" as "1.0e1". This is a kind of scientific notation, a shorthand for 1.0 * pow(10,1).

Note the logical operations available to us for the integer values. Don't worry too much if you don't know what logical and, or, and exclusive or are. We'll get back to these concepts later.

We also introduced comments: Everything after a // is ignored by the compiler. We can do this to write all kinds of helpful explanations into our code.