Introduction To Java

From Compsci.ca Wiki

Revision as of 20:42, 9 October 2008 by Wtd (Talk | contribs)
Jump to: navigation, search

Contents

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.

Personal tools