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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • I had friends who played on Elegant and I can confirm it wasn't a bad server. I don't know about the AI bots running around, but in my opinion the price is simply too high. I don't think many people will spend that much money on Lineage 2 files when alternatives like L2Scripts already exist.   Of course it's all about personal preference, but for such an old chronicle the price seems ridiculously expensive. Modern chronicles usually offer more value.   Anyway, I wish you the best of luck with the sale. This is just my personal opinion. Whoever likes it, good for them, and whoever doesn't, that's fine too. Everyone has different preferences.
    • https://l2avokado.com/ Hello everyone,   After some time of development, we've decided to open L2Avokado to the public in its current development stage. We're looking for players who enjoy Interlude and would like to help shape the project before its official release.   This isn't a "launch" announcement. Instead, we're inviting the community to log in, explore the server, test the systems we've built, and provide honest feedback. Whether it's bug reports, balance suggestions, progression ideas, or quality-of-life improvements, we'd love to hear them.   Our goal has always been to create an Interlude server that feels familiar while offering a fresh progression experience. We've intentionally avoided custom weapons, armor, and client modifications. Instead, we've focused on redesigning progression through reworked hunting grounds, quests, crafting, and gameplay systems while remaining compatible with a clean Interlude client.   At this stage, the core progression path has been implemented, including the main hunting grounds, quests, custom systems, and events. However, as the project is still under active development, there will inevitably be bugs, balance issues, and areas that require further polishing.   This is exactly why we'd like your help.   We're looking for players who are willing to: Test gameplay and progression. Report bugs and exploits. Suggest balance improvements. Share ideas for new features or quality-of-life changes. Help us build a server that the community genuinely enjoys playing.   The Client and System downloads are already available on our website, so you can jump straight into the game. We're also working on a dedicated launcher that will simplify installation and future updates.   If you're interested in helping develop a unique Interlude project and want your feedback to genuinely influence the direction of the server, we'd love to have you with us.   We look forward to seeing you in-game and hearing your thoughts on Discord. https://l2avokado.com/
    • Mate your server used to play with full of bots, from where you find that revenue? 
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..