Saturday, December 17, 2011

I was checking my facebook wall today. I was shocked to see that nearly 80% of my status updates were related to technology. This gave me an idea to start a new page "TechieHub" in facebook which would allow techies like you and me to post latest updates to this one place for all to read.

I would like to suggest you to "LIKE" this new "TechieHub" page if you really like my idea. Here is a link. Click Here

Wednesday, August 11, 2010

JavaFX - Special data types and data holders!

Apart from the usual datatypes found in almost every programming language for eg. Integer, Long, Double, Boolean etc; JavaFX provides a special data type named Duration. As soon as you see Duration data type, you get a strong feeling that it has something to do with Animation. Below is an example:

def mil = 25ms;
def sec = 25s;
def min = 25m;
def hrs = 25h;

Also notice the time literals used in this example. Appending ms, s, m or h  to a value creates an object of type Duration. Not only this, you can also apply arithmatic operations on Duration data types. Cool ... isn't it!

The second feature that is extremely useful is a special collection of objects named Sequences. Peek into the example below:

def seq1:String[] = ["A", "B", "C"];
def seq2 = [1 .. 100];

Don't they look like arrays. Yes they do, but look at the second line, this is special feature in a Sequence. Here seq2 automatically gets assigned the values from 1 to 100 i.e the size of sequence becomes 100. You can also use the following:

def seq3 = [0 .. 100 step 10];

This would assign values 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 to seq3. Not only this, you can do Sequence manipulation as well i.e. you can always perform insert, delete and reverse operations on a sequence. But you must understand that although you can perform such operations but still a sequence is immutable and any such operation would result in creation of a new sequence. Look a the eg. below:

var seq1= [1 .. 5];
insert 6 into seq1;
insert 0 before seq1[0];

This would result in a sequence with values 0, 1, 2, 3, 4, 5, 6. Similary you can delete from a sequence as well. Isn't it much more powerful than an Array!

Tuesday, August 10, 2010

JavaFX - New ways to declare a variable!

While making my way through learning this new and exciting technology, interestingly, i found that in JavaFX there are two ways to declare a variable. Take a look at the code snippet below:
var canAssign: Integer = 10;
def cannotAssign: Integer = 10;
canAssign = 20; // will work fine
cannotAssign = 20; // will result in compiler error
To sum up, you can declare a variable using a keyword var and second option is to use a keyword def. In case you use var keyword to declare a variable, you are creating a reassignable variable. In case you opt in for def keyword, you are creating an initialize-only variable.

However, it must be noted here that although a def variable cannot be reassigned but the object it references can definately mutate i.e. change its contents. Therefore a def variable may be assumed as an immutable type.

Isn't it interesting!

Sunday, August 8, 2010

NetBeans JavaFX Composer - A threat to Adobe FlexBuilder!

Before coming up with tutorial on working with NetBeans 6.9 JavaFX composer and just to make you more curious, I must tell you guys that JavaFX Composer is a great competitor and might be a threat to Adobe FlexBuilder.

Last night, I was playing with this new tool in the block and I must appreciate the efforts of NetBeans team. They are bang on target with this excellent tool. It makes coding in JavaFX a breeze. Most of times you need not code anything, about 90% of the tasks are done by mere drag and drop.

So Adobe and Microsoft get ready to face a stiff competition from Oracle with this new JavaFX composer. I must say... I am impressed! Just remain tuned in for more action.

Friday, August 6, 2010

JavaFX - Rich UI Applications!

Last few days, I had been trying to understand the need of JavaFX in the world of Java. With RIAs becoming so common and Java really loosing out in client side application development at the hands of Microsoft Silverlight and Adobe Flex, something was urgently required to prove the existence of Java on client side as well. And JavaFX is an answer to this.

If you remember, it was Java which first introduced the concept of RIAs in form of an Applet, but with time it became old and mature. And then Flash and AJAX begin dominating Java Applet. It would not be wrong to say that Java was completely wiped off from the RIA arena. But now with JavaFX it has really made a comeback.

Oracle is not only doing a great job in creating JavaFX but it is also trying to create solid tools that would enable the developers to quickly create a JavaFX application. If you don't believe me, check out the latest version of NetBeans 6.9. NetBeans 6.9 has an excellent JavaFX composer tool to start creating JavaFX applications by mere drag and drop stuff and its really really powerful.

I will be soon coming up with a tutorial on how to startup with JavaFX using NetBeans 6.9 JavaFX composer. Until then... Good Bye!

Thursday, April 29, 2010

Here comes one more Java puzzle for you...

I am up with my new puzzle today. Just go through the code below and see if you can guess the output. The code looks pretty simple. But beware, its not that easy:

public class StringPuzzle { 
 public static void main(String[] args) { 
   final String a = "length is 12"; 
   final String b = "length is " + a.length(); 
   System.out.println("Strings are equal: " + a == b); 
 } 
}

Well... can you guess the answer? Ok... let me read your mind... you guessed the output  Strings are equal: true or you might have guessed Strings are equal: false.

Well I have some bad news for you. In both the cases you are incorrect. Isn't it strange! Don't believe my words. Just execute this program and you will see it yourself. 

Post a solution in comment section. Happy coding!!

Sunday, February 21, 2010

Performance driven iteration of an ArrayList - Iterator or Traditional for loop?

Well.. for last few days I have come to know a lot about writing performance driven java code. Thanks to java profiling. Today, I am going to share my small experience with ArrayList iteration with you.

One of the first lessons while going through lectures on Collections is about fetching or iterating through data stored in these Collections. One of the most commonly used Collection is ArrayList. ArrayList is an ordered list that allows index based fetching.

Let me ask you a question. If you have to write a performance driven code, where it is required to iterate through huge dataset contained in an ArrayList, what mechanism would you follow? About 90% of the times the answer would be using an Iterator. Correct?

Well in my opinion, NO. Just try to run the below piece of code and note down the time taken in both type of iterations i.e. Iterator and traditional for loop and you would start to agree with me.

public static void main(String args[])
{
ArrayList arrayList = new ArrayList();
//populating list with elements
for(int i=0; i<1000000; i++)
{
arrayList.add(i);
}

// using traditional for loop
long startTime = System.currentTimeMillis();
for(int i=0; i<1000000; i++)
{
arrayList.get(i);
}
System.out.println("Time taken in traditional for loop:"+(System.currentTimeMillis()-startTime));

// using traditional for loop with iterator
startTime = System.currentTimeMillis();
for(Iterator i = arrayList.iterator(); i.hasNext();)
{
i.next();
}
System.out.println("Time taken in Iterator loop:"+(System.currentTimeMillis()-startTime));
}

Surprising?

Well that was related to iteration of ArrayList elements only. What if, you want to remove elements from the ArrayList while iterating through. In such as case, its always better to use Iterator since it does not enforce you to know the size of underlying Collection and is also fail safe.

So now you should be comfortable enough with both of these mechanisms and when to actually use them.

Happy Coding!

Wednesday, February 17, 2010

Is it a Bug or simply a Java Puzzle?

Ok... I just wrote a simple program where I was trying to divide a microseconds in day with milliseconds in a day and I wrote it as follows:

public class Test {
public static void main(String[] args) {
long microseconds = 24 * 60 * 60 * 1000 * 1000;
long milliseconds = 24 * 60 * 60 * 1000; System.out.println(microseconds/milliseconds);
}
}

Well... can you figure out any problem with this code? If yes, you have got a great presence of mind and If no, its not an issue. Let me explain the problem with this code. You are just thinking that the code should run fine and should print 1000 as a result. My advice, try to copy-compile-run this piece of code, see the result and then continue reading the next section of my post.

Did you just saw it printed 5. Is it hard to believe? Well, the good news, is its not a Java Bug and the bad news is, the code you just executed has a flaw. Although the results of the computation of the variables microseconds and milliseconds are getting stored in long data type but they are being computed as an int arithmetic which makes the microseconds variable to overflow (int range) before storing the result in long data type.

So why the hell these computations are being carried on as an int arithmetic. The reason is pretty simple. All of the values involved in computation i.e. 24, 60, 60, 1000, 1000 are int themselves. So, when you multiply int to an int you get another int.

Whats the solution then? Do you have an idea? Post a solution in comment section. Happy coding!!

Thursday, January 21, 2010

Profiling Java Applications

Well, last few days have given me some serious reasons for profiling Java applications. To be precise, the problem was with one of the Java application which was becoming slower overtime. I tried to figure out the real problem both by manually scanning the code and debugging as well but the problem was hidden and all my efforts were getting wasted. Then, someone came and advised me on using a profiler to find the actual problem in the code.

Really, till then I had just heard about profiling but had never tried myself. This was the first time I gave it a try and it really worked. I used NetBeans profiler in order to find the problem with the code and I must say, it worked wonders for me.

Profiler enables a Java developer to make the application JVM friendly. Java profilers connect to a running Java application's JVM and capture memory information. In older Java versions they used Java Virtual Machine Profiler Interface (JVMPI). In latest versions however, the JVM Tool Interface (JVMTI) for Java is used to get the profiling information. If you don't profile your large enterprise Java applications prior to releasing them to production, they can fail with OutOfMemoryErrors or render poor performance over time.

JVMPI


JVMPI was an experimental feature in the Java 2 SDK. Sun intended tools vendors to use it to develop profilers that would work in conjunction with Sun's JVM. Similar to the Java AWT listener API, JVMPI was based on the event model.

Profiling tools that utilized JVMPI had to implement the function call interface and register for various events in order to get various VM memory stats. When a registered event occurred, the application's VM captured a memory snapshot by querying the object hierarchy. This was very time consuming and it interfered with the running application. Also, when the profiling tool registered all the exposed events, it slowed down the VM considerably. As a result, many vendors stayed away from developing profiling tools with this interface.

JVMTI


As JVMPI had some shortcomings and was not offering finer-grain control of the running JVM, Sun introduced JVMTI with JDK 5.0 as an experimental interface model to profile the JVM. It provides ways to both inspect the state and control the execution of applications running in the JVM. JVMTI supports the full breadth of tools that need access to JVM state, including but not limited to profiling, debugging, monitoring, thread analysis, and coverage analysis tools. The JVMTI model supports both sampling and instrumentation of the JVM.

JVMTI is a two-way (query and control) interface. A client of JVMTI can be notified of interesting occurrences through events. Using JVMTI, profilers can query and control the application through many functions. The native in-process interface allows maximal control with minimal intrusion from the tool.

So what are you waiting for, if you are facing any kind of memory related issues in your Java application, NetBeans profiler is there to help you out.

Friday, September 18, 2009

Free alternative to FlexBuilder in 10 easy steps!

After downloading Flex SDK and documentation last week, I came to know about Adobe FlexBuilder, a commercial tool available from Adobe website to develop flex applications. I was really disappointed when i was not able to find even a single free alternative to FlexBuilder. FlexBuilder is basically based upon eclipse and as soon as I came to know about it, I start wondering if something similar is available for NetBeans as well. And here it is. After some googling around, I found a plugin for NetBeans which would allow me to develop Flex Applications for free. Its pretty easy to install and configure. The only downside being that it won’t give you code auto-complete feature. But anyways, as a beginner its good for me to write my own code instead of making bad habit of code to auto-complete. Below is a quick walkthrough of how I was able to install flex plugin into my NetBeans IDE in just 10 easy steps.

1. To download the FlexBean plugin, click here.

2. Once downloaded, please start NetBeans IDE and click Tools >> Plugins

3. Now in “Plugins” dialog box select “Downloaded” Tab and click “Add Plugins…” button.

4. From “Add Plugins…” dialog please browse to the directory where you downloaded the flex bean plugin file and select it. Finally, click “Open” button to add the plugin to plugin list.



5. Now click “Install” button with FlexBean selected and you will be presented with a FlexBean installation wizard.

6. Click Next, accept the agreement, and finally click install to install the plugin in NetBeans.

7. Click finish and then close all the dialogs to return to NetBeans workbench.



8. To configure FlexBean plugin properly, it needs to know the location where the Flex SDK has been extracted. Click Tools >> Flex Platform.

9. From “Flex Platforms” dialog select "Add Platform…” Button and select the folder where you have extracted Flex SDK and click Next.

10. Finally, write a name of the platform in the next dialog and click Finish.

This is all you need to do in order to make the NetBeans IDE ready for Flex editing.

Next, I will be creating our first flex application and get our hands wet with some real water. Stay tuned!