Jump to content

Recommended Posts

Posted

Java Tutorial #1

Java Tutorial #2

Outline

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)
       do();

 

This nested If statement first tests whether or not a

is true.

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

true.

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)
       die();
}

 

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++)
       System.out.println(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:

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:

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

 

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.

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

 

can.

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?

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

or

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? :D

 

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:

displayNumbers();

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

 

return.

 

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

 

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 int add(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:

public int add(int a, int b)
{
   return a + b; // return sum of a and b

}

 

 

*Please give me critical feedback. I have NOT proof read this, so please, please tell me if you spot any typos or mistakes

*Tutorial was writed about 1-2 hours by me and my friend programer (htpp://hackforum.net)

*I hope this tutorial helps anyone in some way. ::)  Took some time.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...