Posts

Showing posts with the label java

Mystry of Java String.intern

Mystery of Java String.intern We have all learn about how Java has optimised handling string objects. The String class has been made immutable and string literal definition makes sure that existing instance from string pool will be returned instance creating new one. Here is simple code snippet that explains it String firstName = "John"; String firstNameWithNew = new String("John"); String duplicateFirstName = "John"; //All there strings are same System.out.println(firstName.equals(duplicateFirstName) && firstName.equals(firstNameWithNew));//=> true //Since they defiend using string iterals, even there memory references are same System.out.println(firstName == duplicateFirstName);//=> true //Since one of the object has defined explictly using new operator, there memory references are different. System.out.println(firstName == firstNameWithNew);//=> false This is very basic understanding that everyone has. Then I came across String.in...

NPE as well as Null check free code ... Really ?

Image
NPE as well as Null check free code … Really ? Is this how your Java code review start ? NPE is always nightmare for a Java developer. Lets not discussion to much and jump on an usual Java code snippet public interface Service { public boolean switchOn(int timmer); public boolean switchOff(int timmer); //Other controls } public class RefrigeratorService implements Service { // ... } public class HomeServices { private static final int NOW = 0; private static HomeServices service; public static HomeServices get() { //Null Check #1 if(service == null) { service = new HomeServices(); } return service; } public Service getRefrigertorControl() { return new RefrigeratorService(); } public static void main(String[] args) { /* Get Home Services handle */ HomeServices homeServices = HomeServices.get(); //Null Check #2 if(homeServices != null) { Serv...

Java doesn't have "Diamond" problem : Really ..?

Java doesn’t have “Diamond” problem : Really ..? We have been confidently telling that Java doesn’t have diamond problem even if it support multiple inheritance. In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. But do you that the diamond problem is now there in newly introduced Java 8. It is because of the introduction of default methods in Interface. In Java8 Interfaces not only have method declaration but their default implementation. Any class implementing can either use default implementation from interface or provide its own implementation. Since Java class can implement multiple interfaces it is possible that two interfaces has same default methods, that brings diamond problem. How do we overcome diamond problem ? Consider following code snippet public interface InterfaceA { ...

All you need to know about JSON Processing in JEE7

JSON is the choice data transfer format used all RESTful services because it is light weight, open standard, less verbose and easy to understand. One of the most important factor for all current generation services is performance and JSON format satisfies all such needs. There are already few third party Java APIs available to JSON parsing but with growing popularity, JEE introduced new JSON processing 1.0 API as part of JSR-353. This API supports both object model as well as streaming model for processing JSON, which are similar to those used for XML documents.This API is limited to JSON processing but not binding, JSON-binding framework will be available in subsequent version of JEE. Object vs Streaming Model : The object model creates a tree that represents the JSON data in memory. The tree can then be navigated, analyzed, or modified. This approach is the most flexible and allows for processing that requires access to the complete contents of the tree. However, it is often sl...

Getting started with Java 7

Recently out product has adapted JDK 7 for development as well as run-time. So get familiar everybody in the team with the new features of JDK , so i prepared a presentation "Getting started with Java 7". Sharing same presentation to refer you haven't still adapted JDK7.  Presentation:

Serializable Singleton Class

Previously I have posted about Singleton Design Pattern implementation and handling concurrency . In this post I am going to explains challenges for serializable Singleton class. Lets know about Singleton Class ( Single Design Pattern), A Singleton class restricts access to its constructor to ensure that only a single instance is ever created. I have taken example from previous post only   1: /** 2: * Singleton Class 3: * 4: * @author ydevatra 5: * 6: */ 7: public class SingletonResource implements Serializable { 8:   9: /* Private instance reference */ 10: private static SingletonResource resource = new SingletonResource(); 11:   12: /** 13: * Private constructor to restrict creation of instance of this class by 14: * other class. 15: */ 16: private SingletonResource() { 17: // Initialization code... 18: } 19:   20: ...