One of the common task for every programmer is is to compare two files and find out difference between two files. Though there are lot of tools already exists to compare two files e.g. diff command in UNIX, and Win Merge and Beyond compare in Windows, they are external tools. Beyond compare is not even free, but to be frank it's worth of money. I wanted to compare two files directly from Eclipse. Actually, I have been using Beyond compare from long time, but I have practising to do every task ( which I can ) from Eclipse to save time by avoiding transition between multiple applications. This leads me to find out how do I compare two files in Eclipse, it could be Java files, text files, or simply .properties or XML files. This search lead me to find the option I am going to share you with you guys here. I was very surprised that, even though using compare feature of Eclipse to compare local file with remote file in CVS, SVN, TFS and other source control systems in Eclipse for more than 5 to 6 years, I had missed this important feature to compare two totally unrelated files. This is a very good example of how much Java developer know about the IDE they use every day. There are many such features, and I am sure you guys can share something similar too. By using this "compare with each other option" you can just select two files and compare it right there in Eclipse itself.
Friday, August 1, 2014
Wednesday, July 30, 2014
Classpath in Java is not only used to load .class files, but also can be used to load resources e.g. properties file, images, icons, thumbnails, or any binary content. Java provides API to read these resources as InputStream or URL. Suppose, you have a properties file inside config folder of your project, and you want to load that properties file, how do you do that? Similarly, you have icons and thumbnails for your web applications on icons directory of your project, how do you load them? Answer is by using java.lang.Class' getResource() and getResourceAsStream() method. These method accepts path of resource as String and returns URL and InputStream respectively. You can obtain a reference of Class by calling either getClass() method or by using class literal. If you have an object, then you can call getClass() because its a non-static method, on the other hand, if you don't have any object, you can simply use .class with name of any class e.g. Sample.class will give you reference of java.lang.Class. These methods are available from JDK 1.1 and you can even use them anywhere you have access to core Java library. If you are creating J2ME games or application, you can use these method to load icons and tiles for your game, and all other resource for your application as well.
Monday, July 28, 2014
Java 8 now allows you to add non-abstract method implementations to interfaces by utilizing the default and static keyword. Methods with default keyword are known as default methods or defender methods in Java. Before Java 8, it was virtually impossible to change an interface once published. Any change e.g. addition of a new method would have broken all clients. That's why when Java 8 decided to switch to internal iterator implementation using forEach() method, they face a daunting challenge of breaking all implementation of Iterable interface. Since backward compatibility is top priority for Java engineers, and it wasn't practical to break all clients, they came up with idea of default method. This is an amazing and very powerful change, because now you can evolve your existing interface with all the knowledge you have gained after using them. JDK itself is utilizing default methods in big way, java.util.Map interface is extended with several new default methods e.g. replaceAll(), putIfAbsent(Key k, Value v) and others. By the way, Since default method allows extension of existing interface, it’s also known as Extension method. You are also free to define any number of default method in your interface. I think after this change, you unlikely need an abstract class to provide skeletal implementation as described in Effective Java e.g. List comes with AbstractList, Collection comes with AbstractCollection, Set comes with AbstractSet and Map comes with AbstractMap. Instead of creating a new abstract class with default implementation, you can define them as default method inside interface itself. Similarly, introduction of static methods inside interface will make pattern of an interface utility class redundant e.g. Collections for Collection interface, Paths for Path and so on. You can directly define static utility method on interface itself.