Java Introduction

Welcome to the Java beginner tutorial, if you have no experience in programming then you have come to the right place.

Java is one of the most compatible languages around, it works on Windows, Linux and Mac, it is also the main Android language used to create Android applications.

Java History

Java is a programming language originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995 as a core component of Sun Microsystems’ Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers “write once, run anywhere”. Java is currently one of the most popular programming languages in use, and is widely used from application software to web applications.

Install Java on your machine.

If you are using a Macintosh, you can skip this step as the Java SDK(Software Development Kit) should already be on your machine.

If you are using Windows or Linux you can go to http://www.oracle.com/technetwork/java/javase/downloads/index.html and choose the Jdk, you will not need anything else.

For windows follow this guide, http://www.java.com/en/download/help/windows_manual_download.xml

For linux follow this guide, http://www.java.com/en/download/help/linux_install.xml

Getting a Compiler and what is a compiler.

A complier is a program which is used to compile your java program, the compiler will look for errors and will tell you about them if there are any errors.

The JDK you installed should have a compiler now installed on your machine, if any of the code does not work, type the problem into Google and you should find the solution.

You will be using you command prompt or terminal to compile and run your program.

Writing my first java program.

First, create a new folder on your desktop and call it java.

Then open your command prompt or terminal and go to the folder, to do this follow these instructions.

For Macintosh and Linux, open the terminal, type in “ls” to list the directory, then type in “cd Desktop” and press enter, then type in “cd java”. You should now be in your java folder, if you experienced any errors, then the folder does not exist or you typed it in wrong. Linux and Macintosh both use the bash system for their terminals.

For Windows, open up your command prompt and type in “dir” to list your directory, then type in “cd Desktop” and then “cd java”.
I might be wrong as I am currently using a Mac, to go back type in “cd ../”.

Next we will want to start writing our program, to do this open a text editor such as KomodoEdit(available for Mac, Linux or Pc) or TextEdit(already installed for Linux).

Once you have a text editor installed and opened, open a text file and save the file as hello.java into the java folder we created earlier.

Add the following code to the file you have just saved.


public class hello
{
public static void main(String [] args)
{
String myword = "Hello sir, Good day.";
System.out.println(myword);
}
}

Next we will want to compile the program, so go back to your Command prompt or Terminal, we should be in the java folder.

Type in “javac hello.java”

If the window says it cannot find the javac, then you installed the JDK wrong, search Google for a solution.

If you have typed the code in correctly there should not be any errors, we will cover errors in the future.

Next we will want to run the program, to do this type in “java hello”.
The window should print “Hello sir, Good day”.

Have a look at your java folder again, notice the extra file hello.class, when you compiled the program the hello.class file was created, when you run the program you will be running the .class file.

Lets look at the code.


public class hello
{
public static void main(String [] args)
{
String myword = "Hello sir, Good day.";
System.out.println(myword);
}
}

The public class hello is used to declare the name of the program, it is important to place the name of the file where the word hello is, hello is there because the file name is hello.java

Notice the brackets, these are used to hold your program together, ensure you close your brackets and open them where necessary.

Next look at public static void main(String [] args), this is the main of your program, all code is executed here.
Over time you will know this line of by heart.

Inside the main there are two lines, this is the body of the program and will be the purpose of the program.

The first line is used to declare a String, a String is a way of storing words in the program.
This program saves the line “Hello sir, Good day.” into the String myword.

The next line is used to print the String, the code System.out.println(); is the built in command used to print, you can remove the ln in the code, System.out.print(); and the code will print on the same line, ln is used to print the words to the next line.

it is important to end each line in the main with a “;”, this tells the compiler that this is the end of the line and command.

More on Java

Java is not as powerful as C++ or any other similar languages and java is not as fast as them either.
Below is a diagram showing you what happens when a java program is run.

Most programming languages don’t have the environment like java does.

All programming languages must be converted into assembly language so the systems hardware understands what to do.

The advantage to the java environment is that the language can be installed on any sort of machine with different hardware hence making Java very compatible.