Introduction To Java

From Compsci.ca Wiki

Revision as of 20:37, 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
Personal tools