Introduction To Java
From Compsci.ca Wiki
Introduction to Java: Wiki Edition
What is Java?
- Java is a language in which we can write programs.
- Java is a library. It's a set of code that's already been written, and which Java programmers can use to make their lives easier.
- Java is a system for running programs written in the Java language. When Java code is "compiled", rather than being transformed into machine code that can be run by the physical CPU in your personal computer, it's transformed into a machine code that the Java Virtual Machine can run. Thus anywhere the Java Virtual Machine runs, your Java code can run.
How do I get Java?
Head on over to http://java.sun.com and download the Java SE 5.0 Software Development Kit. This includes the compiler which turns Java code you've written into machine code for the Java Virtual Machine. The Java Virtual Machine and standard library are also installed so you can run programs.
What else do I need?
You'll also need a plain text editor. On Windows, Notepad will work, but I'd recommend something like Textpad. Line numbers in particular help.
What does "Hello world!" look like?
What does "Hello world!" look like?
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } }
How do I compile and run this?
You need to save the code example in a file called "HelloWorld.java". Navigate to where this file is stored in a command prompt window (or terminal window, if you're not using Windows).
We then use the "javac" program to compile the code to something that can be run.
prompt> javac HelloWorld.java
Once this completes, you can run it by using the "java" program.
prompt> java HelloWorld
A bit of theory...
Now you know how to create a "Hello world!" program and compile it, and then run it. This doesn't do you much good.
It's important to understand how programs in Java go together.
All executable code in Java is organized into methods. In our "Hello world!" example, "main" is a method. Methods themselves, however, are further organized into classes.
But... what is a class?
A class lays the groundwork for an object.
But... what is an object?
An object is a combination of data, and operations on that data. By "encapsulating" data within an object, and hiding it from the outside world, we can control the ways in which that data can be changed. Instead of the direct ability to manipulate that data, we provide only certain abilities, via methods.
Back to classes. A class describes the data an object is composed of, and provides the methods for it. Methods are typically "public" and thus accessible to the rest of the world, while data is typically "private", and thus hidden from the rest of the world.
Of course, it doesn't always make sense for methods to be directly attached to objects. In this case, we have "static" methods, like our "main" method. Static methods are associated with the class itself, rather than an object of that class.
The "main" method serves as the entry point for our program. It contains the actual code that is executed when the program is run. Since it doesn't return anything to the program, we say that it returns "void". In contrast, a function called "multiply" that takes two numbers and multiples them might be specified as returning "int" (short for "integer").
A final note: the entry point always accepts an array of strings. This represents the arguments given to the program when it's run from the command prompt.
Creating our own method
Understanding methods is critical, and you can't really understand methods until you write them yourself.
Instead of directy printing "Hello world!", let's have a method do that, and then call that method from the main method.
public class HelloWorld { public static void main(String[] args) { sayHelloWorld(); } public static void sayHelloWorld() { System.out.println("Hello world!"); } }
The new method is also static, because it's not associated with any particular object but rather just the HelloWorld class.
Parameters and arguments
Of course that example is still pretty boring. Our sayHelloWorld method can only ever do one thing.
Of course, this is at it should be. To modify the behavior of a method we need to give it a parameter. You can think of a parameter as being an input. Methods may in fact have numerous parameters. Let's give a sayHelloTo method a string parameter containing a name.
public class HelloWorld { public static void main(String[] args) { sayHelloTo("Bob"); } public static void sayHelloWorld() { System.out.println("Hello world!"); } public static void sayHelloTo(String nameToGreet) { System.out.println("Hello " + nameToGreet + "!"); } }
Now the string we actually gave to the sayHelloTo method when we called the method is called an "argument".
Getting a bit more selective with more parameters and "if" statements
At some point we need to be able to make choices based on parameters. Let's refine our sayHelloTo method so that it can offer more than one type of greeting.
public class HelloWorld { public static void main(String[] args) { sayHelloNicely("Bob", true); sayHelloNicely("Bob", false); } public static void sayHelloWorld() { System.out.println("Hello world!"); } public static void sayHelloNicely(String nameToGreet, boolean niceGreeting) { if (niceGreeting) { System.out.println("Hello " + nameToGreet + "!"); } else { System.out.println("Hey " + nameToGreet + "... STFU!"); } } }
Overloading methods
Now that previous example looks a bit messy. Clearly:
sayHelloNicely("Bob");
Should print "Hello Bob!". But the method needs two arguments. We can't just call it with one.
Not yet, but it's not impossible. What we need to do is overload the method. That is, we provide another method with the same name, but which has a different number, or different types of parameters. The compiler can then simply choose the right one for the job, as necessary.
public class HelloWorld { public static void main(String[] args) { sayHelloNicely("Bob"); sayHelloNicely("Bob", false); } public static void sayHelloWorld() { System.out.println("Hello world!"); } public static void sayHelloNicely(String nameToGreet) { sayHelloNicely(nameToGreet, true); } public static void sayHelloNicely(String nameToGreet, boolean niceGreeting) { if (niceGreeting) { System.out.println("Hello " + nameToGreet + "!"); } else { System.out.println("Hey " + nameToGreet + "... STFU!"); } } }