Java Tutorials
Building Hadoop MapReduce Jobs In Java
Building XML With Java And DOM
Creating Java Servlets With NetBeans
Basic File Manipulation With Java
Introduction To Java

Introduction To Java

What is Java?

Java is an open source object oriented programming language developed by Sun Microsystems. The Java language is commonly used to build a variety of client applications and web systems. Java has been growing in popularity and usage in the field since it's release, and the Java platform was designed to be cross platform compatible with the under the hood work done by the Java Runtime.

Basic Syntax

With Java everything is an object. Each object is represented by a class and is stored in a .java source file. Java programs are a collection of classes that work together.
Create a new file HelloWorld.java

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

Before we can execute this, we must compile it. With Java you use the javac command to compile a java class. This will be the only place in this tutorial where we cover compiling as it's the same process for the entire tutorial.

$ javac HelloWorld.java
$ java HelloWorld
Hello World

The class should have the same name of the .java file. Functions in Java are commonly called methods. The method main is used as the function to call when the program is executed. Methods will be covered in detail later. Values passed to the program from the command line will be stored in the String array args. The System.out.println method is called and passed Hello World. System.out.println pushes to the display (STDOUT) the passed argument followed by a newline. All code blocks in Java are delimited with { }.

Working with command line variables

Command line variables provide a simple way for the user to pass data and manipulate the runtime of the application. Command line variables are space delimited and multi word strings need to be properly quoted or escaped. See your operating system or terminal documentation for exact syntax (usually surrounded with "" or before the space).
Create a new file CommandLineEx.java

 class CommandLineEx {
     public static void main(String[] args) {
         System.out.println(args[0]);
     }
 }

This code functions exactly like HelloWorld.java except the printed string comes from the first argument passed to the program. All other options passed on the command line are ignored. See the for loop example for a more complete example of parsing command line variables.

Basic Variable Types

Java variables are typed and must be declared before use. Java has 8 primitive data types. A byte is an 8 bit signed integer with an allowable value between -128 and 127. A short is a 16 bit signed integer with an allowable value between -32738 and 32767. An int is a 32 bit signed integer with an allowable value between -2,147,483,648 and 2,147,483,647. A long is a 64 bit signed integer with an allowable value between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
Create file BasicDataIntEx.java

class BasicDataIntEx {
    public static void main(String[] args) {
        byte b = 5;
        byte b2;
        short s = 48;
        short s2;
        int i = 100;
        int i2;
        long l = 1000;
        long l2;
        
        i2 = i / b;
        l2 = l * s;
        System.out.println(i2);         
    }
}

First initialize 2 variables each of byte, short, int long types, and perform some math operations on the values and print. Math operations cannot be stored into byte or short types, but they can be used in mathmatical operations stored into int or long types.
Adjust BasicDataIntEx.java so the math operations and output are:

        i++;
        l--;
        i+=10;
        l-=100;
        
        System.out.println(i2);
        System.out.println(l2);
        System.out.println(i);
        System.out.println(l);

When an integer style variable is followed by ++ the variable is incremented, and -- it is decremented. If these statements happened to be part of an equation a = b++; a would contain the value of b before the incrementing. Also a = --b; a would be one less than the value of b, but b would remain unchanged. The code i += 10 is shorthand for i = i + 10; and the code l-=100 is similar shorthand for l = l - 100.

The rest of the Java primitive data types are, float which is a 32 bit single precision IEEE 754 floating point number. Double, a 64 bit single precision IEEE 754 floating point number. The char, a single 16 bit unicode character. And boolean which is the standard true/false boolean type.
Create the file FloatDoubleEx.java

class FloatDoubleEx {
    public static void main(String[] args) {
        float pi = 3.14f;
        float r = 4f;
        float c;
        
        c=pi*(r*r);
        System.out.println(c);
    }
}

You might notice this is simply the formula to find the circumference of a circle. Floating point numbers should be denoted with a trailing f. Now change the variable types to double. Also change the trailing f to a d to denote double.
Create a file CharBooleanEx.java

class CharBooleanEx.java {
    public static void main(String[] args) {
        char a = "b";
        boolean b = true;
    }
}

We don't do much with these at this point but show you how to declare them.

Arrays, Strings and Other Common Types

Arrays

All primitive types can be turned into arrays. Arrays are simply indexed lists of the type in which they are initialized. Arrays are initialized by adding [] to the type when declaring the variable.
Create a file ArrayEx.java

class ArrayEx {
    public static void main(String[] args) {
        int[] iArr;
        char cArr[] = {'F', 'a', 'k','e', 'S', 't', 'r', 'i', 'n', 'g'};
        
        iArr = new int[4];
        iArr[0] = 1;
        iArr[1] = 10;
        iArr[2] = 20;
        iArr[3] = iArr[1] * iArr[2];
        System.out.println(iArr.length);
 
        System.out.println(cArr.length + " - " + cArr.toString());
    }
}

First initialize an integer array and a character array. The character array is not a real string as the example will show. You need to use the String type explained next for that. Notice that the first index of the array is 0. So the integer array iArr has 4 indexes numbered 0-3.

Strings

Strings in Java are implemented through java.lang.String. java.lang.String provides a good interface for the storing and manipulation of string data.
Create the file StringEx.java

class StringEx {
    public static void main(String[] args) {
        String s = "new string";
        
        System.out.println(s);
        System.out.println("Length: " + s.length());
        System.out.println("Number of words: " + s.split(" ").length);
    }
}

Initialize a new String s with the value "new string" without the quotes. Print the string. Then print the length of the string. The split method accepts a string as an input and uses that to break the string into an array of strings. We then take the length of that array. In the example we split on a space and use that to calculate the number of words in the string.

 1 2 3  >> Conditionals and Loops
New Content