Skip to main content

Posts

What is the diffrence between "volatile" and "synchronized" keywords?

Volatile keyword is thought of as an easy form of synchronized. It require less coding than synchronized and frequently have less run-time overhead. When we talk about locks, 2 main features introduce: i.) mutual exclusion which means only one thread at a time can hold a given lock, and ii.) visibility which ensures that changes mode to shared data before releasing the lock are accessible to another thread that subsequently acquires the lock. Comparing the volatile keyword with synchronized, the volatile is a field modifier while synchronized is used with code blocks and methods. Let's clear one more point here. Threads can have local copies of variables/data which don't have to be the same as the data held in other threads. In fact, java has an idea of a MAIN memory which holds the current value for variables. And threads can have their own copy of variables which can be different from the main memory. And it's possible that suppose you have a variable named X which value...

Can 'transient' variables be declared as 'final' or 'static'?

Java's serialization provides an elegant, and easy to use mechanism for making an object's state persistent. While controlling object serialization, we might have a particular object data member that we do not want the serialization mechanism to save. The modifier transient can be applied to field members of a class to turn off serialization on these field members. Every field marked as transient will not be serialized. You use the transient keyword to indicate to the Java virtual machine that the transient variable is not part of the persistent state of an object. The transient modifier applies to variables only. Like other variable modifiers in the Java system, you use transient in a class or instance variable declaration like this: class TransientExample { transient int hobo; . . . } This statement declares an integer variable named hobo that is not part of the persistent state of the TransientExample class.

When To Use Abstract Class and Interface?

What is the difference between interface and an abstract class?  This is the first question most of the java interviewers start in interviews. Ofcourse it seems a silly question and can answer. But if interview goes depth of the concept of abstract class and interface, most of the candidates not able to answer. To overcome that ignorance just read the points that I have highlighted below. 1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. A Java Interface can contain only method declarations and public static final constants and doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present. 2. Abstract class definition begins with the keyword "abstract" keyword followed by Class definition. An Interface definition be...

How To Use Lazy Loading for Efficient Java Logging

It is always a good Java programming practice to prefix log statements with a check for "loggability" by testing Logger.isLoggable(Level) prior to logging. However, this practice presents Java developers with a dilemma: Failing to do so results in slower performance, but following it contributes to more total lines of code. Often, developers opt to just bypass it. In this intermediate Java tip, Jason Weinstein offers a solution: lazy loading. By wrapping Java logging in your own class (as shown below), you can ensure the isLoggable() test is always made, while at the same time lazily binding the message creation so that it occurs only when the message is going to be logged.

javax.resource.spi.LocalTransactionException: No more data to read from socket

j avax.ejb.EJBException: Unable to complete container-managed transaction.                 at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5147)                 at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)                 at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)                 at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)            ...

What are the differences between String, StringBuffer and StringBuilder?

I Java the String class is immutable, however StringBuffer and StringBuilder classes are not immutable.  If you want to know why String has been made immutable just click here . There's no reason to use a StringBuffer or StringBuilder unless you're planing on changing the contents. But we should know when to use String StringBuffer and StringBuilder. The String class overrides the default equals() method, but StringBuffer and StringBuilder do not override the default equals() method in Object class. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). For example, public class Program { public static void main(String [] args) { String s1 = new String("Hello World"); String s2 = new String("Hello World"); System.out.println("s...