Browsed by
Category: Java

Serializing an object with static property

Serializing an object with static property

Serialization is often used to convert the in memory objects to stream and vice-versa. But static members are not part the object states so they are not serialized in this process. You need to serialize an object that has some static properties. The answer is, you need to control the process of serialization and De-Serialization for those objects. To do this in java you need to implement the interface Externalizable. This is similar to the Serializable interface but with more control…

Read More Read More

Traversing an array without using length property

Traversing an array without using length property

We often face an interview question when it is asked to search or traverse an array without knowing/using the length property. In Java, if you try to access the element that is outside the length of the array it will give you an exception as java.lang.ArrayIndexOutOfBoundsException. Here I have one implementation of a binary search where I will not use the length property of the array. import static java.lang.Math.pow; public class BinarySearchWithoutLength { public static Integer binarySearch(final Integer[] elements, final Integer find) {…

Read More Read More

Why a constructor should not throw an exception?

Why a constructor should not throw an exception?

Just imagine if somebody asks you to do some task and when you have done 80% of it they come and ask to stop. Because you have not met preconditions for it. How will you feel about it? Something similar happens when you throw an exception from a constructor. What are constructors? (If you already understand this, let’s revisit or just skip to next para) A constructor is a special method that is responsible for initialization of the variable of…

Read More Read More

Java Version Features (A brief History)

Java Version Features (A brief History)

A popular interview question for any Java Developer is “what was new in Java version X?” and if you mention that you have worked with 2 different versions of Java then it’s very likely to have a followup question “what was added in the later one?”. So here are the important new features added in each major Java release with their code names(At least I was asked for code name once). Java Version SE 8 Features forEach() method in Iterable interface default…

Read More Read More

Introduction to ORM (Hibernate)

Introduction to ORM (Hibernate)

Hibernate, If you are a Java developer I am sure you heard this name already. Yes! it is one of the ORMs available in the market. ORM tool or you can say an ORM Frameworks stands for Object Relational Mapping. If you have never worked with any ORM tool then your project structure will be like this   As you have used JDBC for a long time and we know that it is very powerful API for a Java developer…

Read More Read More

Fetch Whole Database in one Query in hibernate

Fetch Whole Database in one Query in hibernate

Hibernate is most widely used ORM tool for Java (at least by the time I am writing this). It has lot’s of Awesome feature and tricks to manage Entities, Implicit Inheritance is one of them. You can use a parent to fetch some entity and it will load it’s some entities. Key: Every entity is a subclass of Object class. If you create criteria for Object class you will get all the entities data. Session session = factory.openSession(); List l=session.createCriteria(Object.class).list();…

Read More Read More

Starting Apache Velocity with struts

Starting Apache Velocity with struts

What is Velocity? Ans: Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code. When Velocity is used for web development, Web designers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, meaning that web page designers can focus solely on creating a site that looks good, and programmers can focus solely on writing top-notch code. Velocity separates Java…

Read More Read More