A basic start on java.

Questions/Tutorials for any programming language not covered in other sections.
6 posts Page 1 of 1
Contributors
User avatar
Duggypker
New Member
New Member
Posts: 10
Joined: Tue Mar 23, 2010 3:10 am

A basic start on java.
Duggypker
Hello everyone. Duggypker here posting a Java programming tutorial... Well let's get started.

First we will start with loops.

While Loop

The first loop base we will discuss is the While loop basically this says While This statement is true do this.

Here's an EX:
Code: Select all
while(i < 5) {
system.out.println("There are" + i + "Items");
}
}
Now this loop says If i is less then 5. then do the following action which is the log. What this does is in the screen it will say: "There are 2 Items" etc.
BUT i does not have a value. So at the start of the script you must set an int.
Code: Select all
int i = 0;
This sets i to equal 0
int stands for integers intergers are basically any WHOLE number as in 0, 1, 2 etc. OR any negative WHOLE number as in: -1, -2, -3.
EX of a negative Int:
Code: Select all
int i = -1;
But for now we are going to go with 0.
Now as you saw earlier in my while loop code. This code does not have an ending because i will always be less then 0 now to set an ending we can simply do this:
Code: Select all
while(i < 5) {
system.out.println("There are" + i + "Items");
i++; //or i = i + 1
}
}
}
Now a lil advice on the addition code there. i = i + 1 you may think how do you set 0 = 1 ? Well what this means is setting everything that is on the Right of the = sign to the left side so in our int. i = 0 but with this i = i + 1 it will raise everytime by 1 which will make it greater then 5 eventually.

Now that we have a basic knowledge of the increase of int. and the while loop Let's move on. I'm going to stop here to show you a little bit more about Variables.


String Variable

Now a String is basically a letter or word formation in a script. whenever you want to place a sentence in a script it is referred to as a string.

Example of a string would be:
Code: Select all
String Ex = "This is an Example String Variable";
Strings can be used mainly for labeling and making titles in your scripts.


Arrays


Arrays are used to set multiple Variables in one variable. Mainly used to set multiple objects like trees or axes.

An Example:
Code: Select all
int [] myArray1 = {1223, 1225, 1227};
int [] myArray2 = {5553, 5554, 5555};
NOTE: These are just random Numbers to be used as an example.

Arrays can take a heavy load off your back when you have to write a lot of integers.

If and Else


Now a major part to your scripts/programs you will be making is the if and else statements. They declare if it happens, then to do this.

Example:
Code: Select all
 if (i == 0) {
system.out.println("i is equal to 0");
} else {
system.out.println("i is Not equal to 0");
}
}
What this says is if i is equal to 0 then to display "i is equal to 0" but if it is not equal to 0 then it will display "i is Not equal to 0".

I'm sure you saw the
Code: Select all
if (i == 0) {
In there what this does is NOT set the left variable (i) but it says is i equal to 0? or is it not equal to 0? This is also VERY important to know when scripting to know your = from your == remeber
Code: Select all
= this sets the right information to mean the left
i = 0
While
Code: Select all
== just asks if it is equal
i == 0
But now we I am going to get back into loops. Our next loop is called a for Loop.

The For loop

A for loop makes a while loop alot shorter you will more then likely use these alot in your java programs but you will use your Public int loop() in your scripts. Let's get started

A for loop basic setup is like this:
Code: Select all
for(int i = 0; i < 5; i++;) {
system.out.println("There are" + i + "Items");
}
}
}
Now this does the same thing but it is all stated in one line. But note that each event is separated by a ; this is VERY important. As the script will run errors if you do not do this.

Adding and About Breaks


Breaks in your script can change the way your script works completely but it can also work to be Very beneficial to you. Let's look at how the break is set u and I will explain to you what it does after we set it up.
Code: Select all
for(int i = 0; i < 8; i++;) {
system.out.println("There are" + i + "Items");
if (i == 5) {
system.out.println("i is equal to 5!");
break;
}
}
}
Now what this does is when the number reaches 5 the loop will stop. It will not continue it will stop and move on outside of the brackets because the break; in there breaks the loop.


Adding and About Continue


Now continue is basically the Same as breaks but the opposite. It continues on with the script.
Code: Select all
for(int i = 0; i < 8; i++;) {
log("There are" + i + "Items");
if (i == 5) {
log("i is equal to 5!");
continue;
}
log("Example Text");
}
}
Once the code hits that continue in there it will skip log("Example Text"); and continue on. But once the script loops it will then re add the log("Example Text"); and continue on to its max.

Cases/Switches


Ah Cases. One of the newer ways to script but it is also the most unused formation. Case and switch are the same thing. First were going to have to set a String OR and int in our variables.
Code: Select all
int status = 0;
OR
String status;
Now we have our variables set. Now we can move on to the loop part of cases.
Code: Select all
switch(status) {

Case 0:
if (i < 1) {
status = 1;
} else if (i > 1) {
status = 2;
}

Case 1:
system.out.println("i is less then 1. Cool!");
i++;

Case 2:
system.out.println("i is greater then 1. Uh oh.");
int i = 0;
}
}
the status = #; says what case it is going to set to. Basically what this code does is if i is less then 1 it will go to case 1 which will display a message and then add 1 to i and if i is greater it will display a message and then reset i to 0.

This concludes the basic Java tutorial. I will update this as needed, and later on get a little more advanced for you guys :) Thanks for taking the time to read.

Please do post questions or if you spot a fault tell me! Thanks! <3

~Duggypker
Image
Current Project: From the Ashes RTS
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Re: A basic start on java.
hungryhounduk
Hi DuggyPker
I am sure this will help many people out who know nothing about Java ( like me :) )

Nice One

Chris
Image
User avatar
bjm0008
Dedicated Member
Dedicated Member
Posts: 69
Joined: Thu Aug 13, 2009 2:35 am

Re: A basic start on java.
bjm0008
I am interested in learning Java and have no clue what software is needed.
On the Java website I get all these different links.
Please PM me with a download link or purchase page(Hope it's little-to-no cost :))
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: A basic start on java.
mandai
The Java IDE is free - it is called NetBeans.
You can get it from netbeans.org.
User avatar
bjm0008
Dedicated Member
Dedicated Member
Posts: 69
Joined: Thu Aug 13, 2009 2:35 am

Re: A basic start on java.
bjm0008
Thanks, but I actually looked into it and I'm now using 'Eclipse' to edit my java files.

I've been at it for about an hour now and things couldn't be better, I understand everything thanks to:
http://www.youtube.com/user/thenewboston
All of the tutorials for java are outstandingly thorough.
Image
User avatar
Freeskill
Just Registered
Just Registered
Posts: 8
Joined: Wed Jun 30, 2010 7:46 am

Re: A basic start on java.
Freeskill
Nice one thanks, it will help a bunch :D goofy;
6 posts Page 1 of 1
Return to “Misc”