Archive

Posts Tagged ‘Java’

How coding to Interface and Inheritance helped me reuse code

Before going into the details I will state the requirement:

  • I need to fetch certain records from 3 different tables into 3 ArrayLists.
  • I have 3 Classes which store the information: Lets name it- Class1, Class2, Class3.
  • All the above 3 classes implement the Interface, lets name it Interface1. The interface has a method called init(). The main task of init() is to initiate the values of the instance variables with the values obtained from the ResultSet (database). This instance is then added to one of the 3 lists define above depending on the type of the reference.

Read more…

Categories: Java Basics Tags: ,

Execute external process from within JVM using Apache Commons Exec library

Executing external command from within JVM often causes problems- be it in terms of the code to write and manage or in the ease of implementation. I had similar requirement in my Major project for my Under Graduate Degree, where in I had to launch a C program from the Java code. I ran into different issues like- the Main thread getting blocked, the GUI freezing, or reading the output streams and so on. Finally I had to give up the idea and stick with launching the external command externally 😛 Had I found the Exec library from Apache Commons then, my work would have been lot easier. Anyways better late then never. I will quickly go through how one can use Exec library to launch external programs from JVM- Its a wrapper over Java’s ProcessBuilder, Runtime.getRuntime().exe(). Read more…

Working with Java Enumerated types (Enums)

December 1, 2009 10 comments

In this post I would like to explain about Enums in Java. Though in my 2 years of coding in Java I have seldom used Enums but they do provide a lot of features when we are required to create a say limited instances of certain type. In this post I have made use of an example written out of inspiration from the example program given in “A Programmer’s Guide to Java SCJP certification” by Khalid A. Mughal and Rolf W. Rasmussen. The example is given in the Section Enumerated Types. I will be developing the Enum as we go through the article.

What is an Enumerated Type or Enum tpye? Enum Type defines a finite set of symbolic names and their values. Suppose we have to define 3 constants- LOW, MEDIUM and HIGH to denote the 3 speeds of Rotation. We can do it by declaring static final variables as follows-

class Speed{
   public static final int LOW=1;
   public static final int MEDIUM=2;
   public static final int HIGH=3;
}

Read more…

How’s Scala different from Java?

  • Scala is statically type like Java but with Type Inferencing support. Which means that the scala compiler analyzes the code deeply to determine what type a particular value is.
  • In Scala its not required to use semicolons to terminate a statement if the termination is obvious by line ending. If there are more than one statements on the single line then they have to be separated by using semicolon.
  • Scala doesnot require the file containing a class definition to mirror the name of the class.
  • In Scala there’s no use of static all together. This is replaced by use of singleton object. Singleton objects are declared by using the keyword “object” and not “class”.
  • In Scala, arrays are instances of Array class and uses square brackets ([]) instead of angle brackets (<>) to indicate parameterized types.
  • In Scala the variable declaration is: “name: type”
  • Scala uses keyword “Unit” to represent functions which return “non-value” value.
  • In Scala the syntax for method declaration is different. It uses the “=” before the method body preceeded by the identifier/name of the method along with the parameter list and return type. Functions are treated like variables and constants.
  • Scala supports the use of closures, Annonymous functions- Which makes longer code simpler and shorter.

These were few things i have noted. There are lot more differences. If u know any please add it as a comment. Will upadate my post accordingly.

Categories: Scala Tags: , ,

Monitoring and Profiling using VisualVM-1

I executed a simple GUI application which would load the CSV file and parse it and show the contents in a JTable. When the applications started- There was a JFrame, 2 JPanels, a JLabel and a JButton with an Icon. I wanted to monitor the Heap size variations, the number of Classes, Threads details and also wanted to profile the application. So i thought of using VisualVM. The following are the results and snapshots of profiling using VisualVM. Note that the application had only one public class MainFrame in gui package. Also note that i was using the Nimbus Look and Feel.

Read more…

My JavaFX Application

Was going thru the JavaFX 1.2 API and found a lot of new features some of them are: New JavaFX Controls, New Layouts, Charting API, Local Data Storage among others. I was impressed by few samples which i saw here and here. So i thought of exploring the New JavaFX Controls and created a sample application.

Screenshot of the application:

Run the JNLP file here.

Download the source code here.

Will try out the Chart API as well!!!!

Categories: JavaFX Tags: , ,

Selection Sort: Brute Force approach to sorting with example in Groovy

In Selection sort technique the list is scanned to find the smallest element and it is then exchanged with the first element, putting the smallest element in its final position i.e at the beginning. Then the list is scanned starting from the second element to find the smallest among the remaining n-1 elements and exchange it with the second element to put the second smallest element in its final position. In general in the ith pass the algorithm searches for the smallest number from the n-i elements and swaps it with the element in the ith position i.e Ai. The list is sorted after n-1 passes. Read more…