Nov 26, 2013 04:15 PM
(This post was last modified: Dec 21, 2014 05:57 AM by WTF¿.)
[img]http://ucttraining.files.wordpress.com/2013/10/java_logo.jpg[/img]
Hey Hack Forums, I'm going to be writing a Java tutorial for you guys! Firstly, I'm going to start by explaining a bit about Java and what makes it different from most other languages. Java runs on what's know as the JVM or Java Virtual Machine, this allows it to run on many different types of computing devices and operating systems. You may have read the Java is slow, but this is not the case it may have been many years ago, but now Java runs as fast as many other programming languages which is why it's being used more and more for applications such as games.
You will need to get a Java IDE, an IDE is sort of a Word for programming, for Java I recommend eclipse.
http://www.eclipse.org/
OK, getting into the practical side of Java programming. Variables are a crucial part to any program, a variable is a small area in memory(RAM) which is given a name and can store a value. types of variables can store different types of values for example an int can store a whole number, floats can store a number with a decimal, strings can store letter numbers and some punctuation. Below is an example of declaring a variable in Java:
Code
int number = 10;Variables can be set to different values at any time in your program, but they only need to be declared once. Also note it is good to get in the habit of naming your variables of what they are and not get random letters.
Now we will move on to selection, selection is when your code can follow a specific route depending on the outcome of a condition/question. If statements are probably the most use form of selection.
Code
if(number == 10){
System.out.println("Number is equal to ten!");
}this if statements would run the code " System.out.println("Number is equal to ten!");" only if number was equal to 10 and if it is not it would simply skip that part of the code. Note: System.out.println prints text into the console window.
As well as having just an if statement you can have else if and else. else if works the same as an if statement, but is used you you want to have more then one possible outcome and else statements are for when you want some to happen only if none of the conditions are met.
Code
if(number == 10){
System.out.println("Number is equal to ten!");
}
else if(number == 5){
System.out.println("Number is equal to five!");
}
else{
System.out.println("Number is equal to something other then 5 or 10!");
}Next I will be introducing you to iteration or loops. The two most common types of loop are while and for loops. a while loop repeats the code inside while the condition is met.
Code
int number = 10;
while(number > 0){
System.out.println("Number is equal to: " + number);
number = number - 1;
}The above example will print out:
Number is equal to 10
Number is equal to 9
And so on...
Loops can be used to repeat code without having to re-write it or when you don't know how many times the code will be repeated for. Note: if you want to add or subtract 1 from a number you can use variable++; or variable--;. Below I will explain the for loop:
Code
int loopCount;
for(loopCount = 10; loopCount =< 0; loopCount--;){
System.out.println("loopCount is equal to: " + );
}Operators are often used in conditions, operators you may already have learned in this tutorial are "==", ">" and "<".
== if equal to
> if greater then
< if less then
=< if equal to or less then
=> if equal to or grater then
|| or "if(number == 1 || numberTwo == 2)"
&& and "if(number == 1 && numberTwo == 2)"
OK guys I have decided to continue my tutorial that I had earlier abandoned. So, moving onto methods. Methods are great, they help you structure your code and can prevent you from having to re-write code. Now we have already been using a method, the main method(As shown below). The main method is the first bit of code that gets executed(run) when your application starts.
Code
public static void main(str[] args){
add(7, 4);
}
public static int add(int number1, int number2){
return number1 + number2;
}If you create a new method as I have done above it will not run unless I tell it to in the main method. You may notice there are some numbers in the brackets when I call/run the method, these are parameters. You choose what parameters you want when you declare a method, so I've told the program that I need two numbers when my method is called and in this case I have given the method 7 and 4. Now you may be wondering about the 'public int' part just before the name of the method don't worry about the public for the moment, we will get into that later. now the int is want we want the method to give us when it is finished, this cam be any data type, so int, string, char, double, float, etc. in the method called add we are returning an integer which is the sum of the two numbers we gave the method.
Arrays if you have not already guessed are lists of variables. Below is an example of declaring an array. An array is not a data type, but an object. the following is an array of integers, in fact it holds 4 integers.
Code
int[] intArray1 = new int[4];
intArray1[0] = 1;
intArray1[1] = 43;Coming soon:
Working with strings
classes/OOP
access modifiers
MySQL
Multi-threading
Networking



![[Image: 9N6Aq1f.gif]](https://i.imgur.com/9N6Aq1f.gif)