Post Reply 

Share on Facebook Share on Twitter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Tutorial
Author Message
Ironside Offline
Site Owner

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #1
Java Tutorial
Java Tutorial

1. Order of Operation
..1.1 Operator Precedence
2. Nested Flow Control Statements
..2.1 Nested If Statements
..2.2 Nested While/For Loops
3. Using Classes
..3.1 Instantiating a Class
..3.2 String Class
..3.3 Math Class
..3.4 Arrays
4. Methods
..4.1 Using Methods
..4.2 Creating Methods
5. File Input/Output
..5.1 File Input
..5.2 File Output
6. End

1. Order of Operation

It is important to understand operator precedence.

Go here for a

complete list of

operator precedence.

2. Nested Flow Control Statements

It is very useful to know about nested flow control statements.

2.1 Nested If Statements

An example of a nested If statement:
if (a)
    if (b)

This nested If statement first tests whether or not a

is true.
If so, it proceeds and then tests whether or not b is

If so, it proceeds to call the do() method.

This can't be written as if (a && b)?
Yes, it could be written that way; however, there may be special cases where someone will find

it practical to use

a nested if statement.
Such as:
if (a)
    c++; // we want to increment C regardless of /b/'s value.
    if (b)

2.2 Nested While/For Loops

An example of a nested For loop inside a While loop:
boolean input = true;
while (input)
    for (int i = 0; i < 3; i++)

This code block will print out 3 to 1, each number on a separate line.
You may have noticed that this will continuously print out:

Every pass through the while loop, it makes 3 passes through the for loop.
Since input is always true, while's condition will always return true; thus, we have an infinite


3. Using Classes

Using classes is another very important part of Java programming.

3.1 Instantiating a Class

Most classes need to be instantiated before being used.

Instantiating is the act of creating an instance of a class; an object.

The syntax for instantiation is:
ClassName identifier = new ClassName;

However, if the class requires any parameters to be passed, it could look this way:
ClassName identifier = new ClassName(1, 2, 3);

The following is also valid:
ClassName identifier;
identifier = new ClassName;

The first line only creates a reference variable, a variable that points to an object.
Since we have not instantiated an object, there is nothing to point to; thus, we have a

null pointer

You may have guessed that a null pointer points to nothing.
This would be a good opening to explain Java's garbage collection.
When a reference variable stops pointing to an object, and no other reference variables are

pointing to it, that

portion of the memory is automatically freed up and available.
This is called garbage collection.

3.2 String Class

Java uses a String class for string-type variables; however, it is quite different from normal

While the new operator can be used to instantiate a String object, it is not

(Only true with the String class. No other classes can be instantiated without the


So this makes creating String objects much like creating regular variables of primitive data

A String object can also be represented as an array of characters.

String name = "name";
String lastName;
lastName = "last name";

3.3 Math Class

The Math class is also unique. The Math class is static; therefore, its methods can be invoked

directly through dot

notation, without an object being instantiated.

Example of square root function:
System.out.println(Math.sqrt(16)); // displays 4
System.out.println(Math.sqrt(4)); // displays 2

3.4 Arrays

Arrays can be difficult to grasp for a newborn programmer. I will try to explain them the best I

An array holds multiple values under one identifier. To reference these values, you use an index

number (or

subscript) between two brackets, [].

int[] grades = new int[2];
grades[0] = 10;
grades[1] = 50;
grades[2] = 100;

The above example instantiates an array of int type, and of size 2.
Array indexes start at 0, so it can technically hold 3 values. In index 0, 1, 2 and 3.

You can also instantiate an array like so:
int grades[] = new int[2];

I will be instantiating arrays this way from now on.

Arrays are also easier to manage than multiple variables.
Let's say you had 100 values you wanted to add 1 to.

Which of the following would you prefer?



for (int i = 0; i < 100; i++)

I think 100 lines vs 2 lines speaks for itself here. Smile

4. Methods

Now, on to methods. Isn't this fun? Big Grin

Methods are used to break up your driver's code and used for segments of code

that would

usually be repeated.

4.1 Using Methods

Methods are fairly simple to use. They may return any type of data, or they may return nothing.
A method with the return type void will not return anything.
To call a method, you simply use the name of the method and if it has any parameters, include


Example of a void method:

This method obviously displays some numbers. It requires no parameters, and has nothing to


Example of an int method:
int x;
x = add(1, 3); // x = 4

This method returns an integer, and in this case, it returns 4, the sum of 1 and 3.
If it's not obvious already, to store the value a method returns, you need to store it in a

variable of the same data


4.2 Creating Methods

Methods consist of a method header and a body of code.
In the method header, we have a visibility modifier, a return

, a method

name and a list of parameters

public int add(int a,


The above is the header for a method that adds two numbers together and returns the sum.
public being the visibility modifier, which decides who can access

this method.
More on visibility modifiers will be discussed in Encapsulation.
int is the data type of the value we return when the method is finished.
"add" is the name of our method, the name we will use to call it.
And then we have the parameter list, which defines variables that are local to the method.
More on variable scopes will be discussed in Scopes.

The full method looks like:
public int add(int a, int b)
    return a + b; // return sum of a and b


The return statement returns the value given to it and ends the method

regardless of its position.
11-12-2010 05:04 PM
Find all posts by this user Quote this message in a reply
Ethereal Offline

Trade Count: (0)
Posts: 39
Joined: Jan 2011
Reputation: 5
Post: #2
RE: Java Tutorial
An awesome tutorial.

I've been meaning to look into Java, seeing as C# syntax was partially based off of it. It seems pretty familiar to CoD patches too.
01-15-2011 06:06 PM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #3
RE: Java Tutorial
Yes, C# has a quite similar syntax to Java.
01-15-2011 06:07 PM
Find all posts by this user Quote this message in a reply
Post Reply 

Forum Jump:

User(s) browsing this thread: