Follow us on RSS or Twitter for the latest updates.

April 3, 2011

Java: A Comprehensive Beginners Guide.


This is a simple guide to give you a brief idea of Java and get you running simple programs as quickly as possible. As with anything you just have to keep at it! You might not understand everything at once but you will eventually. Java is a little tricky to understand at first but once you get the basic idea, its easy! The language itself is relatively easy to learn, the challenge comes in knowing how to logically apply the language to solve problems.

I recommend you download and install eclipse, available at:

http://www.eclipse.org/downloads/

Eclipse is an IDE, Integrated Development Environment and contains amongst other things an editor, compiler and the JVM (Java Virtual Machine). Eclipse IDE will save, compile, and run your code by simply clicking Run.

Java is made up of classes. These classes simply contain Java code to carry out tasks. The different tasks are called methods. The idea of Java is to piece together these classes and use their different methods to solve problems like the programming missions here at HTS.

Java is made up of hundreds of classes organized into packages. These packages of classes are stored in the Java Class Library. The following link shows the classes and methods of the JCL. These are known as the Java Docs.

http://download.oracle.com/javase/6/docs/api/

Lets Start

Install eclipse.
Open eclipse.
Select File | New | Java Project.
Type HTS as the project name and select Finish.
Using the File menu, create a new Class within that project. Name your Class MyFirstProgram, select finish. Note, Java is case sensitive, Classes begin with an upper-case letter, variables with a lower-case letter.

Type the following into your eclipse class and run it! No point moving forward until you see the displayed message in the console. Eclipse might have already typed the class name and inserted the braces.

CODE :
public class MyFirstProgram
{
public static void main(String[] args)
{
System.out.println("IT WORKS");
}
}
Everything inside the first set of braces belongs to your MyFirstProgram class. The next line is the main method. Every Java program must have a main method, we will not discuss the syntax of this line in this article, but you will always use it.

Everything inside the main method braces will be the main code we will focus on in this article to get us started.

System is a class in the JCL. The method println prints (“IT WORKS”) to the console.

If it works create a new class or delete the contents of the one your using and type or copy the following:

CODE :
public class MyFirstProgram
{
public static void main(String[] args)
{
int myAge = 23;
double myMoney = 234.23343;
String name = "fj";

System.out.println("My Name is " + name);
System.out.print("and my age is " + myAge + " and I have ");
System.out.println(myMoney + " pounds");
}
}
Explanation: myAge is a variable, its type is int. int is a Java primitive type, it is an integer. The variable is assigned to 23. The semicolon is the end of that line of code.

The next section of code will take user input from the console. This will give an introduction into Objects. Try the following code:

CODE :
import java.util.Scanner;

public class MyFirstProgram
{
public static void main(String[] args)
{
String name;
int myAge;

System.out.println("Type your name and age: "); // try HTS 10
// Try other inputs, can you get an error? Why?

Scanner myScan = new Scanner(System.in);

name = myScan.next();
myAge = myScan.nextInt();

System.out.println("My Name is " + name);
System.out.print("and my age is " + myAge);
}
}
Scanner is a class in the JCL. We create a new object of the Scanner class. The object myScan is an object/instance of the Scanner class. We can call or invoke the different methods of the Scanner class upon our newly created object. We find the methods that we can use by checking the Java docs (the link above).

We use the new operator to create an object. We use the import statement to import the Scanner class, located in the Java.Util package (but eclipse will do the imports for us)

The Scanner object myScan can be created in different ways using the parameters associated with the Scanner Class. In this case we are telling our new object that its input will be coming from System.in (the console). We could change System.in to take the input from other sources such as the internet. We could use our scanner object to read the source code of an internet site.

Try the following code:

CODE :
import java.net.URL;
import java.util.Scanner;

public class MyFirstProgram
{
public static void main(String[] args)
{
try
{
URL myURL = new URL("http://www.google.com/");
System.out.println(myURL.getDefaultPort());
Scanner myScan = new Scanner(myURL.openStream());
System.out.println(myScan.nextLine());
}

catch (Exception e)
{
System.out.println("There is no URL found, this is why we have the try, catch");
// We catch the error, so we do not crash!
e.printStackTrace();
}

}
}
Here we have an object/instance of the URL class. The URL class has different parameters, our myURL object/instance of the class sets the parameter as http://www.google.com/.

What methods can we invoke upon our newly created URL object? What are the methods of the URL class. The code above invokes the getDefaultPort method and openStream method. We create a new Scanner object, notice that we are using the openStream method instead of System.in as before.

Our code outputs the first line of source code from our website. Lets add a loop to capture all of the source code and then we are done for this article.

CODE :
import java.net.URL;
import java.util.Scanner;

public class MyFirstProgram
{
public static void main(String[] args)
{
try
{
URL myURL = new URL("http://www.hackthissite.org/");
Scanner myScan = new Scanner(myURL.openStream());

for (int i = 0; myScan.hasNext(); i++)
{
System.out.println(i + ": " + myScan.nextLine());
}
}

catch (Exception e)
{
System.out.println("There is no URL found, this is why we have the try, catch");
// We catch the error, so we do not crash!
e.printStackTrace();
}
}
}
As long as our myScan onject/instance of the Scanner class can read a line, then it prints it to the console with the index value. We do this by using the hasNext() method of our myScan instance/object of Scanner.

I hope this gives you a start with Java.