Tuesday, August 25, 2015

Java program to convert numbers to words

Output -

78='Seventy Eight'
456='Four Hundred Fifty Six'
1,000='One Thousand '
99,999='Ninety Nine Thousand Nine Hundred Ninety Nine'
199,099='One Lakh Ninety Nine Thousand Ninety Nine'
10,005,000='One Crore Five Thousand '

Tuesday, June 23, 2015

JSch - Upload/download files from remote server

We will using JSch to connect our unix box. Once connected you should be able to upload or download the required files.

JSch is a pure Java implementation of SSH2. To know more click here.

For running below code you need to add jsh jar file (e.g. jsch-0.1.53.jar) in your applications classpath. Download jsch-0.1.53.jar from here.

Please see the self explanatory code below -

Tuesday, June 9, 2015

Serialization problem with Singleton

In previous example we have seen the basics of singleton. In this example we will see how Serialization can effect the Singleton design pattern.

Sometimes in distributed systems, we may need to implement Serializable interface in Singleton class so that we can store it's object state in a file system and retrieve it at later point of time. Let's take an exammple of Clipboard class which implements Serializable interface.



Output :
c1 hashCode = 28117098
c2 hashCode = 13495805

From the above hashCode output we can see the intent of singleton pattern is simply blown up, to overcome this problem we need to provide the implementation of readResolve() method as shown below.

Now if u see the hashCode output, it will be same.

The readResolve method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules.

Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signature - ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;

Thursday, October 30, 2014

Top Ten Reasons for learning Java Programming Language

Java is the best programming languages created ever as it has last 20 years by gaining popularity every passing day. Although there were occasions when Java development slowed down, but with path breaking changes in form of Enum, Autoboxing and Generics in Java 5, Google's choice of language for Android apps development and performance improvement with Java 6, kept Java as top programming language. Also In terms of Job opportunities and popularity Java outscore every one with lots of Jobs opportunity available. You can work on developing core Java based server side application, can even go for Android based mobile application development and J2EE web and enterprise applications.

Here are top 10 reason for Learning Java Programming Language

1) Java is Easy to learn

Java has fluent English like syntax which makes it easy to read Java program and learn quickly. Once you are familiar with initial hurdles with installing JDK and setting up PATH and understand How Classpath works, it's easy to write program in Java.

2) Java is an Object Oriented Programming Language

Java is an Object Oriented Programming language. Developing OOPS application is easier, and it also helps to keep system modular, flexible and extensible. You can use all key concepts like AbstractionEncapsulationPolymorphism and Inheritance. Java also promotes use of SOLID and Object oriented design principles in form of open source projects like spring, which make sure your object dependency is well managed by using dependency Injection principle.

3) Java has Rich API

Java provides API for networking, I/O, utilities, xml parsing, database connection, and almost everything. Whatever left is covered by open source libraries like Apache Commons, Google Guava and others.

4) Powerful development tools e.g. Eclipse, Netbeans

Eclipse and Netbeans has played huge role to make Java one of the best programming language. Coding in IDE is a treat, especially if you have coded in DOS Editor or Notepad. They not only help in code completion but also provide powerful debugging capability, which is essential for development and testing. Integrated Development Environment made Java development much easier, faster and fluent. Apart from IDE, Java platform also has several other tools like Maven and ANT for building Java applications, JConsole, decompilers, Visual VM for monitoring Heap usage etc.

5) Good collection of Open Source libraries

Open source libraries ensures that Java should be used everywhere. Apache, Google, and other organization has contributed lot of great libraries, which makes Java development easy, faster and cost effective. There are framework like Spring, Struts, Maven, which ensures that Java development follows best practices of software craftsmanship, promotes use of design patterns and assisted Java developers to get there job done.