Learning Java/Introduction to Java

What is Java? edit

Java is an Object-Oriented, cross platform, multi-purpose programming language produced by Sun Microsystems. First released in 1995, it was developed to be a machine independent web technology. It was based on C and C++ syntax to make it easy for programmers from those communities to learn. Since then, it has earned a prominent place in the world of computer programming.

Java has many distinct characteristics that have contributed to its popularity:

  • Platform independence - Many languages are compatible with only one platform. Java was specifically designed so that it would run on any computer, regardless if it was running Windows, Linux, Mac, Unix or any of the other operating systems.
  • Simple and easy to use - Java's creators tried to design it so code could be written efficiently and easily.
  • Multi-functional - Java can produce many applications from command-line programs to applets to Swing windows (basically, sophisticated graphical user interfaces).

History of Java edit

See History of Java article section at Wikipedia

The Java Platform edit

One thing that distinguished Java from some other languages is its ability to run the same compiled code across multiple operating systems.

In these other languages such as C or C++ the source code, or the instructions which are written by the developer, is compiled by a compiler into an object file, which is then linked with other object files to create an executable file. This file is in machine language, and is intended for a single operating system/processor combination. This means that the developer would have to compile the program separately for separate operating system/processor combinations.

Java is different in that it does not compile the code into machine language code. Compilation creates bytecode out of the source code. Source code is written and stored in files with the extension of .java, and after compilation a file with a ".class" extension is created which contains the bytecode instructions. Bytecode generally looks something like this:

public class UserDetails extends java.lang.Object{
public UserDetails();
  Code:
   0:	aload_0
   1:	invokespecial	#1; //Method java/lang/Object."<init>":()V
   4:	return

public UserDetails(java.lang.String, java.lang.String, java.lang.String);
  Code:
   0:	aload_0
   1:	invokespecial	#1; //Method java/lang/Object."<init>":()V
   4:	aload_0
   5:	aload_1
   6:	putfield	#2; //Field userName:Ljava/lang/String;
   9:	aload_0
   10:	aload_2
   11:	putfield	#3; //Field password:Ljava/lang/String;
   14:	aload_0
   15:	aload_3
   16:	putfield	#4; //Field email:Ljava/lang/String;
   19:	return

public UserDetails(java.lang.String, java.lang.String, java.lang.String, java.lang.String);
  Code:
   0:	aload_0
   ....
}

To view the bytecode of a compiled Java program one can execute the following command at the command-line:

javap -c NameOfClassFile

where NameOfClassFile is the file ending in ".class" which was generated during compile time.

When the code is run by the user, it is processed by Java Virtual Machine (JVM). The JVM is essentially an interpreter for the bytecode. At execution time the JVM translates the compiled bytecode into machine language which can then be executed by the hardware. There are different versions of the JVM which are compatible with various operating systems, allowing the same code to be executed on different platforms. This makes little difference to the end-user, however it greatly reduces the effort needed to develop cross-platform applications on the part of the developer.

Installing the Java Development Kit edit

The Java Development Kit (JDK) contains the core libraries and compiler required to develop Java, and it is distributed by Sun. The JDK should not be confused with the JRE (Java Runtime Environment). The JRE is a virtual machine for running, as opposed to compiling, Java programs.

Downloading and Installing edit

To download the JDK, go to http://java.sun.com/javase/downloads/index.jsp. Click on "JDK 6 Update 3 with NetBeans 6.0". Follow the instructions for downloading the JDK installation file. If you are running Windows, simply run the executable file and follow the installation instructions. If you are using UNIX, Solaris or Linux, it might take a little more work.

If you are using a Macintosh, the latest available JDK is automatically installed by the operating system. Because Java on the Macintosh is developed and maintained by Apple, in coordination with Sun, the current version on the Macintosh may not be the current version that is available from Sun.

Note on Editions edit

The JDK comes in three editions.

  • Java 2 Standard Edition (J2SE) This is the basic platform for Java. This is the one this course uses.
  • Java 2 Enterprise Edition (J2EE) This edition is mainly for developing and running distributed multi-tier architecture Java applications, based largely on modular software components running on an application server. We will not be covering this version in this course.
  • Java 2 Micro Edition (J2ME) This is primarily for developing programs to run on consumer appliances, such as PDAs and cell phones.

Now the '2' has been dropped from the name and is generally referred to as Java SE/EE/ME. Also JDK 1.5 is JDK 5 and so on.

Configuring Variables edit

Before writing code, it is recommended that you set the Path variable on your system so you can compile your code more easily.

For Windows Users edit

  • From the Control Panel, double click "System"
  • For XP and 2000, click on the "Advanced" tab and click on "Environment Variables" For NT, click on the "Environment" tab.
  • Under System variables Select the Path variable and click "Edit"
  • Add the path to the bin directory of where Java is installed on your hard drive. It should probably be: C:\Program Files\Java\jdk1.6.0\bin unless you changed it during installation.
  • Click OK

For Linux and UNIX edit

One way to set your path in Linux/Unix is to add a path export to your bash profile.

  • In order to do this, first open your bash profile in a text editor. For example,

pico ~/.bash_profile

  • Then add this line:

export PATH=$PATH:/usr/local/jdk/bin

Note that the path to the java directory "/usr/local/jdk/bin" may be different on your machine.

  • Restart your shell.

For Macintosh edit

Apple sets everything up for you. Sit back and relax.

The only drawback is that because Apple handles development and maintenance of Java on the Mac, there is usually a delay from the time that a new version is released by Sun and the time that the new version is released on the Mac. Also, getting the latest version sometimes requires an operating system upgrade.

Oh well, you can't have everything.

"Hello World" edit

Anytime you learn a computer programming language, it is tradition that the first program you write should be to make your computer say, "Hello World". This is a small feat, but is a good opportunity to make sure that you installed the JDK properly.

The Java compiler reads basic text files. Open up a text editor like Notepad (do not use a complex program like Microsoft Word for this). Type this code, remembering that Java is case-sensitive:

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

Here's the JDK 1.5 or later version of Hello World

public class HelloWorld 
{	
    public static void main(String... args) 
    {
        System.out.println("Hello World!");
    }
}

Save this file as HelloWorld.java. Start your system's command line and navigate to the folder that you saved HelloWorld.java to. Type javac HelloWorld.java. This runs the java compiler, javac, and creates a class file, which contains the bytecode for the application. Next type java HelloWorld. This runs the class file that was just created by the compiler. Your console should print:

Hello World!




Congratulations - you finished reading Intro to Java. Now, you can move on to Basic Java Language


Project: Topic:Java
Previous: Topic:Java — Learning Java/Introduction to Java — Next: Basic Java Language


External links edit