Sunday 29 November 2009

Sorting an array of objects in javascript

How can we sort an array of objects based on some property of objects in javascript? For sorting a simple array, javascript provides a sort( ) method, but what to do when we want sorting on the basis of some property of an object.

Lets take an example. We have an employee array whose elements are objects.Every object has 2 properties: name and id. We want to sort the array by employee name.
Steps ares follows:

1. Create the array.

var emp = new Array( );

emp[0]= new Object("name","id");
emp[1]= new Object("name","id");
emp[2]= new Object("name","id");
emp[3]= new Object("name","id");


emp[0].name="Mn";
emp[1].name="Cb";
emp[2].name="Cd";
emp[3].name="Ab";

emp[0].id="1";
emp[1].id="2";
emp[2].id="3";
emp[3].id="4";



2. Write a function which and takes 2 employee objects as parameters compares the names of employees and returns 1,0 or -1 after comparison.

function compareNames(obj1,obj2)
{
if (obj1.name < obj2.name) { return -1; }
else if (obj1.name > obj2.name) { return 1; }
else return 0;
}


3. Call the javascript sort() function on emp array, with above function name as argument.

emp.sort(compareNames);

sort() function internally calls compareNames() and sorts the array by employee names in ascending order.
To sort in descending order, change the compareNames() as follows:


function compareNames(obj1,obj2)
{
if (obj1.name < obj2.name) { return 1; }
else if (obj1.name > obj2.name) { return -1; }
else return 0;
}

We do not need to bother how the sorting is done by sort(). To know how the objects are passed to compareNames(), put an alert() inside this function.

Saturday 28 November 2009

Java Exceptions: Using Exception's Sub Classes

Syntax of try-catch

try
{

//code which throws some exception
}

catch(XException xe)
{


}
where X is type of exception.

In catch, we have to pass type of exception which is thrown by the code in above try
block.If we know the exact type of excepton thrown, we should use it in catch, as:

catch(SQLException se)
{


}


But what if we are not sure about type of exception thrown? Here is the solution:



catch(Exception e)
{


}


Exception is superclass of all Exception classes. So a reference of Exception can catch all type of exceptions.
So whatever is the exception type, it will catch it and will not give any error.
To know the type of exception, we can print it:

catch(XException e)
{
System.out.println("Exception type: "+e);

}


Caution: If we are using multiple catch blocks for a single try, always put superclass before subclass, as:

try
{

// code here
}

catch(XException xe)
{


}

catch(Exception e)
{


}

where XException may be any exception as SQLException,ClassNotFoundException etc.

Putting Exception before XException will produce error.


try
{

// code here
}

/* this will give compilation error
catch(Exception e)
{


}

catch(XException xe)
{


}

*/

Thursday 19 November 2009

Java Basics: How to set PATH and CLASSPATH

When we write a java program and put the source file(.java file) in a location other than
bin of jdk, java compiler can not find the necessary class files of java api(as java.lang.*,
for example) to compile our file.So we need to set PATH and CLASSPATH environment variables
in our system. Here are the steps:

Adding Path:
1. Go to my computer
2.Right click, go to properties.
3. Go to Advanced tab.
4. Go to Environment Variables tab.
5. Search for PATH, then click on edit.
6. Add the path of your JDK bin folder, as C:\Program Files\Java\jdk1.5.0_15\bin
7. Click OK.

Adding Classpath:

1. Go to my computer
2.Right click, go to properties.
3. Go to Advanced tab.
4. Go to Environment Variables tab.
5. Search for CLASSPATH, then click on edit.
6. If it is not there, then add it by clicking NEW.
7. Add the path of your JDK lib folder, as C:\Program Files\Java\jdk1.5.0_15\lib
7. Click OK.

By these steps,we can execute javac command under any directory in our system.
After these steps, open a new cmd prompt window, and compile your program, as:
C:\Documents and Settings\chirag.jain\Desktop>javac Program.java

After compilation, run the program as:

C:\Documents and Settings\chirag.jain\Desktop>java Program

Getting the source code from java .class file

When we compile .java file using javac, we get .class file. But we can get the source code from class file, but not the full source code. javap, is the utility which gives details of variables and methods declared in the code.


C:\Documents and Settings\chirag.jain\Desktop>javac Test.java

C:\Documents and Settings\chirag.jain\Desktop>javap Test

Compiled from “Test.java”
public class Test extends java.lang.Object{
int number;
public Test();
public void sayHello();
}


To get the byte code details, use javap -c


C:\Documents and Settings\chirag.jain\Desktop>javap -c Test

Compiled from “Test.java”
public class Test extends java.lang.Object{
int number;
public Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.””:()V
4: aload_0
5: iconst_5
6: putfield #2; //Field number:I
9: return
public void sayHello();
Code:
0: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #4; //String Hello
5: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
8: return

}