Wednesday, October 31, 2012

How to set JAVA_HOME and PATH in Linux and Windows

Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture.
The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1991 and first released in 1995. 

It's been around 20 years and it still doesn't register JAVA_HOME and JAVA_PATH itself. So you end up like these error : 

So it feel like

JAVA_HOME environment variable is for to point to the directory where the Java runtime environment (JRE) is installed on your compute

Why PATH variable is important?
Set the PATH environment variable if you want to be able to conveniently run the executable (javac.exe, java.exe, javadoc.exe, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as

The PATH environment variable is a series of directories separated by semicolons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should have only one bin directory for the JDK in the path at a time (those following the first are ignored), so if one is already present, you can update that particular entry.
The following is an example of a PATH environment variable:

It is useful to set the PATH environment variable permanently so it will persist after rebooting

Windows System
For demo purpose, we assume 
JAVA_HOME  = C:\Java\jdk1.7.0
PATH  = C:\Java\jdk1.7.0\bin;

  • Depending on Window's version find and click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New. Similarly, for JAVA_HOME variable.
  • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Note: You may see a PATH environment variable similar to the following when editing it from the Control Panel:

Linux System
For demo purpose, we assume 
JAVA_HOME  = /disk2/jdk1.7.0
PATH  =  /disk2/jdk1.7.0/bin

  • Launch the Terminal and sudo su to root.
  • Identify where is Java installed on your Linux Machine by typing
    Which java 
  • Now to set the JAVA_HOME globally, we edit the .bashrc or .bash_profile or .profile
    export JAVA_HOME
  • Now set the PATH
    export PATH
  • Finally execute the bash files depending on Linux version. For instance,
    . ~/.bash_profile
    . ~/.profile
  • Type java -version to check the correct Java version.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

Tuesday, October 30, 2012

How subString works in Java

In this post, we'll see how substring is implemented in Java and what's the implication of substring method.

It is used to get the parts of the String and define in the String Class and is overloaded like this
public String substring(int beginIndex)
"unhappy".substring(2) returns "happy"

public String substring(int beginIndex,int endIndex) 
"hamburger".substring(4, 8) returns "urge"

Every time you call substring it returns new String because string is immutable.
If beginIndex = endIndex  then it will not throw any exception, it will return empty String.
Same case if beginIndex = length of the String.
It will throw exception only when beginIndex is –ve, larger then endIndex and larger than String length.
Now we will see how substring works:
If you see the implementation of substring() in String class it look like
               if (beginIndex < 0) {
public String substring(int beginIndex, int endIndex) {
                   throw new StringIndexOutOfBoundsException(beginIndex);
               if (endIndex > count) {
                   throw new StringIndexOutOfBoundsException(endIndex);
               if (beginIndex > endIndex) {
                   throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
               return ((beginIndex == 0) && (endIndex == count)) ? this :
                   new String(offset + beginIndex, endIndex - beginIndex, value);
So basically, it calls String(int offset,int count,char[] value).
Interesting thing here is that value[] array is the same character array used to represent the original String.
Main problem is that if Original String is very huge say 1GB, no matter how small the substring be it will be taken the space as 1GB.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...

Monday, October 29, 2012

What is Atomic Operation in java?

Atomic means each action take place in one step without interruption or we can say that operation is performed as a single unit of work without the possibility of interference from other operations.
An Atomic operation cannot stop in the middle, either it happened completely or doesn't happen at all.

Java language specification guarantees that
  • Reading or writing of a variable/reference is atomic unless the variable is of type long or double.
  • Read and write are atomic for all variable declared volatile including long and double variables.

Atomic action can be used without fear of thread interference.
Using simple atomic variable access is more efficient than accessing the same variable through synchronized code. But it require more  care and attention from programmer to avoid memory consistency .
Operation i++;

This operation is not atomic as it happens in 3 steps
  1. Reading the current value of i
  2. Increment the current value of i
  3. Writing the new value of i

Since java 1.5, java language provides atomic variable e.g. AtomicInteger or AtomicLong which provides methods like
getAndDecrement(),getAndIncrement() and getAndSet() in java.util.concurrent.atomic package which are all atomic.


Counter class is designed so that invocation of increment () add 1 to the variable c and decrement () subtract 1 from c.
If Counter object is reference from multiple threads, interference b/w threads may prevent from happening as expected.

Suppose 2 thread are accessing the Counter object
  1. Thread A: Retrieve c.
  2. Thread B: Retrieve c.
  3. Thread A: Increment retrieved value; result is 1.
  4. Thread B: Decrement retrieved value; result is -1.
  5. Thread A: Store result in c; c is now 1.
  6. Thread B: Store result in c; c is now -1.

Thread A result is lost and overwritten by Thread B.
Under different circumstances, it might be Thread B result is lost and overwritten by Thread B, or there could be no error.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...