15 July, 2011

Teachings Of Basic, Proper Java

So, where do we begin?

Variables

In Java, there are certain things that you can create called variables. Technically, a variable is an object that can hold a specific type of data. In other words, a variable is something that can hold a value.
Here are the types common of variables, what they stand for, and the type of value they can hold:

Code:

int - integer - whole numbers under 2.17 billion in size
long - long integer - whole numbers that are larger than 2.17 billion.
double - double - variable that can contain numbers with decimal values
boolean - boolean - holds only two values, true or false
String - String - holds more than one character, like a word or sentence.
int

This is a very common variable. It stands for integer, and can hold values of whole numbers less than 2.17 billion (this includes negatives), but cannot hold decimal values. If you try to make an integer bigger than 2.17 billion, your program will crash.

long

This is the exact same as an int, except it can hold larger values than 2.17 billion (on Windows machines). It cannot hold decimal values.

double

The double is the exact same as a long, except it can hold decimal values.

boolean

This is a helpful little variable, although it can only hold two values. The boolean cannot hold numbers, it can only hold two values: true and false.

String

The String is a special type of variable. It can hold multiple characters to form a series of words or a sentence.

So how do we use these variables?
In Java, any variable must be explicitly declared. Because of this, Java is sometimes considered a strongly typed language. Don't worry about that though. Just know that before you want to use a variable, you must tell Java that it exists. To do this, we declare our variable.

Declaring a variable looks like this:

Code:

accesslevel type name;
Access Level:
The access level is how your variable can be accessed. By default, other packages cannot see or use a variable unless you set the access level properly. A package is a folder that contains related Java classes. A Java "class" is a Java file. Most private servers don't use packaging, so you don't need to be too worried about the access level.
Common access levels are as so:
Code:
public
private
There are more, but you don't need to worry.

public

The access level public means that other packages can see it, hence the term public.

private

The direct opposite of public, other packages cannot see a variable with access type of private

You do not have to specify an access level for your variables. By default, variables are private and cannot be accessed by other packages unless you declare them as public.

Type:

The type in our variable declaration is the type of variable we are declaring: int, long, boolean, double, etc.

Name:

Name is simply the name you give to the variable you are declaring. By Java conventions, your variables should start with a lowercase letter, and any other words in your name must start with an upper case letter. The name cannot contain spaces.

An example of a variable declaration:

Code:

public int myInteger;
Statements

A statement in Java is basically anything you are coding. Anything from declaring a variable to doing advanced math is either a statement or a series of statements. The variable declaration above is a statement. Statements must end with a semicolon unless you are using a code block. A code block is simply, a block of code, starting with { and ending with }. Therefore, for every {, there must be a } to end the statement.

Conditional Statements

A conditional statement is a statement that will execute only when a certain condition is met. These statements are also called "if" statements. This is the layout for an if statement:

Code:

if(arguement)
    code to execute;
If you are using only a semi colon in any statement, you cannot perform more than one action in the statement. Here is an example explaining this concept:
Code:
int myInteger;
myInteger = 1;
if(myInteger == 1)
    System.out.println("Yay!");
System.out.println("myInteger is equal to one!");
How many statements are in the above code? There are actually four statements in the above code.
Statement one: declaring myInteger.
Statement two: setting myInteger to 1.
Statement three: this is our conditional statement, it therefore includes the "System.out.println("Yay!")", but only executes it if myInteger is 1.
Statement 4: printing "myInteger is equal to one!" into our console.

System.out.println(String) prints text to the console.

What do you think the output is of this program? Here is what it will say:

Code:

Yay!
myInteger is equal to one!
What happens if we change the code to this?

Code:

int myInteger;
myInteger = 2;
if(myInteger == 1)
    System.out.println("Yay!");
System.out.println("myInteger is equal to one!");
Now what is the output?

Code:

myInteger is equal to one!
Surprised? I expect you to be. By changing myInteger to 2, the conditional statement should not execute and it should not say anything at all. Wrong, because as I stated above, you can only execute one action if you are using just a semi colon. To change this, we will use what is called a code block. A code block allows you to execute more than one action at once within one statement.

Here is an example of the above code, with a code block instead:

Code:

int myInteger;
myInteger = 2;
if(myInteger == 1)
{
    System.out.println("Yay!");
    System.out.println("myInteger is equal to one!");
}
Now what is the output of the program?

Nothing.

We have successfully bunched two actions into our code block, and because our myInteger variable is 2, the if statement never executes because it only executes if myInteger is 1.

You might have noticed the "=" and "==" characters in the above statements. These are called operators, which brings me to the next section...

Operators

An operator is something that performs a calculation. There are many operators, all meant for specific calculations. Here is a list of the common ones, and their purpose.
Code:

+ ~ Addition
- ~ Subtraction
* ~ Multiplication
/ ~ Division
> ~ Greater Than
< ~ Less Than
>= ~ Greater than or equal to
<= ~ Less than or equal to
== ~ Equal to
!= ~ Not equal to
++ ~ Increment
-- ~ De-increment
&& ~ AND
|| ~ OR
Thats a lot of operators eh? Trust me, there are more operators, designed for performing calculations on the level of bits and bytes. For the sake of your sanity, we'll just stay with the operators I've listed above.

I wont go into detail about these operators for the sake of time, you can figure out what they do by looking at the list above.
Now that I've gone over basic conditional statements and variables, lets go onto methods.

Methods

Simply put, a method is a chunk of code that performs a calculation, and returns a value based on the calculation it makes. Its not as complicated as it seems. A method is just like a variable, it is declared in the same way. The key difference is that a method must have a code block, while a statement doesn't have to. A method is a holder for statements. Here are a few types of methods:

Code:

void
int
long
String
boolean
double
The list goes on, however, these are the most common types of methods. You may have noticed that they are very similar to variables, well in a sense they are. I mentioned before that a method returns a value. Every method returns a value except for a void. Lets go over what they return.

void
No return type.

int

Returns an integer.

long

Returns a large integer.

String

Returns a String value.

boolean

Returns a boolean value

double

Returns an integer value with decimal points supported, or a double.


You may have noticed that the methods return the value that their brother variables can hold. If you made this connection, good job. If not, that's okay.

Declaring a method is just like declaring a variable, but you add a code block.

Here's the way you declare methods:

Code:

accesslevel type name(parameters)
{
    Code inside the method
    return value
}
Parameters are slightly more advanced. Basically, a parameter is a way of giving our method variables so it can perform calculations. You can have multiple parameters in your method.

To make a method execute, we have to call the method by its name and its parameters. Here is the layout of calling a method:

Code:

name(parameters);
Separate parameters are separated by a comma. Here is an example of how to use parameters:
Code:

public static void main(String[] args)
{
    System.out.println("The value of 5.5 + 5.5 is equal to" + doAddition(5.5, 5.5));
}
double doAddition(double one, double two)
{
    double solution = one + two;
    return solution;
}
We will go over the above code line by line

Line 1: our main method which Java calls when it starts the file.
Line 2: opening brace for our main method code block
Line 3: printing to the console window by calling doAddition(int, int) method
Line 4: closing brace for our main method code block
Line 5: declaration of our doAddidion method which is the double type, so we can do addition with not only whole numbers but also decimals
Line 6: opening brace for our doAddition method code block
Line 7: creating a new integer called solution, then setting solution to one + two.
Line 8: returning solution, the value that was calculated based on the parameters given
Line 9: closing brace for our doAddition method code block


The output of the above program looks like this:
Code:

The value of 5.5 + 5.5 is equal to 11
What might confuse you is the parameters. You declare variables in parameters, and use them only within the code block of the method that has the new variables. We give the parameter variables their value when we call the method.

Why do this when we can just do variable = number + number? Well sometimes we want to do more advanced calculations than just addition, such as the quadratic formula or distance formula. Methods are advanced, custom calculations.

About the void. A void is a method with no return type, therefore it does not return anything. The void simply executes the statements within it and finishes whenever it is called.

Classes

A class, technically, is an object in object-oriented programming. Simply put, a class is usually a Java file that holds methods. This is a slightly more advanced concept than we have previously talked about.
A class is declared just like a method, except you must put 'class' as the type. All of your code must be contained within the code block of a class.

Here is what a normal Java program looks like:
Code:

public class HelloWorld
{
    public static void(String[] args)
    {
  System.out.println("Hello World!");
    }
}
The above code will compile and run in the Java environment.
When running once compiled, it will say
Code:
Hello World!
Cute.
Lets move on.

Classes can be (here comes a big word) instantiated, or created an instance of. Creating an instance of a class from another class tells that class to run relative to the class that is creating an instance of it. Its complicated, you may have to read that sentence a few times to understand. Basically, you can call a class like you can call a method. But instead of calling the class, you must create an instance of the class. Once you create an instance of the class, you can use almost any method or variable that is inside the class.

Here is how you instance a class:
Code:

classname variablename = new classname(parameters);
Yes, classes can also have parameters. This is done by what is called the classes constructor. It is an advanced concept that you may not understand for a while until you get used to programming Java.
By default, any class you create has no parameters, so you do not include any parameters in the instance of the class.

This is how to create an instance of a class using parameters.The class we are going to instantiate
Code:
public class SayHello
{
    String text;

    public SayHello(String text)
    {
  this.text= text;
    }

    public static void main(String[] args)
    {
  System.out.prinltn(text);
    }
}
Line 1: declaring our class SayHello
Line 2: opening brace for our SayHello class
Line 3: declaring a new string, naming it 'text'
Line 4: white space
Line 5: constructor. Notice the parameters, we are creating a new variable inside the constructor.
Line 6: opening brace of the code block for our constructor
Line 7: setting the String text in the outside of our constructor to the parameter that we received when the class was instantiated.
Line 8: closing our constructor code block
Line 9: white space
Line 10: main method called by Java when the program starts
Line 11: opening brace for our main method code block
Line 12: printing to the console the value of the 'text' String
Line 13: closing our main method code block
Line 14: closing our SayHello class code block.

Instancing the class
Code:

SayHello mySayHelloClassVariable = new SayHello("Hello, World!");
Now, when we run the above statement that instances the SayHello class, the SayHello class will print the 
following text:
Code:

Hello, World!
Congratulations, if you have made it this far, you now understand the basics of Java programming.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home