Browsed by
Category: Java

Exploring some interesting stuffs in java.



Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that represents the original string.

For example SHA-2 Hash of above statement will be 696421ad8a148a5e4c4064b9c9191831b6bed4aa484b2ccebe3208184bb30b29

Check Online at

Other hashing are MD5 and SHA-1 (previous version of SHA – 2).

Importance of Hashing Functions can be judged based upon there use cases
1) BlockChain Powered BitCoin (Digital Currency) core is SHA-2 Hash.
2) Digital Signature for Documents.

WeakReference and WeakHashMap in Java

WeakReference and WeakHashMap in Java

Weak Reference :
An Object will not be garbage collected if one or more reference exits. This is not true always if the references are Weak Reference then its a valid candidate of Garbage Collection.

four types of references in Java i.e. strong reference, soft reference, weak reference and phantom reference. We will discuss about these references later.

Lets start with an example to understand Weak Reference.
Suppose you have a requirement to store current Active visitors with the entry time.

Now you can see v1 left but the entry still exist in HashMap. So it shouldn’t exist in the Map and v1 should get garbage collected but this will not because its a strong reference.

The output to above program will be

Where as if you change the HashMap to WeakHashMap(Predefined Map which uses weak reference.there is a private method name expungeStaleEntries(), which is used to remove stale entries as given in above example and this method is used in all exposed methods of WeakHashMap) .

Note :System.gc() is a request not a command but by JVM respects my request 🙂


The output will be as below

So you can use WeakHashMap whenever entries are no more used. Bit of google will help to understand it better.

JAVA 7 Features

JAVA 7 Features

Key Features

  • Strings in switch Statement
  • Multiple Exception Handling
  • Try with Resources
  • Java nio Package
  • Binary Literals, underscore in literals
  • Diamond Syntax
  • Many others refer to Oracle Release note

JAVA 7 : Code named Dolphin and released on July 28, 2011.

Release Note
Oracle Release Note
Feature Page

Support of String in Switch Case.

String in Switch support are useful for processing data without conversion to enum or if-else logic. Sometimes is just easier to switch on String.

The switch statement when used with a String uses the equals() method to compare the given expression to each value in the case statement and is therefore case-sensitive and will throw a NullPointerException if the expression is null

Binary Literals
Binary literals are new features in Java 7. As you all know that we can write integral types (byte, short, int, and long) in Binary and Hexadecimal formats but from Java 7 onwards we can write these numbers in binary format also. The number should be prefixed with 0b or 0B to be treated as binary literal.

This feature is very helpful to bit-oriented systems like processors, network protocols and bitmapped hardware device. Early the programmer used to transform from binary to decimal/hexadecimal and vice versa. Using this feature will remove this transformation and chances of error will be less in this conversion.
ex :

Underscore Between Literals
In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

Diamond Syntax/Operator
The diamond operator is a nice feature as you don’t have to repeat yourself. It makes sense to define the type once when you declare the type but just doesn’t make sense to define it again on the right side.

The Diamond Operator reduces some of Java’s verbosity surrounding generics by having the compiler infer parameter types for constructors of generic classes.

Catching Multiple Exception Types
In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.
Consider the following example, which contains duplicate code in each of the catch blocks:

In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.

The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:

The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.

Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each. A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers.

Rethrowing Exceptions with Improved Type Checking
The Java SE 7 compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE. This enables you to specify more specific exception types in the throws clause of a method declaration.

Consider the following example:

This examples’s try block could throw either FirstException or SecondException. Suppose you want to specify these exception types in the throws clause of the rethrowException method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the catch clause, e, is type Exception, and the catch block rethrows the exception parameter e, you can only specify the exception type Exception in the throws clause of the rethrowException method declaration.

However, in Java SE 7, you can specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration. The Java SE 7 compiler can determine that the exception thrown by the statement throw e must have come from the try block, and the only exceptions thrown by the try block can be FirstException and SecondException. Even though the exception parameter of the catch clause, e, is type Exception, the compiler can determine that it is an instance of either FirstException or SecondException:

Try with Resources
The try-with-resources statement is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement, can be used as a resource.

Native Keyword / Calling C Program From Java

Native Keyword / Calling C Program From Java

You might have seen in Java API many methods are using native keyword. Lets understand them.

The native keyword is applied to a method to indicate that the method is implemented in native code using JNI.

Definition From JSR
A method that is native is implemented in platform-dependent code, typically written in another programming language such as C, C++, FORTRAN,or assembly language. The body of a native method is given as a semicolon only, indicating that the implementation is omitted, instead of a block.

Still looking more .. google it 🙂

Lets start with an example :

see this link

Stay Tuned… will post an example soon.

Ant Build Tool : Quick Notes

Ant Build Tool : Quick Notes

Ant Script for JAVA Developers

1) Simple Ant Script for Clean , Compile and jar generation adding Manifest information to jar

Note :- All folder path are relative to basedir . Here is basedir=”.”
Note :- the values at class-path are separated by space

2) Using Property in Ant Script

3) Reading From Property File

4) Current Time Stamp in Ant Script

5) Directory Opertaions
Make dir

6) Ant Script and Svn

SVN Checkout

Note :- for https:/ type svn
7)File uploads and Ant Script

8) Variable of Varible in Ant Scipt

9)creating war and zip file in ant script

10) Creating Custom Ant Tasks

Required Jar files for ant’s different task
antlr-runtime-3.1.3.jar commons-net.jar ganymed.jar jna.jar
jsch-0.1.44.jar sqljet.1.0.4.jar svnant.jar svnClientAdapter.jar
svnjavahl.jar svnkit.jar svnkit-cli.jar
svnkit-javahl.jar trilead.jar