Search More About Above Topic Here

Custom Search

Java.Warning - Build path specifies execution environment J2SE-1.6

Links to this post
When you creat a Maven Project in the Eclipse, your Eclipse may show the following warning
" Build path specifies execution environment J2SE-1.6. There are no JREs installed in the workspace that are strictly compatible with this environment. "
Go to your project 
> properties
-> java build path : Libraries
 - >Remove the "JRE System Library[J2SE 1.6]"
-> click "Add Library" button -> JRE System Library
-> select the new "Executin Environment" or Workspace default JRE
Hope your problem is solved now.

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

Links to this post
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 is 1 for thread1 and 2 for thread2 but these values are not distributed to main memory or other threads.

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

Links to this post
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?

Links to this post
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 begins with the keyword "interface".

3. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses

4. All variables in an Interface are by default - public static final while an abstract class can have instance variables.

How To Use Lazy Loading for Efficient Java Logging

Links to this post
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.

Custom Search
Related Posts with Thumbnails