Gå til innhold

Hva skjuler det seg bak din Ctrl+v?


Anbefalte innlegg

Videoannonse
Annonse

Alright, 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. If you would like to view the full list, go here.

 

Chapter 1: Keywords

Boolean - Indicates whether a value is true or false

Break - Jumps out of a loop or switch

Byte - Indicates that a value is an 8-bit whole number

Case - Introduces one of several possible paths of execution in a [iurl=#switch]switch[/iurl]

Class - Introduces a class-a blueprint for an object

Else - Introduces statements that are executed when the condition in an [iurl=#if]if[/iurl] statement isn't true

Enum - Creates a newly defined type-a group of values that a variable can have

[anchor=if]If[/anchor] - Tests to see whether a condition is true. If it's true, the computer executes certain statements; otherwise, the computer executes other statements

New - Creates an object from an existing class

Private - Indicates that a variable or method can be used only within a certain class

Public - Indicates that a variable or method can be used in subclasses from another package

[anchor=switch]Switch[/anchor] - Tells the computer to follow one of many possible paths of execution (one of many possible cases), depending on the value of the expression

This - A self-reference-refers to the object in which the word this appears

Try - Introduces statements that are watched (during runtime) for things that can go wrong

Void - Indicates that a method doesn't return a value

 

Please Note: I do not take credit for these definitions, they are directly from the book Beginning Programming with Java For Dummies.

 

 

Chapter 2: 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.

 

Code:

class RyleeIsLeet {

 

public static void main(String args[]) {

System.out.println("I am leet");

}

}

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.

 

Code:

class RyleeIsLeet {

Quote:

public static void main(String args[]) {

Quote:

System.out.println("I am leet");

}

}

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 "RyleeIsLeet" 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.

 

Code:

class RyleeIsLeet {

}

Code:

public static void main(String args[]) {

}

Code:

System.out.println("I am leet");

Punctuation

Punctuation 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:

 

 

Code:

System.out.println("I am leet");

A meanful code.

 

 

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:

 

 

Code:

if(ryleeIsLeet) {

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.

 

Indentation

When 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.

Code:

 

Code:

if(ryleeIsLeet) {

System.out.println("Rylee is leet!");

} else {

System.out.println("Rylee isn't leet! :(");

}

This code has good indentation because everything is aligned, the code below is bad indentation because everything is indented differently.

Code:

 

Code:

if(ryleeIsLeet) {

sendMessage("Rylee Is Leet!!!);

} else {

System.out.println("Rylee isn't leet! :(");

}

Slashes

In 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.

 

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:

 

 

Code:

/*

* Made by Rylee

*

*/

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 3: 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.

Code:

 

Code:

class RyleeIsLeet {

 

public static void main(String args[]) {

System.out.println("I am leet");

}

}

"class RyleeIsLeet" 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 "RyleeIsLeet," 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 4: 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.

 

 

Code:

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.

 

 

Code:

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 5: 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 Statement

 

 

Code:

break;

The If Statement

 

 

Code:

if(expression) {

//statements

} else {

//statements

}

The Switch Statement

 

 

switch(expression)

Code:

{

case constant;

statements;

break;

default;

statements;

break;

}

The Throw Statement

 

Code:

throw (exception)

The Try Statement

 

 

Code:

try {

//statements

} catch (exception-class e) {

//statements

} finally {

//statements

} try {

//statements

} finally {

//statements

}

The Break Statement

First 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. - Bolded sentence is from this site.

 

The If Statement

The 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.

 

Code:

if(ryleeIsLeet) { //ryleeIsLeet must be declared as a boolean expression

System.out.println("Rylee is leet!");

}

When opening an if statement you use curly brackets (in my style of coding 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

 

 

Code:

private boolean ryleeIsLeet = true;

 

if(ryleeIsLeet) {

System.out.println("Rylee 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 ryleeIsLeet = 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 "Rylee is leet" regardless of whether the value returns true or false as long as you have System.out.println("Rylee 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 "Rylee is leet" is displayed, and if the value returns false, "Rylee isn't leet" is displayed.

 

 

Code:

if(ryleeIsLeet) {

System.out.println("Rylee is leet!");

} else {

System.out.println("Rylee isn't leet!");

}

 

The Throw Statement

The 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:

 

 

Code:

if(ryleeIsLeet) {

throw new LeetBarException();

}

To make the LeetBar object throwable, somewhere else in your code you need to have

 

Code:

throws LeetBarException();

for it to work properly.

 

If I feel up to it later I may add the try and switch statements.

 

 

 

Chapter 6: 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 7: 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 Operators

Arithmetic Operators are extremely simple as long as you are at least 9 years old... :| 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 Operators

Relational 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 Operators

A 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.

Lenke til kommentar

""Du burde komme deg ut i verden og oppleve u-land.". Ja, jeg skulle gjerne dratt til ett u-land jeg, men de fattige folkene der er så skittene, at jeg vet ikke helt om man har lov til å gå der de fattige er? Ikke vet jeg, men noe litt kult er at jeg hadde vært rikere enn kongen der. Hihih. Nice. "

:wallbash:

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...