Beginners Java Tutorial




"please if you want the qualitity of this tutorial to increase and want me to write more tutorials like this then click on the ad above."

Well now I have shown you how to write a basic program my next tutorial is about how to use arrays , functions and classes . in this tutorial I will only show the stuff which is used more commonly and not the advance features of these things . so let us start . first of all is

Array is described as collection of elements each described by one or more index . in more common words array is type of varible in which you can store more then one value . each value can be accessed by its indicator called index .

here i will only show you some few basic types . once you have learned this you can learn rest from any advance java tutorial or documentation widely available on the internet

Well this is the example program.

import java.io.*;
public class hello{
public static void main(String[] args){
int r [] = new int[4];
r[0] = 1;
r[1] = 2;
r[2] = 5;
r[3] = 4;

int x;
for(x = 0;x < 4 ;x++){
System.out.println(r[x]);

}



}
}

This is its output

Now how this program works I will show you in another figure

Remember that number here [number] while reading or writing into your array can be any expression .

an expression is just similar to mathematical expression and can be any thing from a single variable name or single number to more complex mathematical expressions . just the values produced by it must be in limits from 0 to the maximum number of places you have defined in your arrays definition .

function is the building block of any structured or object oriented programming language . a function is just like mathematical function . its like a machine you give it input it processes it and gives you output . and the similar machine can be used again and again. here is a figure describing a function.

In java every function is written in between a class never outside it .for simplicity write the function in different class as in your main class . here is an example program

import java.io.*;
public class hello{
public static void main(String[] args){
int a;
int b;
int c;
a = 3;
b = 7;
function t = new function();
c = t.sum(a,b);
System.out.println(c);


}

}



class function{
public int sum(int x,int y){
int z;
z = x + y;
return z;
}


}

its output is 10

so how does this program works . here is the description

remember that a variable defined outside a function can be accessed in a function . while a variable defined inside a function can not be accessed outside its function

Well i am not going to explain you all the features of a class just its basics. Well a class is a collection of different variables and functions bunched together .

Here is an example of a class.

import java.io.*;
public class hello{
public static void main(String[] args){
int a;
int b;
int c;
a = 3;
b = 7;
function t = new function();
c = t.sum(a,b);
System.out.println(c);
c = t.sub(a,b);
System.out.println(c);


}

}



class function{

function(){
System.out.println("Hello...");
}

private int z;

public int sum(int x,int y){
z = x + y;
return z;
}

public int sub(int x,int y){
z = x - y;
return z;
}
}

its output is

So how does this program works here is its description

 

Well what public and private is that public are those kinds of functions and variables which are accessed every where in our program and private are those function and variables which can only be accessed in that class only.



To Learn more about java Have a look at these book