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:

Code:

3
2
1

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

loop.

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.

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

The syntax for instantiation is:

Code:

ClassName identifier = new ClassName;

However, if the class requires any parameters to be passed, it could look this way:

Code:

ClassName identifier = new ClassName(1, 2, 3);

The following is also valid:

Code:

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

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

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

new

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

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

Code:

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

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:

Code:

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?

Code:

var1++;
var2++;
var3++;
...
var99++;
var100++;

or

Code:

for (int i = 0; i < 100; i++)
var[i]++;

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

4. Methods

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

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

them.

Example of a void method:

Code:

displayNumbers();

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

return.

Example of an int method:

Code:

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

type.

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

type, a method

name and a list of parameters

public intadd(int a,

int b)

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:

Code:

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