Chapter 1: Introduction
Java looks like C++, but is different because there does not exist any pointers in it. Wich you don't need in Java. Java is object oriented, it doesn't matter if you don't know much about the content.
For very simple program's that does not matter but you need to begin execution of your graphics programs or some deeper techniques.
Java is an interpreted language, this means that Java can be performed on different Operating Systems E.G under Windows and Linux without recompiling the program.
The disadvantage, This is a consequence of the fact that Java is interpreted. The speed of Java programs compared to C++ programs is limited. Because Java is still pretty much in development, sometimes things disappear from old versions.
In this tutorial you will be learning about some of the more basics of the Java programming language. Since barley no one goes to the Java.Sun websites, I will post my own version. However, the Java tutorials can be found
here.
First I'm going to start off with some basic and more common java keywords. It is important to know these words if you get errors in your codes, otherwise you won't know how to fix them.
Useful DownloadsJava JDK & JRE DownloadsOfficial Java tutorialsJcreator &
EclipseChapter 2: Downloading and installing the JDK/JRE
We are going to download the JDK and the JRE, in one installer.
So just follow the steps below and we'll be done in no time!
First, you need to go to the download page of Java
here.
Now click the "Download JDK" button.
Now, choose your computer platform and click on the "Download >>" button.
If it asks you to Log In click on the ">> Skip this Step" button.
Then wait untill it's done with downloading.
Click on everything that looks like a hard-drive and make sure you select "This feature and all subfeatures will be installed on local hard drive." especially for the option that says "Public JRE"
Click "Next >" and let the installer to its thing.
You have successfully installed the JDK and the JRE, now we're moving to the next chapter.
Chapter 3: Setting the PATH and CLASSPATH
Now that you have installed the JDK and the JRE, they won't do anything until you let the system know that they exist. Click Start, move your mouse to My Computer and Right-Click it. Click "Properties".
So now we have this screen in front of us. Well, don't worry, that's just the System Properties Control Panel. It should look somewhat like this:
Now, Click the tab that says "Advanced". Click the button that says "Environmental Variables" You should see something like this:
In the box on the
BOTTOM, scroll down until you see "PATH" or "Path" on the left column. Double-click that. Go to the very end of the input box on the bottom and type in:
[/list]
;C:\Program Files\Java\jdk1.6.0\bin;.
You must include the semicolons[;] and the period[.]. Now select everything in the bottom input box and press "C" while holding "Ctrl". Press OK. Now, almost at the very bottom of the Environment Variables screen, there should be a button that says "New". Click that. In the top input-box type in
Classpath
and in the bottom, press "V" while holding "Ctrl". Press OK until the System Properties window disappears.
Now you're ready to run and compile Java programs.
Chapter 4: Keywords
Boolean -
Indicates whether a value is true or falseBreak -
Jumps out of a loop or switchByte -
Indicates that a value is an 8-bit whole numberCase -
Introduces one of several possible paths of execution in a switchClass -
Introduces a class-a blueprint for an objectElse -
Introduces statements that are executed when the condition in an if statement isn't trueEnum -
Creates a newly defined type-a group of values that a variable can haveIf -
Tests to see whether a condition is true. If it's true, the computer executes certain statements; otherwise, the computer executes other statementsNew -
Creates an object from an existing classPrivate -
Indicates that a variable or method can be used only within a certain classPublic -
Indicates that a variable or method can be used in subclasses from another packageSwitch -
Tells the computer to follow one of many possible paths of execution (one of many possible cases), depending on the value of the expressionThis -
A self-reference-refers to the object in which the word this appearsTry -
Introduces statements that are watched (during runtime) for things that can go wrongVoid -
Indicates that a method doesn't return a valueChapter 5: Java Symbols and Punctuation
This is quite a short chapter, you will learn the basic Java Symbols and Punctuation, and learn that each and every one of the symbols and punctuations, has a purpose, and without them it would be meaningless.
Every one of the bolded symbols has a meaning, if you look below, you will see what role a curly bracket ({, }) plays in a Java code.
If you notice, the curly bracket acts like a box around the code, when you start, your code goes inside your main class, in this case the class is "DeathSpiritIsLeet" and you keep building things inside of the main class, which has a meaning. If you were to code like in the picture below, the code is meaningless, as nothing is inside the main class.
PunctuationPunctuation is key in the Java Programming Language. A simple switch of an uppercase and lowercase letter can change the meaning of a code to nothing at all, or something you didn't want to happen.
If you were to change this lines uppercase letters to lowercase, and vise versa, the code goes from:
System.out.println("I am leet");
A meanful code.
system.Out.Println("I am leet");
To a completely worthless and unmeaningful code.
It is also extremely important that you know when naming classes,
they must be in all Caps(uppercase) or each word must start with a capital, like I have in my code.
Variables don't start with capital letters. They follow the format lowercaseUppercase and continue uppercase for every word. An example would be:
if(DeathSpiritIsLeet) {
Don't name classes "class public" because public is a keyword and posses a meaning in the Java Language. This goes for all other keywords as well.
IndentationWhen coding java, you will learn to indent your code to organize it, indentation is very easy, but it is very important you learn to do it properly. Bad indentation is not a habit you want to have. The code below, is good indentation.
if(DeathSpiritIsLeet) {
System.out.println("DeathSpirit is leet!");
} else {
System.out.println("DeathSpirit isn't leet!");
}
This code has good indentation because everything is aligned, the code below is bad indentation because everything is indented differently.
if(DeathSpiritIsLeet) {
sendMessage("DeathSpirit Is Leet!!!);
} else {
System.out.println("DeathSpirit isn't leet!");
}
SlashesIn Java, there are things you can do to write a message in a code, but make it meaningless. In your server, you probably have seen "//" after some codes. This will do nothing, as long as you have two slashes at the end of the code.
System.out.println("I am leet"); //Displays message on the screen
This code is fine, and you can write whatever you want after the two slashes, and you will have no errors.
There is also another way to do this. Generally at the top of most programs you probably see something like:
/*
* Made by DeathSpirit
*
*/
This is another way to do the same thing as above, but it is usually for more of a credit based thing. It can also be useful when you are trying to figure out what a code does, so you put a "/*CODEHERE */" around it which will disable that particular code or line.
Chapter 6: Very Simple Programs
This is a very short Chapter, you will learn probably the most basic program, and what it does.
We are going to take the code above, and explain all of it's functions.
class DeathSpiritIsLeet {
public static void main(String args[]) {
System.out.println("I am leet");
}
}
"class DeathSpiritIsLeet" is a new class, when you name a new class, be sure to name it something that is meaningful, I don't suggest naming a class "CuddlePuppy" or something cute like that. It's not very meaningful and may confuse you. "public static void main(String args[])" pretty much explains itself, but nonetheless, it is the main void. "args" has no meaning, just like "DeathSpiritIsLeet," it's just a name, I could name my string "(String goat[])" if I wanted to. "System.out.println("I am leet");" displays the text "I am leet" on the screen, or depending on the program you run it in, the output box.
Chapter 7: Teaching Methods
In Java many codes are explained by more the less a to-do-list, or a step-by-step guide on doing something. I could write a could for making a peanut butter sandwich, which would probably look a lot like this.
void MakePeanuutButterSandwich() {
walkInto(feet, kitchen);
Open(breadbag);
Grab(bread);
Open(cupboard);
Grab(peanutbutter);
Open(lid);
Open(drawer);
Grab(knife);
Spread(penutbutter);
Fold(sandwich);
Eat(sandwich);
}
However, that method is meaningless unless it is called upon somewhere else.
MakePeanutButterSandwich(peanutButterSandwich);
I am not by any means saying this code will work, it's just an example.
When writing a method, it must have four things:
- Method Header
- Method Body
- Method Declaration
- Method Call
The diagram below illustrates each part of the method.
Chapter 8: Java Statements
In this chapter you will learn the most commonly used Java statements, what they do, and what they can be used for. I will only be explaining a few of them, as the other ones may be a little bit more advanced. You can learn those with a quick search on google.
The Break Statementbreak;
The If Statementif(expression) {
//statements
} else {
//statements
}
The Switch Statementswitch(expression)
{
case constant;
statements;
break;
default;
statements;
break;
}
The Throw Statement throw (exception)
The Try Statement try {
//statements
} catch (exception-class e) {
//statements
} finally {
//statements
} try {
//statements
} finally {
//statements
}
The Break StatementFirst off we are going to explain the break; statement, it's meaning, and what it's used for.
A break jumps out of a loop or a switch, and even in our above examples you can see it in the switch statement. Some times we need to exit from a loop before the completion of the loop then we use break statement and exit from the loop and loop is terminated. The break statement is used in while loop, do - while loop, for loop and also used in the switch statement.
The If StatementThe If statement heavily relies on the use of boolean expression (a boolean indicates whether a value is true or false). You may not even do the simplest if statement without a boolean expression. I will talk about the use of boolean expressions later on (may not get added for a day or two). The if statement is probably the most common statement in the java language.
In the most basic if statement you may execute a single statement or a block of statements only if a boolean expression returns true. A simple if statement can look like this.
if(DeathSpiritIsLeet) { //DeathSpiritIsLeet must be declared as a boolean expression
System.out.println("DeathSpirit is leet!");
}
When opening an if statement you use curly brackets, in my style of programming anyway
it just makes things easier to understand) to start and finish the statement. I suggest putting the opening curly bracket on the same line as the if statement (again that is just my preference).
Generally if statements are in way like to-do-lists. If the boolean expression returns true, then you follow the statements. If not, you skip the statements. I could make a code like this
private boolean DeathSpiritIsLeet = true;
if(DeathSpiritIsLeet) {
System.out.println("DeathSpirit is leet!");
}
and everything would work because the boolean expression is true, so the code carries on. Now if I was to make a boolean expression with DeathSpiritIsLeet = false, the code changes, and the System.out.println message is skipped. Use the image below for more clarification.
With the if statement comes the else statement. The if statement stays the same as explained above, except instead of skipping the statement if the value returns false, it goes to the else statement, where you can make the message "DeathSpirit is leet" regardless of whether the value returns true or false as long as you have System.out.println("DeathSpirit is leet!"); on both sides of the else statement. You can also make it display a different message, or change the statement to whatever you want. It really is up to you. This is an example of an if and else statement where if the value returns true "DeathSpirit is leet" is displayed, and if the value returns false, "DeathSpirit isn't leet" is displayed.
if(DeathSpiritIsLeet) {
System.out.println("DeathSpirit is leet!");
} else {
System.out.println("DeathSpirit isn't leet!");
}
The Throw StatementThe throw statement is quite simple. You cannot throw a new exception if the object is not throwable. To make an object throwable you can use the throws statement. The throw statement can also be incorporated with the if statement, so instead of the earlier example, you could use this:
if(DeathSpiritIsLeet) {
throw new LeetBarException();
}
To make the LeetBar object throwable, somewhere else in your code you need to have
throws LeetBarException();
for it to work properly.
If I feel up to it later I may add the try and switch statements.
Chapter 9: Boolean Expressions
This will be a very short chapter, as I will just be teaching the basics of booleans and what role they play.
Booleans are very easy to understand. As we should've learned by now, booleans indicate whether a value returns true or false.
Chapter 10: Java Operators
Operators are just basic mathematical sings and other types of sings that you will probably have seen before, that posses meanings in the Java language.
There are three different types of Operators. They are:
- Arithmetic
- Relational
- Logical
Arithmetic+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
++ Increment
-- Decrement
+= Addition and assignment
-= Subtraction and assignment
*= Multiplication and assignment
/= Division and assignment
%= Remainder and assignment
Relational== Equal
!= Not equal
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Logical! Not
& And
&& Conditional and
| Or
|| Conditional or
^ xor
Arithmetic OperatorsArithmetic Operators are extremely simple. I won't even make up equations for you as Math is what you learn at school, I won't torture you.. If you know how to add, subtract, multiply, and divide, you are pretty much capable of understanding Arithmetic Operators.
Relational OperatorsRelational operators are very simple as well. You will have seen most of the operators (arithmetic, relational, and logical) before if you have ever seen a server. These can also be used elsewhere in java math. Just be logical when using them, make sure everything makes sense, and you've used the right operator.
Logical OperatorsA logical operator is another word for a boolean operator. A logical operator is an operator that returns a boolean result (true or false) that is based on the boolean result of one or two other expressions. Expressions that use logical operators are usually called compound expressions because they allow you to combine two or more expressions into one single expression. Below is a list of the operators, their names, their type and their description.
Operator ! - This operator is named "not," and is a unary operator. This operator returns true if the operand to the right returns false, and if the operand to the right returns true, this operator returns false.
Operator & - This operator is named "and," and is a binary operator. This operator returns true if both of the operands evaluate to true. Both operands are evaluated before the "and" operator is applied.
Operator && - This operator is named "conditional and," and is a binary operator. This operator is the same as &, but if the operand on the left returns false, it is returned false without evaluating the operand on the right.
Operator | - This operator is named "or," and is a binary operator. This operator returns true if at least one of the operands evaluates to true. Both operands are evaluated before the "or" operator is applied.
Operator || - This operator is named "conditional or," and is a binary operator. This is the same as |, but if the operand on the left returns true, it returns true without evaluating the operand on the right.
Operator ^ - This operator is named "xor," and is a binary operator. This operator returns true if one and only one of the operands evaluates to true. If both operands evaluate to true or if both operands evaluate to false, it will return false.
I will be adding more, this will get you started for a while if you are just learning
So yeah, I hope you enjoyed my tutorial so fair..