Monday, December 30, 2013

Learning Data Structures now

Nowadays, I am learning data structures starting with simple sorting techniques, searches, linked-list, stack algorithms etc, and I will post about my experiences. I don't know how far, but I'll share so far I learn. :-)

Insertion Sorting Logic

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. -Wiki

Monday, December 16, 2013

LinkedList: Node creation and display example


package ds;

/**
 * @author Rajdeep
 * 
 * Note
 * Creating the list:
 * 1. 'start' refers to the first node and 'now' is made to refer to the latest
 *   added node.
 * 2. after creating the first linkedlist object and assigning value,
 *   'start' points to it because we are checking for start==null
 *   'start' is null in first node addition only.
 *   'now' also points to it, this being the latest node.
 * 3. Second node onwards, start is never null, so control goes to else part.
 *   'now.next' points to nothing because its of reference type.
 *   so assign 'now.next' to the newly created object after
 *   checking 'now.next==null'.
 * 4. Now 'start' pointed or the first object's (node's) 'next' part is linked
 *   to the 2nd object since now was referring to it, and then after
 *   'while' execution, 'now' points to the newly created node, referred by
 *   the local 'temp' variable.
 * 5. e.g.: start -> [10|add1] at add0 -> [20|add2] at add1 -> [30|NULL] at add2;
 *   'now' points to this add2 having info 30, and 'now.next' is NULL,
 *   so that when another node is created, 'now.next' refers to the new
 *   node, and then 'now' refers to the new node, and so on.
 * 
 * Displaying the list:
 * We start from the starting node, and display 'info' part of each node, and
 * move on to next node by checking the current node's 'next' part is not null.
 */

// This is the Linked List node
class LinkedList{
 int info;   //has value
 LinkedList next; //for pointing to next node
}

// Demonstration on creation of node and displaying the values
public class LinkedListDemo {

 static LinkedList start = null;
 static LinkedList now = null;
 
 public static void main(String[] args) {
  createNode(0);
  createNode(1);
  createNode(5);
  createNode(7);
  createNode(10);
  createNode(11);
  createNode(54);
  createNode(76);
  
  display();
 }
 
 static void createNode(int val){
  LinkedList temp = new LinkedList();
  
  temp.info = val;
  if (start==null){
   start = temp;
   now = temp;
  }
  else{
   while(now.next==null){
    now.next=temp; 
   }
   now = temp;
  }
 }
 
 static void display(){
  LinkedList temp = start;
  System.out.print("[");
  while(temp!=null){
   System.out.print(temp.info + "|");
   temp = temp.next;
  }
  System.out.println("\b]");
 }

}
The output will be:
[0|1|5|7|10|11|54|76]
This was a demo program for my understanding, and now I understand how the nodes are connected, and so the term "linked list".

Tuesday, October 1, 2013

The try-catch-finally love affair!

Ok, everybody here already know about the affair, I am just discussing some more bits of this love triangle.
Lets dig hard to the shallow level :)

try{
    //try something
}
//and if the trial goes wrong, catch the issue and handle
catch(Exception e){
    //handling the problematic part
    //or at least letting the program to continue
    //and exit gracefully and not shut at face!
}
//but
finally{
    //do this, so you are the winner!!
}

So from even the baddest of our common-sense, we smell that finally block supercedes the two others, viz., try and catch. Lets prove a point in this.

Tuesday, August 13, 2013

How many objects are created when you create an object of a plain class?

This is shared out of a random thought. Let’s say we’ve got two classes:
class A{}
class B extends A{
 static B obj = new B();
}
Do not read further until you have the answer to the question.

Tuesday, July 16, 2013

Understanding the Java bytecode even before the JVM takes it!

Yeah....passion and endurance is what I'll need in order to go through the wreckage of my so-beautifully-written piece of Java code, bytecode as known by nerds.
I'll update what and how I learned once I kick in its tooth. And with that I mean, ASAP.
3 days later….
javap -c
Legends say that the "p" stands for printer. But we never met 'em... And "c" option is for disassembling the class files.
javap as described in the JSE documentation is an utility that disassembles the class files provided in its argument and prints a human-readable version of the bytecode.
I stumbled upon this utility just for enjoyment and in my first encounter, I instantly entered "exit" when I saw a weird output on my console. But now, I find it as the first step to understand program behavior for simple programs (I'll never do that for a bigger program :D)

Monday, June 10, 2013

How to create a tar.Z file? The .Z extension in Unix!

We are talking on how to create a tar.Z (nicknamed .taz) file in UNIX-based systems like Linux, because this particular file format could not be easily found, so I tried randomly and learned, leading to this share. This is a normally compressed tar file in Unix-based systems The name of the utility itself is compress[1] [2].

Monday, June 3, 2013

Loose Coupling and High Cohesion in Java


These are the most religiously known or learnt but orthodoxly neglected principles of so-called Object-Oriented-Programming concept.

Tuesday, May 7, 2013

What is SQL injection? - Infographic


A lot of SQL queries function behind our everyday use, be it on Facebook, Twitter, Gmail or university result. The information that we enter in the web pages or web apps are taken and put into SQL queries to fetch our resutls. The simplest that anyone understands is their username and password for a specific online service. Okay, good. But what if someone passes some tricky input to those fields in order to mess up the system or just by mistake. If there is strong validation in presentation layer (in the user front-end itself), then user will be notified about the wrong/undesirable input, and what if there's no stringent validation?

Monday, April 15, 2013

resultSet.next() always returns “false”!!

You might be executing some query in your application and even though that you verified all the connection parameters like connection URL to the database, username and password, and the SQL statement that you fired, and that data also resides in the database in proper table and there are absolutely no case-related errors like “Scott” in place of “scott” user, resultSet.next() is always returning false.

Thursday, March 7, 2013

A suggestion on how to write the variable names in Java

Following Java variable naming conventions is a very important practice. First of all, if you are breaking the rules, then you will be punished with "Invalid Character" compilation error. Secondly, if you are not following the conventions, it will make your variable names confusing or unmeaningful or both to yourself and others who will use your code. We don't want our life difficult by even a micro-unit. You too.
For example, see the following two code snippets that show off the difference.

Thursday, February 21, 2013

What happens when we compile a program: "javac HelloWorld"?

When you go for compilation of a simple class, what happens? A .class file is generated for that class. Yes. But what the -verbose switch tells us is appealing. I mean by javac -verbose HelloWorld.java. Obviously you do not have anything to do with the information other than to learn for yourself the happenings, and also have some idea about the application's performance, if it's big. Let's go with a demo.

Wednesday, January 30, 2013

Removing unnecessary methods with inline code

Note: This holds only for smaller or restricted applications wherein the method logic is called at exactly one place or so. This is strict violation of object-oriented programming principles which advocate that each task or purpose should be served upon by an individual method as it warrants easy extensibility, and maintenance, apart from giving a clean picture of its job.
So, in any application, if we see that there might be future implementation change possibilities or the method is being called or may be called from multiple places in the application, then, even though if the current implementation has it that the method will be called from only one place, we have to put the logic in a separate method with an appropriate name and also mention in comment about the usage, if any.

Objective: "Put a method's logic into the body of its caller and remove the method”.

Friday, January 11, 2013

Exception in thread "main" java.lang.ExceptionInInitializerError


The API says:
Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

Lets take an example and see what is it exactly:

Tuesday, January 8, 2013

Time taken by various string concatenation methods

As the title goes, the objective of the folowing program will be to find out the time difference or performance various string concatenation methods:

i) + operator
ii) String class' concat() method
iii) StringBuffer class' append() method
iv) StringBuilder class' append() method

The program is followed by results in nanoseconds.

Saturday, January 5, 2013

Java Keywords

As of Java 7, there are 50 keywords. I have compiled them for convenience by grouping them into different categories.

> The static keyword has been mentioned twice just for the sake of completing the category.
> In data types, all keywords but void may be used as data type as well as return type of methods, void can be used only as return type.

Wednesday, January 2, 2013

JVM - the runtime data areas (Part 2/2)

Part 2 is continuation of Part 1. If you have not read Part 1 or want to review, please head here.

I have created some independent images which have a small program on each and the memory allocation being shown. As I informed in Part 1, our main concern will be three runtime areas, viz., Method Area, Heap Area, and Java Stacks Area, so I have only shown them in the following explanations.

final and immutable things in Java, briefly!

I was always doubtful over two terms in Java (not limited to, though!), so they get a quote respect here:
final, and immutable
If one goes by English literature, both of them are twins or at least best friends. In Java, there's considerable difference in their usage, although their meanings are stunningly similar.
I've never heard of

JVM - the runtime data areas (Part 1/2)

Hi, This is Part 1 of a 2 part series that unfolds the abstract view of runtime memory allocation. You will understand how the variables get memory, how methods are created, how the control might flow. I have explained it in layman's language, for insight I will add links to a few external resources. Link to part 2 is provided at the bottom of this article.