SRL Archive - Java Help and Tutorials

JAVA BEGGINERS-INTERMEDIATE TUTORIAL *traitors*


traitors

2009-10-04 07:17:10

relised for me to cover everything i have learnt in the detail that i have been is going to end up being a few pages and i dont have the time as i have only briefly covered 3 lecture slides out 25 and readings. So in saying that wont be adding any more wil leave what i have wrote

This tutorial assumes you have a java IDE set-up and know how to compile a java file. If you don’t Google it there are plenty of guides online. This tutorial is written mainly for revision as I have an end of semester test coming up soon. Use, don’t use don’t care.

This tutorial doesn’t really follow the traditional way of doing things I am going to cover fundamentals you should be very familiar with in order to fully understand the code and how it’s works as opposed to traditional tutorials which jump straight into code.

So let’s begin,

What’s java and what is a java program?

A program is a written sequence of instructions or code that a computer can execute. An example of this could be Microsoft Word, internet explorer, lime wire etc

Java is a type of computing language. A java program consists of a few things
• A java program consists of 1 or more class definitions
• These classes(class definitions) may contain data fields and methods
• One of these classes must have a method called main

The java program runs by executing each statement of code in the class containing the main method as above. The java language is accompanied by the standard class libraries which includes many useful classes that we can use in our own programs.

Okay so that’s a bit of a mouth full but don’t worry I don’t expect you to know exactly what I’m talking about right now the above will become more clear as you progress through this tutorial. For now just know that a java program is made up of a class or classes and these classes contain Data Fields and Methods and that one of these classes has a main method where the code is executed in order for the program to do something.

If you have read a few java tutorials already you would have probably heard that java is an object oriented programming language a few times and if you were like me you have no idea what that means at the start. If you haven’t already guessed it’s a pretty important concept to learn pretty much what the whole language is based around so I’m going to try and explain the concept as beginner friendly as possible it may or may not make sense right now but as you go further along you will start to get the concept of object oriented programming.

So when we say we want to create an object in java we are saying we want to create something that does something. One of the things that makes java so great is that we can have these objects (create something that does something) to represent real life things. For instance say we want to write a program that deals with bank accounts. Our object is the bank account. The java language follows certain principles when creating an object, pretty much a set of rules you should follow. An object can have a state (fundamental characteristics that currently define the object) and a behavior (activities associated with the object, pretty much what it does and can do), in this case the state could be the current account balance where the behavior could be the ability to make withdraws and deposits.

To go even further into detail, so we have an object which is defined by its state and behavior. The current state of the object is defined by the objects attributes, attributes are values the object stores internally. Remember above how we said a class has Data Fields and Methods, the values an object holds are generally stored in those data fields. So once again objects current state is defined by its attributes which are generally internally stored values usually kept in the data fields of a class. See how it starts coming together: D Now an objects behavior or potential behavior can be defined by its methods (code which does something) which are also part of a class as said above.

So we have an

Object>Defined by>State/Behavior>State defined by Attributes>Behavior defined by methods

Ok so take a second to let that sink in.

Bit to get ya head round but don’t worry that pretty much the hard part. Once you grasp the object concept the rest is pretty straight forward, sort of. What I’ll write about now is how java code is actually written. Writing code is not that hard of a thing to do it’s like learning to read and write. Java code has rules just like English for example if you write an essay you have to have an introduction, paragraphs and a conclusion. As well as using punctuation like Capital letters, spelling, full stops etc. Java follows the same sort of principles expect different names e.g. variables, methods, arrays etc all the things I will cover in this tutorial.

Naming things in java

When we write code a lot of the times we have to write names for variables methods etc. Java has a few naming conventions you should follow

Avoid using words already used by java(you will find these out later on)
Names can't begin with a number or contain white spaces
Class names generally begin with an uppercase letter, while variables or methods begin with a lower case letters
names should generally describe what they are doing

As said above in our code we will alot of the time be dealing with data, Their are different types of data

Primitive data
Whole numbers- represented by long,int,short,byte(the difference between each is how many numbers they can hold)
Decimal Numbers- double, float
Characters - char
Truth values - boolean

these will make more sense as you go along.Also note that java has a range of primitive data types as well as reference types, we will get more into that later.

Variables

Now we have these data types but what do we need to do with them? In java we can store these data types into things called variables which will hold the value we assign to the variable for us. For instance in our bank account object we could have a defualt opening balance of $10 when we join with that bank. So we want to store and keep that value. So $10 is considered a whole number data type so we can store it in long,int,short or byte. Seing as $10 is not a very big number we would just store it as an int like below

int defaultAccountBalance = 10;

when declaring a variable and setting a predefined value ($10) in java is known as static typing. Dont worry about the code to much now just grasp the concept.

Variables can only hold one type of data for instance you cannot declare a variable

long int defaultAccountBalance = 10;

you cannot have long and int only one. Their are 2 basics stages you need to remember when dealing with variables.

Declaration- stating what data type your variable is eg int,long,byte

int defaultAccountBalance;

Initilisation- If you wish you can predefine the value of the variable,you do not have to

defaultAccountBalance = 10;

so if we didn't know what the defualt account balance was for a new account in our account object we could just declare the variable type

int defaultAccountBalance;

would still work but its value would not be 10.we do not have to initilise(predefine) it.

Once a variable is declared in java we do not have to declare it every time we use it.So if we wanted to make another account in our account object and we have already declared the defaultAccountBalance variable we do not have to do it again.

Constants

Sometimes in our code a value(variable) is fixed in advance for example in our account object we could have a fixed interest rate of 5% that we do not want to change. To do this we make a constant. a constant is pretty much a variable with final written in fron of it

final int INTERESTRATE = 5;

also as a java naming convetion we usually name contants in all uppercase letters.variables are used in computing expressions(code that does something) or can a value can be assigned to a variable after a computing expression is done.

for numerical data we can use all the normal operators eg +,-,*,/ etc
One thing to watch out for is that when dividing integers eg 7/2 should give you an answer of 3.5 but as an int data type only stores whole numbers you will get an answer of 3 as java automatically truncates decimal places if it is a whole number data type. To get around this we do something called casting. Pretty much we change the data type by thrwoing the data type we want infront of it eg

int x = 7;
int y = 2;

(double)x / y;

as double is a number data type that also deals with decimal places.

Strings

Strings is another data type but it is not considered a primitive data type. A String data type is just a bunch of chararcters eg

String accountHoldersName = "Stewie";

when we declare a string data type we enclose it with quotation marks "" as we do with the char data type. Once again the variable above has been declared and initilised we can also just declare it without initilising it and assign it a value later on eg

String accountHoldersName;

Strings can be joined together, concatenated eg

String accountHoldersName;
String nameOfBank;
accountHoldersName+nameOfBank;

the code above would give us the account holders name as well as which bank they are with.

Methods

this is where it starts to get good you will be using a lot of methods(code which does something) in your code. Ok now you rembember above i said that a java program can be made up of one main or many classes and that their is a libary of useful classes made by sun microsystems. Well methods can be called from these sun microsystem classes you can even write your own methods if you wish. I am not going to go into to much detail with methods as their is a lot to cover and i dont want to confuse you. Methods like variables have a specific way they need to be written. Ok so we have our bank object and one of its behavoirs is to be able to make a withdrawl. behaviours are defined by methods so if we were to write a method for withdrawls we would follow the below layout

public static void withdrawlMethod(){
// code here that makes withdrawls happen
}

dont worry to much about public,static and all those other words i will explain them later on. For now just understand to define the behaviour of our object we can use methods which may in some cases follow the layout as above.

Method

2009-10-04 07:32:14

What will this offer that the official Java tutorial and other professional ones don't? Why reinvent the wheel?

n3ss3s

2009-10-04 12:28:54

When writing tutorials you should try to write it so that it stays interesting, e.g use some bolding and separate separate things :)

weequ

2009-10-04 12:55:30

Goog tutorial. I hope you will write more ;)

traitors

2009-10-05 02:51:32

guys its just for revision im at uni and thought id study and contribute to srl at the same time if u dont wanna read dont. If someone wants to make it more visually appealing is their any way i can let you have access to this thread to edit it?

fORCE_wORKS

2009-10-17 01:27:07

thx ive never programed java i have some interest in learning and it gave me some understanding of what java is. i think this is a better way of approaching a beginner tutorial rather jumping into code right away just as traitors said.

weequ

2009-10-22 07:47:34

From: traitors
Ok now you rembember above i said that a java program can be made up of one main or many classes and that their is a libary of useful classes made by sun microsystems.

Where I can see those methods by sun microsystems?