<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2860645638104875527</atom:id><lastBuildDate>Fri, 30 Aug 2024 05:19:20 +0000</lastBuildDate><category>Java</category><category>javascript</category><title>Chirag&#39;s Computer Blog</title><description>A dose of my technical experience...</description><link>http://techwithcj.blogspot.com/</link><managingEditor>noreply@blogger.com (chirag jain)</managingEditor><generator>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2860645638104875527.post-457057487599094612</guid><pubDate>Sun, 29 Nov 2009 13:37:00 +0000</pubDate><atom:updated>2009-12-05T19:32:49.541+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><title>Sorting an array of objects in javascript</title><description>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.&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;Steps ares follows:&lt;br /&gt;&lt;br /&gt;1. Create the array.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;var emp = new Array( );&lt;br /&gt;&lt;br /&gt;emp[0]= new Object(&quot;name&quot;,&quot;id&quot;);&lt;br /&gt;emp[1]= new Object(&quot;name&quot;,&quot;id&quot;);&lt;br /&gt;emp[2]= new Object(&quot;name&quot;,&quot;id&quot;);&lt;br /&gt;emp[3]= new Object(&quot;name&quot;,&quot;id&quot;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;emp[0].name=&quot;Mn&quot;;&lt;br /&gt;emp[1].name=&quot;Cb&quot;;&lt;br /&gt;emp[2].name=&quot;Cd&quot;;&lt;br /&gt;emp[3].name=&quot;Ab&quot;;&lt;br /&gt;&lt;br /&gt;emp[0].id=&quot;1&quot;;&lt;br /&gt;emp[1].id=&quot;2&quot;;&lt;br /&gt;emp[2].id=&quot;3&quot;;&lt;br /&gt;emp[3].id=&quot;4&quot;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;&lt;br /&gt;function compareNames(obj1,obj2)&lt;br /&gt;{&lt;br /&gt;if (obj1.name &lt; obj2.name) { return -1; }&lt;br /&gt;else if (obj1.name &gt; obj2.name) { return 1; }&lt;br /&gt;else return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;3. Call the javascript sort() function on emp array, with above function name as argument.&lt;br /&gt;&lt;br /&gt;emp.sort(compareNames);&lt;br /&gt;&lt;br /&gt;sort() function internally calls compareNames() and sorts the array by employee names in ascending order.&lt;br /&gt;To sort in descending order, change the compareNames() as follows:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;&lt;br /&gt;function compareNames(obj1,obj2)&lt;br /&gt;{&lt;br /&gt;if (obj1.name &lt; obj2.name) { return 1; }&lt;br /&gt;else if (obj1.name &gt; obj2.name) { return -1; }&lt;br /&gt;else return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;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.</description><link>http://techwithcj.blogspot.com/2009/11/sorting-array-of-objects-in-javascript.html</link><author>noreply@blogger.com (chirag jain)</author></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2860645638104875527.post-8587574823516928599</guid><pubDate>Sat, 28 Nov 2009 04:43:00 +0000</pubDate><atom:updated>2009-11-28T10:20:41.723+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Java Exceptions: Using Exception&#39;s Sub Classes</title><description>&lt;span style=&quot;font-weight:bold;&quot;&gt;Syntax of try-catch&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;try&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;//code which throws some exception&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch(XException xe)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt; where X is type of exception.&lt;br /&gt;&lt;br /&gt;In catch, we have to pass type of exception which is thrown by the code in above try &lt;br /&gt;block.If we know the exact type of excepton thrown, we should use it in catch, as:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;catch(SQLException se)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;But what if we are not sure about type of exception thrown? Here is the solution:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;catch(Exception e)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Exception is superclass of all Exception classes. So a reference of Exception can catch all type of exceptions.&lt;br /&gt;So whatever is the exception type, it will catch it and will not give any error.&lt;br /&gt;To know the type of exception, we can print it:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;catch(XException e)&lt;br /&gt;{&lt;br /&gt;System.out.println(&quot;Exception type: &quot;+e);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Caution: If we are using multiple catch blocks for a single try, always &lt;span style=&quot;font-weight:bold;&quot;&gt;put superclass before subclass&lt;/span&gt;, as:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;try&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;// code here&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch(XException xe)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch(Exception e)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;where XException may be any exception as SQLException,ClassNotFoundException etc. &lt;br /&gt;&lt;br /&gt;Putting Exception before XException will produce error.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;// code here&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* this will give compilation error&lt;br /&gt;catch(Exception e)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch(XException xe)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;</description><link>http://techwithcj.blogspot.com/2009/11/java-exceptions-using-exceptions-sub.html</link><author>noreply@blogger.com (chirag jain)</author></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2860645638104875527.post-2491391934763472286</guid><pubDate>Thu, 19 Nov 2009 16:42:00 +0000</pubDate><atom:updated>2009-11-19T22:14:59.943+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Java Basics: How to set PATH and CLASSPATH</title><description>When we write a java program and put the source file(.java file) in a location other than&lt;br /&gt;bin of jdk, java compiler can not find the necessary class files of java api(as java.lang.*,&lt;br /&gt;for example) to compile our file.So we need to set PATH and CLASSPATH environment variables&lt;br /&gt;in our system. Here are the steps:&lt;br /&gt;&lt;br /&gt;Adding Path:&lt;br /&gt;1. Go to my computer&lt;br /&gt;2.Right click, go to properties.&lt;br /&gt;3. Go to Advanced tab.&lt;br /&gt;4. Go to Environment Variables tab.&lt;br /&gt;5. Search for PATH, then click on edit.&lt;br /&gt;6. Add the path of your JDK bin folder, as C:\Program Files\Java\jdk1.5.0_15\bin&lt;br /&gt;7. Click OK.&lt;br /&gt;&lt;br /&gt;Adding Classpath:&lt;br /&gt;&lt;br /&gt;1. Go to my computer&lt;br /&gt;2.Right click, go to properties.&lt;br /&gt;3. Go to Advanced tab.&lt;br /&gt;4. Go to Environment Variables tab.&lt;br /&gt;5. Search for CLASSPATH, then click on edit.&lt;br /&gt;6. If it is not there, then add it by clicking NEW.&lt;br /&gt;7. Add the path of your JDK lib folder, as C:\Program Files\Java\jdk1.5.0_15\lib&lt;br /&gt;7. Click OK.&lt;br /&gt;&lt;br /&gt;By these steps,we can execute javac command under any directory in our system.&lt;br /&gt;After these steps, open a new cmd prompt window, and compile your program, as:&lt;br /&gt;C:\Documents and Settings\chirag.jain\Desktop&gt;javac Program.java&lt;br /&gt;&lt;br /&gt;After compilation, run the program as:&lt;br /&gt;&lt;br /&gt;C:\Documents and Settings\chirag.jain\Desktop&gt;java Program</description><link>http://techwithcj.blogspot.com/2009/11/java-basics-how-to-set-path-and.html</link><author>noreply@blogger.com (chirag jain)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2860645638104875527.post-6247491629713958547</guid><pubDate>Thu, 19 Nov 2009 16:27:00 +0000</pubDate><atom:updated>2009-11-19T22:19:51.958+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Getting the source code from java .class file</title><description>When we compile  .java file using &lt;code&gt; javac&lt;/code&gt;, we get .class file. But we can get the source code from class file, but not the full source code. &lt;code&gt; javap&lt;/code&gt;, is the utility which gives details of variables and methods declared in the code.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;C:\Documents and Settings\chirag.jain\Desktop&gt;javac Test.java&lt;br /&gt;&lt;br /&gt;C:\Documents and Settings\chirag.jain\Desktop&gt;javap Test&lt;br /&gt;&lt;br /&gt;Compiled from “Test.java”&lt;br /&gt;public class Test extends java.lang.Object{&lt;br /&gt;int number;&lt;br /&gt;public Test();&lt;br /&gt;public void sayHello();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To get the byte code details, use  &lt;code&gt; javap -c &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;C:\Documents and Settings\chirag.jain\Desktop&gt;javap -c Test&lt;br /&gt;&lt;br /&gt;Compiled from “Test.java”&lt;br /&gt;public class Test extends java.lang.Object{&lt;br /&gt;int number;&lt;br /&gt;public Test();&lt;br /&gt;Code:&lt;br /&gt;0:   aload_0&lt;br /&gt;1:   invokespecial   #1; //Method java/lang/Object.”&lt;init&gt;”:()V&lt;br /&gt;4:   aload_0&lt;br /&gt;5:   iconst_5&lt;br /&gt;6:   putfield        #2; //Field number:I&lt;br /&gt;9:   return&lt;br /&gt;public void sayHello();&lt;br /&gt;Code:&lt;br /&gt;0:   getstatic       #3; //Field java/lang/System.out:Ljava/io/PrintStream;&lt;br /&gt;3:   ldc     #4; //String Hello&lt;br /&gt;5:   invokevirtual   #5; //Method java/io/PrintStream.println:(Ljava/lang/Str&lt;br /&gt;ing;)V&lt;br /&gt;8:   return&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description><link>http://techwithcj.blogspot.com/2009/11/getting-source-code-from-java-class.html</link><author>noreply@blogger.com (chirag jain)</author><thr:total>0</thr:total></item></channel></rss>