Sunday, June 27, 2010

Java - 4 - Ever

I saw this video on Facebook one of my friend has added it. It is really funny and gives something to think about.

Saturday, June 26, 2010

Usage of Static in Java in SIMPLE

class A {
static int x = 100;
int y = 200;

void m1(){
//int Z; //this is wrong; a method local variable cannot be kept uninitialized.
int z = 300;
System.out.println(z);
}

static void m2(){
System.out.println("A");
}
public static void main(String[] args) {
A a = new A();
A b = null;//default value of object reference
System.out.println(a.x); //prints 100
System.out.println(a.y); //prints 200
a.m1();//prints 300
a.m2();//prints A
System.out.println(A.x);//prints 100 because int x is a static int
/*System.out.println(A.y);// compile error cannot referenced by the class name because its not static*/
/*A.m1();// compile error because it can not be referenced by the class name because its not static*/
A.m2(); //prints A
System.out.println(b.x);//prints 100
System.out.println(b.y);/*compies well but gives a run time error called nullObjectPointerException
* it also happens because y and m1() are not static. the reference of b is A*/
b.m1();//same as previous line
b.m2();//prints A

System.out.println(x);
// System.out.println(y);//compilation error
//m1();//compilation error
m2();//prints A
}



}


/*to understand this I recommend you to compile it and run then compare results with the code.:)*/

Creating Objects in Java

Here I'm going to show how to create objects in Java and how to use those objects with a simple Java code. The use of each line is described end of each line with comment(//).

class Family {

static String surname = "Mathiwes"; /* static variable so variable declarations(features) similar variable*/
double height; // instance variable

static void breathe() { // static method and methosds can be known as behaviors
System.out.println("Breathing");
}

void goToWork() { //instance method
double tempHeight = height + 0.1; // method local variable
}

public static void main(String[] args) {
Family father = new Family(); //Initialization of an Object
System.out.println(father.surname); //prints Silva
System.out.println(Family.surname); //prints Silva
System.out.println(father.height); //prints 0.0
father.height = 6.0; // variable initialization
System.out.println(father.height); //prints 6.0
father.breathe(); //method call and prints breathing
Family.breathe(); //method call and prints breathing
father.goToWork();//prints going to work
}

}

If you call a static variable or static method you can call it by the class name. But if you want to call a instance variable or method you have to create a an instance(means an object) of the class.

A simple Java code to understand Multy Dimensional Arrays

Here we create a three dimensional array and see how it stores values. Once you copy the code and compile it as a java code and run it you will have so many printed outputs on your command prompt. Then you can compare the printed lines with System.out.println(); lines of the code.


class TestArrays{

public static void main(String[] args){


int[][][] x = new int[3][][];
x[0] = new int[2][];
x[0][0] = new int[3];
x[0][0][0] = 10;
x[0][0][1] = 20;
x[0][0][2] = 30;
x[0][1] = new int[]{40, 50, 60, 70};
x[1] = new int[][]{{80}, {90, 100}};
x[2] = new int[1][];
int[] y = {110, 120, 130, 140, 150};
x[2][0] = y;
System.out.println(x[0].length);
System.out.println(x[1].length);
System.out.println(x[2].length);


System.out.println("");


System.out.println(x[0][0].length);
System.out.println(x[0][1].length);


System.out.println("");


System.out.println(x[0][0][0]);
System.out.println(x[0][0][1]);
System.out.println(x[0][0][2]);


System.out.println("");




System.out.println(x[0][1][0]);
System.out.println(x[0][1][1]);
System.out.println(x[0][1][2]);
System.out.println(x[0][1][3]);






System.out.println("");


System.out.println(x[1].length);
System.out.println(x[1][0].length);
System.out.println(x[1][1].length);


System.out.println("");


System.out.println(x[1][0][0]);
System.out.println(x[1][1][0]);
System.out.println(x[1][1][1]);


System.out.println("");


System.out.println(x[2].length);


System.out.println("");


System.out.println(x[2][0].length);


System.out.println("");


System.out.println(x[2][0][0]);
System.out.println(x[2][0][1]);
System.out.println(x[2][0][2]);
System.out.println(x[2][0][3]);
System.out.println(x[2][0][4]);


}
}

Saturday, June 19, 2010

ඇයි Java Slow ?

සාමාන්‍යයෙන් කට්ටිය කියන දෙයක් තමයි ජාවා ටිකක් slow කියන එක. මම හිරු පිලිගත් ජාවා පොරක් වෙන්න ජාවා ඉගෙන ගත්ත රජිත සර් දවසක් කිව්වා "Java is a bullock cart against C or C++". එයා කියන විදිහට දැනට තියෙන programing languages අතරින් C වේගවත්ම programing language එකක් විදිහට සලකන්න පුලුවන් කියන එක. ඉතින් ඇයි ජාවා slow ? ඇත්තටම ජාවා slow ද? මටත් ඔය වගේ ප්‍රශ්ණ ගොඩක් ආවා. ඉතින් ටිකක් හොයල බලපුහාම කතාව ඇත්ත. ජාවා, C එක්ක බලනකොට slow තමයි. ඇයි? ඒකට හේතු ගොඩක් තියෙනවා.
ජාවා වලින් ලියපු program එකක් පාවිච්චි කරපු කෙනෙක්ට හරි ජවා වලින් programing කරපු කෙනෙක්ට හරි ලේසියෙන් තේරුම් යන දෙයක් තමයි ජාවා slow කියන එක. හැබැයි ජාවා විතරක් දන්න කෙනෙක් කියයි වෙන දෙයක්.
හැබැයි ජාවා C හා සමානව speed එකක් ගන්න එක විශේෂිත අවස්තාවක් තියෙනවා. ඒ තමයි ඔක්කොම compilations එකම class එකක විතරක් තියෙන සහ පාවිච්චි කරල තියෙන ඔක්කොම data types ටික primitive data types වෙනවනම්. එ කියන්නෙ int වගේ data types.
ඒ කියන්නේ අපි ලියන ජාවා program එකේ objects පාවිච්ච් කරන්න ගත්ත ගමන් අපි ලියන program එක slow වෙනවා කියන එක. අපි බලමු ඒ ඇයි කියලා?
  • හැම object එකක් වෙනුවෙන්ම ඉඩ heap එකේ වෙන් කිරීම. ජාවා වල int වගේ primitive data types සහ object references වලට ඉඩ වෙන් කරන්නේ stack එකේ. හැම object එකක් සදහාම ඉඩ වෙන් කරන්නේ heap එකේ. C++ වගේ languages වල විශාල objects වලට heap එකේ ඉඩ වෙන් කලත් ජාවා වල ලොකු කුඩා හැම object එකක්ම හැදෙන්නේ heap එකේ.
  • casts ගොඩක් පාවිච්චි වීම.
  • ජාවා වල තියෙන garbage collector එකට memory use වීම.
  • ජාවා වල objects විශාල වීම.
මේ වගේ හේතු ගණනාවක්ම තියෙනවා ජාවා ටිකක් slow වෙන්න. Java අනිත් programing languages වලින් හොද වෙන්න හේතු වෙන post එකක ලියන්නම්.