Showing posts with label compilers. Show all posts
Showing posts with label compilers. Show all posts

Monday, 4 July 2011

Java package

Package Definition

A package is a collection of related classes.
It helps Organize your classes into a folder structure and make it easy to locate and use them.
More importantly,It helps improve re-usability.

Syntax:-
package <package_name>;

Package declaration

The first statement, other than comments, in a Java source file, must be the package declaration.
Following the optional package declaration, you can have import statements, which allow you to specify classes from other packages that can be referenced without qualifying them with their package.

Default package. Altho all Java classes are in a directory, it's possible to omit the package declaration. For small programs it's common to omit it, in which case Java creates what it calls a default package. Sun recommends that you do not use default packages.

Example of using package

Example1 : Creating the package:
package p1;
class c1{
public void m1(){
System.out.println("Method m1 of Class c1");
}
public static void main(String args[]){
c1 obj = new c1();
obj.m1();
}
}

Example 2 : Creating the sub packages:
package p1.p2;
class c2{

public void m2(){
System.out.println("Method m2 of Class c2");
}
public static void main(String args[]){
c2 obj = new c2();
obj.m2();
}
}


Compiling the above Classes

When a class is placed in a package the class file must be located in a sub-directory under one of the directories listed in the CLASSPATH. The sub-directory structure must map exactly onto the package name.
Given a CLASSPATH=/lib the above class must reside in one of:
./p1 
/lib/p1

Or  in case of example2 :
./p1/p2
or /lib/p1/p2

The javac compiler creates class files in the current directory by default, even if they are specified as belonging to a package. To get around this, use the -d flag to the compiler . Which will create the necessary directories under the /lib directory, if your classpath is /lib.

Take example1, Save the file as Demo.java.
Compile the file as,
javac – d . Demo.java
Run the code as
java p1.c1

Taking example2:
Save the file as Demo2.java.
Compile the file as
javac – d . Demo2.java

Run the code as
java p1.p2.c2

Importing a package


To create an object  of a class (bundled in a package), in your code, you have to use its fully qualified name.
Ex.
java.awt.event.actionListner object = new java.awt.event.actionListner();

But , it could become tedious to type in the long dot-separated package path name for every class you want to use. Instead it is recommended you use the import statement.

Syntax
import <package_name>;

Once imported , you can use the class without mentioning its fully qualified name.

import java.awt.event.*; // * signifies all classes in this package
import javax.swing.JFrame // here only the JFrame class is imported
//Usage
JFrame f = new JFrame; // without fully qualified name.

To import package

// Using packages created in earlier assignment
package p3;
import p1.*; //imports classes only in package p1 and NOT in the sub-package p2
&nbsp;
class c3{
public void m3(){
System.out.println("Method m3 of Class c3");
}
public static void main(String args[]){
c1 obj1 = new c1();
obj1.m1();
p1.p2.c2 obj2 = new p1.p2.c2();
obj2.m2();
}
}

Save the file as Demo2.java . Compile the file using the command
javac –d .Demo2.java

Execute the code using the command
java p3.c3

Packages - points to note:


  • To avoid naming conflicts packages are given names of the domain name of the company in reverse Ex :  com.guru99. com.microsoft, com.infosys etc.
  • When a package name is not specified , a class is into the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
  • While creating a package, care should be taken that the statement for creating  package must be written before any other import statements
  • When importing packages with * format, only classes in that packages are imported and not sub packages.

Wednesday, 29 June 2011

Tomcat - JSP Precompilation


JSP is usually compiled during runtime by Java server. Some disadvantages are

1. If you JSP page is large, it will take time to compile at runtime. If that is the first hit, the user will have to wait before the page is served to the user. This is a performance bottlenect.

2. Although the current IDE, such as Eclipse, provide JSP syntax check, you may still run into the situation whereby runtime error occurs due to JSP runtime compilation issues.

To overcome these 2 issues, you can precompile your JSP page before putting them into the server. Tomcat come with JSP precompilation tools for your usage.

The following steps provide information on how to perform JSP precompilation

1. Make sure you have your Tomcat server installed. Or, you need the jar files from tomcat/bin and tomcat/lib, as well as tomcat/bin/catalina-tasks.xml. catalina-tasks.xml is a helper file for loading Catalina ant task for you, ie, jasper task.

2. Make sure you have Apache Ant installed

3. Add the following build script. This build script assume that your jsp source is at your web container.


<project name "Webapp Precompilation" default = "all" basedir= ".">
<import file = "${tomcat.home }/bin/catalina-tasks.xml" />
<target name= " jspc">
<jasper
validateXml = " false"
uriroot= "${webapp.path}"
webXmlFragment= "${webapp path }/WEB-INF/ generat ed we b xml "
outputDir= " $ {webapp.path}/WEB-INF/src" />
</target>

<target name= "compile">
<mkdir dir= " ${webapp.path}/WEB-INF/classes" />
<mkdir dir= " ${webapp.path }/WEB-INF/lib" />
<javac destdir= " ${webapp.path}/WEB-INF/classes"
optimize="off"
debug="on" failonerror= "false"
srcdir= " $ {webapp.path}/WEB-INF/src"
excludes= " ** /*.smap">
<classpath>
<pathelement location= "${webapp.path}/WEB-INF/classes"/>
<fileset dir= "${webapp.path}/WEB-INF/lib">
<include name= " .jar" />
</fileset>
<pathelement location= "${tomcat.home}/lib" />
<fileset dir= "${tomcat.home}/lib">
<include name= "*.jar" />
</fileset>
<fileset dir= "${tomcat.home}/bin">
<include name= "*.jar" />
</fileset>
</classpath>
<include name= " ** " />
<exclude name= "tags/**" />
</javac>
</target >
<target name= "all " depends = "jspc,compile">
</target>
<target name= "cleanup">
<delete>
<fileset dir= "${webapp.path}/WEB-INF/src" />
<fileset dir= "${webapp.path}/WEB-INF/classes/org/apache/jsp"/>
</delete>
</target >
</project>


4. run the script with ant -Dtomcat.home="your tomcat server install home" -Dwebapp.path="your jsp source path". As you can see, tomcat.home is used to locate catalina-tasks.xml and webapp.path is used to locate your libraries and jsp source code. By changing these variables accordingly, you can customize your build path.

5. By default, it will compile your jsp into class file and put them at your "webapp.path"/WEB_INF/classes

Now, how to use these JSP classes file. 2 ways

1. Locate "webapp.path"/WEB_INF/generated_web.xml. Copy these contents into your web.xml

2. Copy all files at "webapp.path"/WEB_INF/classes into your tomcat server work folder. The common path is "your_tomcat_home"/work/Catalina/localhost/_/

You may want to ask why are we not using Ant JSPC task. That task is deprecated due to known problem in Tomcat 1.5 and it won't be fix by Apache Ant as well.

To date, there are 2 known issues for JSP precompilation. below is the abstract from Tomcat



As described in bug 39089, a known JVM issue, bug 6294277, may cause a java.lang.InternalError: name is too long to represent exception when compiling very large JSPs. If this is observed then it may be worked around by using one of the following:
reduce the size of the JSP
disable SMAP generation and JSR-045 support by setting suppressSmap to true.

Dynamic in-memory compilation using javax.tools


Once I needed to calculate expressions dynamically, so one option I got was dynamic in-memory compilation. I searched, and to my surprise, almost at the end of Java 6 (I am expecting Java 7 to be out soon…), I noticed this feature under javax.tools package. May be I am the last one to notice this!!. Similar feature is present in .net as well. But I had to leave this idea, because may be I thought there is some memory overhead, designed whole expression calculator application. But still let's see how dynamic compilation is possible in java.

This dynamic compiler API is included with Java 6 under javax.tools package.

How does it work?
javax.tools package has all the required interfaces and classes. Here, we will see how to compile a simple “HelloWorld” program source code stored in an in-memory String variable.
Able to compile a piece of source code stored in a string variable, WOW! this is interesting! isn’t it?
Follow the sequence of steps mentioned below. I explained these steps with the required code-snippets at that point. The full version of source code is available at the end of the article.

Important API's
The most important classes in this API are,
  • JavaCompiler - This is used to create a compilation task
  • JavaCompiler.CompilationTask – The compilation task, on which we execute compile operation using it’s call method
  • JavaFileManager:Manages how the compiler read and writes to the files
  • JavaFileObject: The file object that abstracts the java source and class files
  • DiagnosticListener: This listens to the compilation diagnostic events
  • ToolProvider: Which is used to get the compiler object from the underlying platform.
We will discuss these classes further in the example below. Let’s start…

Looking at the Example
1. Build the source code to compile; we can read it from file system, retrieve from database, or generate it dynamically in memory!!

Get the source code to be dynamically compiled ready:
StringBuilder src = new StringBuilder();
src.append("public class DynaClass {\n");
src.append(" public String toString() {\n");
src.append(" return \"Hello, I am \" + ");
src.append("this.getClass().getSimpleName();\n");
src.append(" }\n");
src.append("}\n");


Create a JavaFileObject instance for each of the compilation unit.

If the source is not from file system, then we need to write a class implementing from JavaFileObject interface. Java 6 provides a sample implementation of this in the form of SimpleJavaFileObject. We can extend from this and customize it as per our needs.

CharSequenceJavaFileObject implements the SimpleJavaFileObject interface and represents the source code we want to compile. Normally instances of SimpleJavaFileObject would point to a real file in the file system, but in our case we want it to represent a StringBuilder createdy by us dynamically. Let’s see how it goes:

import java.net.URI;

import javax.tools.SimpleJavaFileObject;
import javax.tools.JavaFileObject.Kind;

public class CharSequenceJavaFileObject extends SimpleJavaFileObject {

/**
    * CharSequence representing the source code to be compiled
    */
private CharSequence content;

/**
    * This constructor will store the source code in the
    * internal "content" variable and register it as a
    * source code, using a URI containing the class full name
    *
    * @param className
    * name of the public class in the source code
    * @param content
    * source code to compile
    */
public CharSequenceJavaFileObject(String className,
CharSequence content) {
super(URI.create("string:///" + className.replace('.', '/')
+ Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
}

/**
    * Answers the CharSequence to be compiled. It will give
    * the source code stored in variable "content"
    */
public CharSequence getCharContent(
boolean ignoreEncodingErrors) {
return content;
}
}

If the source code is from file system, then create JavaFileObject instances from the File objects read from the file system.

/*Java source files read from file system*/
File []files = new File[]{file1, file2} ;
Iterable<? extends JavaFileObject> compilationUnits1 =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1));

Though I am keeping the file object as in-memory one, ie not from file system.

Representing the compiled byte code

Next we must define the class representing the output of the compilation – compiled byte code. It is needed by the ClassFileManager which we will describe later. Compiler takes the source code, compiles it and splits out a sequence of bytes which must be stored somewhere. Normally they would be stored in a .class file but in our case we just want to make a byte array out of it. Here is a class that fulfills our needs:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

import javax.tools.SimpleJavaFileObject;

public class JavaClassObject extends SimpleJavaFileObject {

/**
     * Byte code created by the compiler will be stored in this
     * ByteArrayOutputStream so that we can later get the
     * byte array out of it
     * and put it in the memory as an instance of our class.
     */
protected final ByteArrayOutputStream bos =
new ByteArrayOutputStream();

/**
     * Registers the compiled class object under URI
     * containing the class full name
     *
     * @param name
     * Full name of the compiled class
     * @param kind
     * Kind of the data. It will be CLASS in our case
     */
public JavaClassObject(String name, Kind kind) {
super(URI.create("string:///" + name.replace('.', '/')
+ kind.extension), kind);
}

/**
     * Will be used by our file manager to get the byte code that
     * can be put into memory to instantiate our class
     *
     * @return compiled byte code
     */
public byte[] getBytes() {
return bos.toByteArray();
}

/**
     * Will provide the compiler with an output stream that leads
     * to our byte array. This way the compiler will write everything
     * into the byte array that we will instantiate later
     */
@Override
public OutputStream openOutputStream() throws IOException {
return bos;
}
}

At some point of the compilation, compiler will call openOutputStream() method of our JavaClassObject class and write there the compiled byte code. Because the openOutputStream() method returns a reference to the bos variable, everything will be written there, so that afterwards we will be able to get the byte code from it.

FileManager - putting the bytecode into JavaClassObject

We will also need something like a “file manager” that will tell the compiler to put the compiled byte code into an instance of our JavaClassObject class instead of putting it to a file. Here it is:

import java.io.IOException;
import java.security.SecureClassLoader;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.JavaFileObject.Kind;

public class ClassFileManager extends
ForwardingJavaFileManager {
/**
     * Instance of JavaClassObject that will store the
     * compiled bytecode of our class
     */
private JavaClassObject jclassObject;

/**
     * Will initialize the manager with the specified
     * standard java file manager
     *
     * @param standardManger
     */
public ClassFileManager(StandardJavaFileManager
standardManager) {
super(standardManager);
}

/**
     * Will be used by us to get the class loader for our
     * compiled class. It creates an anonymous class
     * extending the SecureClassLoader which uses the
     * byte code created by the compiler and stored in
     * the JavaClassObject, and returns the Class for it
     */
@Override
public ClassLoader getClassLoader(Location location) {
return new SecureClassLoader() {
@Override
protected Class<?> findClass(String name)
throws ClassNotFoundException {
byte[] b = jclassObject.getBytes();
return super.defineClass(name, jclassObject
.getBytes(), 0, b.length);
}
};
}

/**
     * Gives the compiler an instance of the JavaClassObject
     * so that the compiler can write the byte code into it.
     */
public JavaFileObject getJavaFileForOutput(Location location,
String className, Kind kind, FileObject sibling)
throws IOException {
jclassObject = new JavaClassObject(className, kind);
return jclassObject;
}
}

Function getClassLoader() will be called by us to get a ClassLoader instance for instantiating our compiled class. It returns an instance of SecureClassLoader modified by the function findClass(), which in our case gets the compiled byte code stored in the instance of JavaClassObject, defines a class out of it with the function defineClass() and returns it.

Writing our dynamic compiler


import java.util.ArrayList;
import java.util.List;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;

import com.vaani.compiler.files.CharSequenceJavaFileObject;
import com.vaani.compiler.files.ClassFileManager;


public class DynamicCompiler {
private JavaFileManager fileManager ;
private String fullName;
private String sourceCode;

/**
     * @param fullName_
       Full name of the class that will be compiled.
       If class should be in some package,
       fullName should contain it too
       (ex. "testpackage.DynaClass")
     * @param SrcCode_
       Here we specify the source code of the class to be compiled

     */

public DynamicCompiler(String fullName_, String SrcCode_){
fullName = fullName_;
sourceCode=SrcCode_;
fileManager = initFileManager();
}

public JavaFileManager initFileManager(){
if(fileManager!=null)
return fileManager;
else {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
fileManager = new
ClassFileManager(compiler
.getStandardFileManager(null, null, null));
return fileManager;
}

}

public void compile(){
// We get an instance of JavaCompiler. Then
// we create a file manager
// (our custom implementation of it)
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();


// Dynamic compiling requires specifying
// a list of "files" to compile. In our case
// this is a list containing one "file" which is in our case
// our own implementation (see details below)
List<JavaFileObject> jfiles = new ArrayList<JavaFileObject>();
jfiles.add(new CharSequenceJavaFileObject(fullName, sourceCode));

// We specify a task to the compiler. Compiler should use our file
// manager and our list of "files".
// Then we run the compilation with call()
compiler.getTask(null, fileManager, null, null,
null, jfiles).call();

}

public void run() throws InstantiationException,
IllegalAccessException
, ClassNotFoundException{
// Creating an instance of our compiled class and
// running its toString() method
Object instance = fileManager.getClassLoader(null)
.loadClass(fullName).newInstance();
System.out.println(instance);
}
}

As you see the code we want to compile is stored in the variable sourceCode. Also our dynamic compiler object takes 2 parameters, the fully qualified classname and its sourceCode. In the constructor we get both initialized and then we initialize filemanager as well, which will hold the classes in it.

Lets, first focus on the compile method. After we define it, we print it to the console, get an instance of the compiler, put the source code into an object representing a source file.. The real compilation starts when we call the call() method of the compilation task. Then we get the Class representing our compiled class from the file manager, instantiate our class and print it to the console, using the toString() function that we implemented in the code.

There are three classes used in the code that are not available in the JDK and hence we have to implement them by ourselves – CharSequenceJavaFileObject, JavaClassObject and ClassFileManger.
These classes have already been implemented by us and explained as well. 

In the run method, we call getClassLoader() to get the class we need to get instance of.

Running the program

The main method

import com.vaani.compiler.DynamicCompiler;

import com.vaani.compiler.src.SourceCodes;

public class DynaCompTest {


public static void main(String[] args) throws Exception {
// Full name of the class that will be compiled.
// If class should be in some package,
// fullName should contain it too
// (ex. "testpackage.DynaClass")
String fullName = SourceCodes.strDynaClassFullName;

// Here we get and specify the source code of the class to be compiled
String src = SourceCodes.getDynaClassSource();

DynamicCompiler uCompiler = new DynamicCompiler(fullName, src);
uCompiler.compile();
uCompiler.run();

}
}

Output

Now that we have all our classes ready, lets compile them and run the program. We should get an output like this:

public class DynaClass {
public String toString() {
return "Hello, I am " + this.getClass().getSimpleName();
}
}

Hello, I am DynaClass


Application of dynamic compilation
As introduced in the beginning, I needed this for expression calculator, for expressions such as “y=2*(sin(x)+4.0)”. Using dynamic compilation you don’t have to parse it any more by yourself, you could just compile it and get a fast, optimized function representing this expression. You can read about it (and much more about dynamic compilation generally) here.

Some other usage is creating dynamic classes for accessing data stored in JavaBeans. Normally you would have to use reflection for it, but reflection is very slow and its generally better to avoid using it when possible. Dynamic compilation allows you to minimize the use of reflection in a library that handles JavaBeans. How? We will try to show it in one of our next posts, so stay tuned!

Sunday, 15 May 2011

JSP compilers

We can compile a JSP manually.
The JSP compiler(JSPC) depends on the Web Container. Eg. take, BEA WebLogic Application Server.

  • goto DOS Shell and run "setEnv.cmd"
  • using cd command move to the JSP file directory.
  • run " java weblogic.jspc -keepgenerated JspOne.jsp "

Note : if " -keepgenerated " is not used on above command, the JSPCompiler removes the .java file which is generated by JSPC.

Saturday, 30 April 2011

Online java compilers

What is common between Innovation.ch, youjavait.com, ideone.comcompilr.com and browxy.com?

If you are sitting on a machine on which Java is not installed and you can not even download Java because of some reason but you have internet connectivity then there is no one stopping you from compiling and testing your source code. There are many online compilers available on the internet which can be used to see the output of the code you have written.

Some of them are powerful and some are not. Here are some of them reviewed.

1) Innovation.ch:

Positives:
a) You can specify source files using the browse button
b) You can also specify the Jar files which are required for the compilation to complete
c) You can doenload the compiled .class files individually or download a zip/gz file containing all the classes.
d) The compiler interface can be extended by creating your own HTML page and calling the JXXX compiler's cgi APIs.
e) The website can run the applets for you.
f) Supports JDK 1.5 and JDK 1.6
g) The source files can have packages.
h) There are multiple options availale to pass to the javac command.
i) Works with IE as well as Firefox.

Negatives:
a) A maximum of 5 source and 5 jar files can be specified in one go.
b) The source files should have ASCII text only.
c) You can not browse a folder to compile all the source files under the folder.

2) youjavait.com:

Positives:
a) You can specify the program to use the server JDK or local JDK (JAVA_HOME needs to be set).
b) You can maintain multiple projects.
c) You can create multiple source files with built in syntax highlighter and then you can refer these source files as they are in the same package.
d) You can use Google/OpenID account for this website and store projects for later reference.
e) Compiles and runs the program and then displays the output.

Negatives:
a) Seems to run only on IE and not Firefox
b) There is only one default package named com and you can not create more packages.
c) You can not get the compiles .class files.

3) ideone.com:

Positives:
a) Works in Firefox also.
b) It has options for multiple programming languages.
c) You can run the code to view the output.
d) You have a built in syntax highlighter.

Negatives:
a) Though the source code is available for later viewing purpose, you can not get the .class files generated by the compiler.
b) Supports only JDK 1.6

4) browxy.com:

Seems to be not working in neither Firefox and nor IE. You can try your luck with this online Java compiler and Runner.

You can see these observations, but may be some changes they must have dist, after this blog.

Tuesday, 19 April 2011

Significance of searching class path in java

Java is an OOP language. In java programming language for compiling the program we use the compiler that converts the source code into the byte code after that, that byte code is interpreted by JVM that converts the bytecode into the machine independent code. In java how the Java compiler and the JVM use the class search path to locate classes when they are referenced by other Java code. Searching class path is important for all Java developers.Many development tools have their own ways of manipulating the class path, which vary from product to product.For doing this we will use only simple command-line tools to carry out the compile operations. No difference, between the way that the Java compiler searches for classes, and the way that the JVM does it at run time. The compiler has the ability to compile classes from source code, where the JVM does not. We will use the compiler, but similar issues apply at run time.

Searching of multiple class directories

  • javac (compiler) search the files in only one directory at a time.
  • But, class search path will contain numerous directories and JAR archives. The -classpath option to javac and java allows multiple entries to be specified, but the syntax is different for Unix and Windows systems.

For Unix like this :
javac -classpath dir1:dir2:dir3 ...

For Windows like this:
javac -classpath dir1;dir2;dir3 ...

The difference is that Windows uses the colon (:) character as part of a filename, so it can't be used as a filename separator. Naturally the directory separator character is different as well: forward slash (/) for Unix and backslash (\) for Windows.

Wednesday, 15 September 2010

Overview of java

Java is a simple and yet powerful object oriented programming language and it is in many respects similar to C++. Java originated at Sun Microsystems, Inc. in 1991. It was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. It was developed to provide a platform-independent programming language.

Platform independent

Unlike many other programming languages including C and C++ when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.

Java Virtual Machine

What is the Java Virtual Machine? What  is its role?
Java was designed with a concept of ‘write once and run everywhere’ i.e. WORE. Java Virtual Machine plays the central role in this concept. The JVM is the environment in which Java programs execute. It is a software that is implemented on top of real hardware and operating system. When the source code (.java files) is compiled, it is translated into byte codes and then placed into (.class) files. The JVM executes these bytecodes. So Java byte codes can be thought of as the machine language of the JVM. A JVM can either interpret the bytecode one instruction at a time or the bytecode can be compiled further for the real microprocessor using what is called a just-in-time compiler The JVM must be implemented on a particular platform before compiled programs can run on that platform.

Object Oriented Programming

Since Java is an object oriented programming language it has following features:
  • Reusability of Code
  • Emphasis on data rather than procedure
  • Data is hidden and cannot be accessed by external functions
  • Objects can communicate with each other through functions
  • New data and functions can be easily addedJava has powerful features. The following are some of them:-

    Simple
    Reusable
    Portable (Platform Independent)
    Distributed
    Robust
    Secure
    High Performance
    Dynamic
    Threaded
    Interpreted
Object Oriented Programming is a method of implementation in which programs are organized as cooperative collection of objects, each of which represents an instance of a class, and whose classes are all members of a hierarchy of classes united via inheritance relationships.
OOP Concepts
Four principles of Object Oriented Programming are
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction
Abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.
Encapsulation

Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior ; encapsulation serves to separate the contractual interface of an abstraction and its implementation.
Encapsulation
* Hides the implementation details of a class.
* Forces the user to use an interface to access data
* Makes the code more maintainable.
Inheritance
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism
Polymorphism is the existence of the classes or methods in different forms or single name denoting different
implementations.

Java is Distributed

With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and access the objects across net via URLs.


Java is Multithreaded

One of the powerful aspects of the Java language is that it allows multiple threads of execution to run concurrently within the same program A single Java program can have many different threads executing independently and continuously. Multiple Java applets can run on the browser at the same time sharing the CPU time.

Java is Secure

Java was designed to allow secure execution of code across network. To make Java secure many of the features of C and C++ were eliminated. Java does not use Pointers. Java programs cannot access arbitrary addresses in memory.

Garbage collection

Automatic garbage collection is another great feature of Java with which it prevents inadvertent corruption of memory. Similar to C++, Java has a new operator to allocate memory on the heap for a new object. But it does not use delete operator to free the memory as it is done in C++ to free the memory if the object is no longer needed. It is done automatically with garbage collector.

Java Applications

Java has evolved from a simple language providing interactive dynamic content for web pages to a predominant enterprise-enabled programming language suitable for developing significant and critical applications. Today, It is used for many types of applications including Web based applications, Financial applications, Gaming applications, embedded systems, Distributed enterprise applications, mobile applications, Image processors, desktop applications and many more. This site outlines the building blocks of java by stating few java examples along with some java tutorials.