Search This Blog

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday 15 December 2016

Difference between PATH and CLASSPATH in Java

Here are some of the common difference between PATH vs CLASSPATH in Java :



1) PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. CLASSPATH, an environment variable is used by Java to locate and load compile Java bytecodes stored in the .class file.

2) In order to set PATH in Java, you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.

3) Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands.



4) PATH environment variable is used by operating system to find any binary or command typed in the shell, this is true for both Windows and Linux environment while CLASSPATH is only used by Java ClassLoaders to load class files.


These are some notable difference between PATH vs CLASSPATH in Java.
How to set PATH and CLASSPATH in Windows and Unix
Use command prompt in Windows or shell in Linux to set PATH and CLASSPATH . Both PATH and CLASSPATH are environment variable and can be set using 
set keyword in DOS and Windows and export command in Linux

Command to set PATH in Windows

set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin

Command to set PATH in UNIX/Linux

export PATH = ${PATH}:/opt/Java/JDK1.6.18/bin

In Linux use a colon(:) as a 
separator and in Windows use semi-colon(;) as a separator.

Command to set CLASSPATH in windows

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib

Command to set CLASSPATH in Unix/Linux

export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib





Wednesday 30 November 2016

What is static class loading and dynamic class loading in Java

Static Loading
Classes are statically loaded in Java using new operator.

For Example :
class MyClass {
public static void main(String args[]) {
Car c = new Car();
}
}

A NoClassDefFoundException is thrown if a class is referenced with Java’s new operator but the runtime system cannot find the referenced class.


Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time.
The following specifies how classes are loaded dynamically in Java

Class.forName (String className); //static method which returns a Class
The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time.
Unlike the static loading, the dynamic loading will decide whether to load the class ClassXXX  or the class ClassYYY at runtime. Once the class is dynamically loaded the following method returns an instance of the loaded class.


class.newInstance (); //A non-static method, which creates an instance of a class
Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list.

. . . read more

Wednesday 9 November 2016

Difference between String, StringBuffer and StringBuilder

String

The String class represents character strings. All string literals in Java programs, such as "xyz", are implemented as instances of String class.
Strings are constant, i.e. their values cannot be changed after they are created.
String objects are immutable and they can be shared.

For example:

     String str = "xyz";

is equivalent to:

     char data[] = {'x', 'y', 'z'};
     String str = new String(data);

The class String includes methods for
  • Examining individual characters of the sequence
  • Comparing strings
  • Searching strings
  • Extracting substrings
  • Creating a copy of a string with all characters translated to uppercase or to lowercase.



The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

String conversions are implemented through  toString method, which is defined in  Object class.

StringBuffer

StringBuffer is a thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.
String buffers are safe for use by multiple threads. The methods of StringBuffer are synchronized.

The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type.
Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer.
The append method always adds these characters at the end of the buffer
The insert method adds the characters at a specified point.

For example,
if str refers to a string buffer object whose current contents are "pot",
then the method call z.append("s") would cause the string buffer to contain "pots",
whereas z.insert(2, "s") would alter the string buffer to contain "post".


If sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

StringBuilder


Since Java 1.5
StringBuilder is a  mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization.
This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread
Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.


Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.




Friday 4 November 2016

What is JVM, JRE and JDK

Java Virtual Machine (JVM)
The Java Virtual machine (JVM) is the virtual machine that run the Java bytecodes. The JVM doesn't understand Java source code.
You compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM.
JVM makes Java to be a "portable language" (write once, run anywhere).
There are specific implementations of the JVM for different systems (Windows, Linux, MacOS) the aim is that with the same bytecodes they all give the same results.

Java Runtime Environment (JRE)
The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. The JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications. If we want only to execute the programs written by others, then JRE alone will be sufficient.

Java Development Kit (JDK)
The JDK is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.