Remove Ads

Share on Facebook Share on Twitter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Tutorial
#1
[size=13]Java Tutorial[/size]

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








[size=13][b]1.[/b] Order of Operation[/size]

It is important to understand [i]operator precedence[/i].

Go here for a

complete list of

operator precedence.





[size=13][b]2.[/b] Nested Flow Control Statements[/size]

It is [u]very[/u] useful to know about nested flow control statements.

[size=11][b]2.1[/b] Nested If Statements[/size]

An example of a nested If statement:
[code]if (a)
if (b)
do();[/code]

This nested If statement first tests whether or not [font=courier][color=orange]a[/color][/font]

is true.
If so, it proceeds and then tests whether or not [font=courier][color=orange]b[/color][/font] is

true.
If so, it proceeds to call the [font=courier][color=aqua]do()[/color][/font] method.

[color=yellow]This can't be written as [font=courier]if (a && b)[/font]?[/color]
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:
[code]if (a)
{
c++; // we want to increment C regardless of /b/'s value.
if (b)
die();
}[/code]


[size=11][b]2.2[/b] Nested While/For Loops[/size]

An example of a nested For loop inside a While loop:
[code]boolean input = true;
while (input)
for (int i = 0; i < 3; i++)
System.out.println(3-i);[/code]

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[/code]

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.


[size=13][b]3.[/b] Using Classes[/size]

Using classes is another very important part of Java programming.


[size=11][b]3.1[/b] Instantiating a Class[/size]

Most classes need to be instantiated before being used.

[color=yellow]Insantiate?[/color]
Instantiating is the act of creating an instance of a class; an object.

The syntax for instantiation is:
[code]ClassName identifier = new ClassName;[/code]

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

The following is also valid:
[code]ClassName identifier;
identifier = new ClassName;[/code]

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 [i]

[color=lime]null pointer

[/color][/i].
You may have guessed that a null pointer points to nothing.
This would be a good opening to explain Java's [color=lime][i]garbage collection[/i][/color].
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 [i]garbage collection.[/i]



[size=11][b]3.2[/b] String Class[/size]

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

classes.
While the [font=courier]new[/font] operator can be used to instantiate a String object, it is not

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

[font=courier]new[/font]

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";[/code]


[size=11][b]3.3[/b] Math Class[/size]

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:
[code]System.out.println(Math.sqrt(16)); // displays 4
System.out.println(Math.sqrt(4)); // displays 2[/code]


[size=11][b]3.4[/b] Arrays[/size]

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, [].

Example:
[code]int[] grades = new int[2];
grades[0] = 10;
grades[1] = 50;
grades[2] = 100;[/code]

The above example instantiates an array of [font=courier]int[/font] 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];[/code]

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++;[/code]

or

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


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


[size=13][b]4.[/b] Methods[/size]

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

Methods are used to break up your [color=lime]driver[/color]'s code and used for segments of code

that would

usually be repeated.

[size=11][b]4.1[/b] Using Methods[/size]

Methods are fairly simple to use. They may return any type of data, or they may return nothing.
A method with the return type [font=courier]void[/font] 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();[/code]

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[/code]

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.


[size=11][b]4.2[/b] Creating Methods[/size]

Methods consist of a method header and a body of code.
In the method header, we have a [color=lime]visibility modifier[/color], a [color=lime]return

type[/color], a method

name and a list of parameters

[color=blue]public int[/color] [color=white]add[/color]([color=blue]int[/color] a, [color=blue]

int[/color] b)

The above is the header for a method that adds two numbers together and returns the sum.
[color=blue]public[/color] being the [i]visibility modifier[/i], which decides who can access

this method.
More on visibility modifiers will be discussed in [b]Encapsulation[/b].
[color=blue]int[/color] 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 [b]Scopes[/b].

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

}[/code]

The [font=courier]return[/font] statement returns the value given to it and ends the method

regardless of its position.
Reply
#2
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.
Reply
#3
Yes, C# has a quite similar syntax to Java.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)