Search This Blog
Tuesday, November 10, 2009
introduction to java
What is Java?
Java is a high-level object-oriented programming language developed by the Sun Microsystems. Though it is associated with the World Wide Web but it is older than the origin of Web.
Java as an Internet Language
Java is an object oriented language and a very simple language. Because it has no space for complexities. At the initial stages of its development it was called as OAK. OAK was designed for handling set up boxes and devices.
Java as general purpose language
Java is an Object oriented application programming language developed by Sun Microsystems.
Java Features
Case sensitivity
What is case sensitivity: Case sensitivity is the mechanism in which words can be differ in meaning based on different use of uppercase and lowercase letters.
Java is Simple and platform Independent
The concept of Write-once-run-anywhere (known as the Platform independent) is one of the important key feature of java language that makes java as the most powerful language.
Java Enabled browsers
Java language is the most powerful language and is widely used in the web application. Today most of the web browser are java compatible.
Java Tools
Java Compiler
To commence with Java programming, we must know the significance of Java Compiler.
Java Interpreter
We can run Java on most platforms provided a platform must has a Java interpreter.
Java Debugger
Java debugger helps in finding and the fixing of bugs in Java language programs.
Java Header File Generator
In Java programming we need to implement some native methods. To implement these methods Javah generates C header and source files that are used by C programs to reference an Object's instance variables from native source code.
JavaDoc
This tool is used to generate API documentation into HTML format from Java source code.
Applet Viewer
Applet viewer is a command line program to run Java applets.
java tools
To commence with Java programming, we must know the significance of Java Compiler.
To commence with Java programming, we must know the significance of Java Compiler. When we write any program in a text editor like Notepad, we use Java compiler to compile it. A Java Compiler javac is a computer program or set of programs which translates java source code into java byte code.
The output from a Java compiler comes in the form of Java class files (with .class extension). The java source code contained in files end with the .java extension. The file name must be the same as the class name, as classname.java. When the javac compiles the source file defined in a .java files, it generates bytecode for the java source file and saves in a class file with a .class extension.
The most commonly used Java compiler is javac, included in JDK from Sun Microsystems.
Following figure shows the working of the Java compiler:
Once the byte code is generated it can be run on any platform using Java Interpreter (JVM). It interprets byte code (.class file) and converts into machine specific binary code. Then JVM runs the binary code on the host machine.
How to use Java Compiler
When you run javac command on the command prompt, it shows the following output.C:\>javac
Usage: javac
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are
used
-classpath
annotation processors
-cp
annotation processors
-sourcepath
-bootclasspath
-extdirs
-endorseddirs
-proc:{none,only} Control whether annotation processing and/or
compilation is done.
-processor
Names of the annotation processors to run;
bypasses default discovery process
-processorpath
-d
-s
-implicit:{none,class} Specify whether or not to generate class files
for implicitly referenced files
-encoding
-source
release
-target
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J
C:\>
Above output shows the different options of javac tool.
Using java compiler to compile java file:
Following example shows how a Compiler works. It compiles the program and gives the Syntax error, if there is any. Like in this example, we haven't initialized 'a' and we are using it in the next statement as 'int c=a+b'. That is why its showing a syntax error. class A{
public static void main(String[] args){
int a;
int b=2;
int c=a+b;
System.out.println(c);
}
}
Output of program: C:\Program Files\Java\jdk1.6.0_01\bin>javac A.java
A.java:6: variable a might not have been initialized
int c=a+b;
^
1 error
C:\Program Files\Java\jdk1.6.0_01\bin>
Now, lets tweak this example. In this we have initialized 'a' as 'int a =2'. Hence, no syntax error has been detected.class A{
public static void main(String[] args) {
int a=2;
int b=2;
int c=a+b;
System.out.println(c);
}
}
Output of program:C:\Program Files\Java\jdk1.6.0_01\bin>javac A.java
C:\Program Files\Java\jdk1.6.0_01\bin>java A
4
Java Interpreter
We can run Java on most platforms provided a platform must has a Java interpreter.
We can run Java on most platforms provided a platform must has a Java interpreter. That is why Java applications are platform independent. Java interpreter translates the Java bytecode into the code that can be understood by the Operating System. Basically, A Java interpreter is a software that implements the Java virtual machine and runs Java applications. As the Java compiler compiles the source code into the Java bytecode, the same way the Java interpreter translates the Java bytecode into the code that can be understood by the Operating System.
When a Java interpreter is installed on any platform that means it is JVM (Java virtual machine) enabled platform. It (Java Interpreter) performs all of the activities of the Java run-time system. It loads Java class files and interprets the compiled byte-code. You would be glad to know that some web browsers like Netscape and the Internet Explorer are Java enabled. This means that these browsers contain Java interpreter. With the help of this Java interpreter we download the Applets from the Internet or an intranet to run within a web browser. The interpreter also serves as a specialized compiler in an implementation that supports dynamic or "just in time," compilation which turns Java byte-code into native machine instructions.
Throughout Java programming, we'll build both, the standalone Java programs and applets.
Sun's Java interpreter is called java. Lets learn how to start a standalone application with it. Load an initial class and specify it. Some options can also be specified to the interpreter, and any command-line arguments needed for the application as well:
% java [interpreter options] class name [program arguments]
The class should be specified as a fully qualified class name including the class package, if any.
Note : Moreover, we don't include the .class file extension. Here are a few examples:
% java animals.birds.BigBird
% java test
Once the class is loaded, java follows a C-like convention and searches for the class that contains a method called main(). If it finds an appropriate main() method, the interpreter starts the application by executing that method. From there, the application starts additional threads, reference other classes, and create its user interface.
Now, lets see how to go about an Applet. Although Java applet is a compiled Java code, the Java interpreter can't directly run them because they are used as part of a larger applications. For this we use Java Applet Viewer. It is a command line program to run Java applets. It is included in the SDK. It helps you to test an applet before you run it in a browser. We will learn more about it later.
Java debugger helps in finding and the fixing of bugs in Java language programs.
Java debugger helps in finding and the fixing of bugs in Java language programs. The Java debugger is denoted as jdb. It works like a command-line debugger for Java classes.
jdb session
The way to start the jdb session is to have jdb launch a new JVM (Java virtual machine) with the main class. Then the application of the main class is debugged by substituting the command jdb in command-line. For instance, if the main class of the application is TempClass, the following command is used to debug it:
% jdb TempClass
There is another way to use jdb i.e.to attach jdb to Java VM which is already running.
There are few options which are used to debug a VM with jdb. These are:option purpose
-Xdebug Enables debugging in the VM
-Xrunjdwp:transport=dt_socket,server=y,suspend=n Loads in-process debugging libraries and specifies the kind of connection to be made
The following command will run the TempClass application to which the jdb will connect afterwords.
% java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n TempClass
Now the jdb will be attached to the VM in this way: % jdb -attach 8000
You can go through the basic jdb commands that the Java debugger supports.
cont
Ihis command Continues the execution of the debugged application after a breakpoint, exception, or step.
run
Use run command to start the execution after starting jdb, and setting any necessary breakpoints, use run command to start the execution . When jdb launches the debugged application then only this command is available.
print
This command is used to display Java objects and primitive values. The actual value is printed for the variables or fields of primitive types. For objects, a short description is printed.
Few examples of print command are:
print TempClass.myStaticField
print myObj.myInstanceField
print myObj.myMethod()
dump
This command is similar to print command. For objects, it is used to print the current value of each field defined in the object including Static and instance fields.
The dump command supports the same set of expressions as the print command.
Exceptions
If any kind of exception occurs in the program, the VM will print an exception trace and exits. It will only print this exception when there isn't any catch statement in the throwing thread's call stack.
There is another command to be used to stop the debugged applications at other thrown exceptions. The command is catch command. For instance, "catch java.io.FileNotFoundException" or "catch mypackage.BigTroubleException.
Also the ignore command negates the effect of a previous catch command.
help, or ?
The command which helps in displaying the list of recognized commands is the help or ? command.
threads
This command list the threads that are currently running. The name and current status are printed for each thread. The index is also printed that can be used for other commands, for example:
4. (java.lang.Thread)0x1 main running
This example shows that the thread index is 4, the thread is an instance of java.lang.Thread, the thread name is "main", and it is currently running.
thread
This command selects a thread to be the current thread. Some jdb commands are based on the setting of the current thread. The thread index specifies the thread as explained in the threads command above.
where
The command where is used to dump the stack of the current thread. Whereas the command where all is used to dump the stack of all threads in the current thread group. And the where thread index command is used to dump the stack of the specified thread.
Breakpoints
The way to use Breakpoints is to set it in jdb at line numbers or at the first instruction of a method, for example:
stop at TempClass:14 (sets a breakpoint at the first instruction for line 14 of the source file containing TempClass)
stop in TempClass.
It is essential to specify the argument types whenever we overload any method in order to select the proper method for a breakpoint. For example, "TempClass.myMethod(int,java.lang.String)", or "TempClass.myMethod()".
We also use the clear command to remove breakpoints using a syntax as in "clear TempClass:20" and the cont command continues execution.
Command Line Options
Use jdb in place of the Java application launcher on the command line, it accepts most of the same options as the java command, which includes -D, -classpath, and -X
The Java Applet Viewer
An applet is a special type of application that's included as a part of an HTML page and can be stored in a web page and run within a web browser. The applet's code gets transferred to the system and then the Java Virtual Machine (JVM) of the browser executes that code and displays the output.. So for running the applet, the browser should be Java enabled. To create an applet, we need to define a class that inherits the Applet.
We generally use web browsers to run applets. Its not always mandatory to open a Web browser for running an applet. There is another way as well. The other way to run an applet is through Java applet viewer. This is a tool that acts as a test bed for Java applets. The working of Applet viewer is a bit different from a Web browser, though they are logically same. The Applet viewer runs on the HTML documentation, and uses embedded applet tags. The difference in using the applet viewer and the web browser to run the applet is that the applet viewer only deals with the applet code not the HTML cod i.e. it doesn't display HTML code. So we should test our program in applet viewer and web browser to confirm its working.
The applet viewer command connects to the documents or resources designated by urls. It displays each applet referenced by the documents in its own window.
The syntax for the applet viewer is:
appletviewer Options URL
Where the URL specifies the location of the applet program and the Options argument specifies how to run the Java applet. We can use only one option -debug that starts the applet viewer in the Java debugger. Using this option we can debug an applet.
The following program shows how to build an applet and the HTML file for it. Firstly create a class. Then start the applet using init method. After that enter a string as str = "This is my first applet". Use paint method to give the dimensions of the applet. Beneath that is the HTML file which shows how to give the body for applet.
Here is the Java File:import java.applet.*;
import java.awt.*;
public class Myapplet extends Applet{
String str;
public void init(){
str = "This is my first applet";
}
public void paint(Graphics g){
g.drawString(str, 50,50);
}
}
Here is the HTML File:
After building the program, run the applet and the applet viewer as shown below.C:\javac> javac Myapplet.java
C:\javac>appletviewer Myapplet.html
When we run the applet viewer it will display the window as shown below.
Sunday, November 8, 2009
Javadoc
Sun Microsystems has provided a computer software tool known as Javadoc. This tool is used to generate API documentation into HTML format from Java source code. It is interesting to know that Javadoc is the industry standard for documenting Java classes.
Javadoc is a program that is already included in JDK. We can use Javadoc to run over the source code to produce documentation of our classes in the HTML files . We have to tag our code with by using some comment formats to use javadoc tag on our source code. For instance Javadoc comments looks like this:
NOTE : To start a Javadoc comments use /** to start, and */ to end, and use tags such as @param, @return, and @exception in between to describe the workings of a method.
The format is given below to use the Javadoc comments:
/**
* Summary of the sentence.
* information about the
* program, class, method or variable
* then the comment, using as many lines
* as necessary.
*
* zero or more tags to specify any type
* of information, such as parameters and return
* values for a method
*/
Start with comment delimiter (/*) followed by another (*). The next line starts with an asterisk and write as many line as you want starting with an asterisk. The last line ends with the delimiter (*/). The first sentence to start with is a "summary sentence" showing the description of the program, class, method or variable. We can write few more lines if required, but not any blank lines. After writting the general description, a sequence of tags follows leaving a blank line.
Use different tags to display different situations. The example below shows a Javadoc comment without tags that describes the variable declared immediately below it:
/**
* The number of employees in a company. This variable must not be
* negative or greater than 200.
*/
public int numEmployees;
One interesting thing to know about Javadoc comments is that we can embed HTML tags to format the text. For example:
/**
* java declaration
*/
Lets have a look on Javadoc tags
| Tag | Description |
| @version | Shows the version number of a class or method. |
| @author | Shows the developer name |
| @return | Documents the return value. This tag should not be used for constructors or methods defined with a void return type. |
| @deprecated | Marks a method as deprecated. Some IDEs will issue a compilation warning if the method is called. |
| @see | Documents an association to another method or class. |
| @param | Defines a method parameter. Required for each parameter. |
| @throws | Documents an exception thrown by a method. A synonym for @exception introduced in Javadoc 1.2. |
| @since | Documents when a method was added to a class. |
| @exception | Documents an exception thrown by a method — also see @throws. |
| private | Displays all classes and members |
| use | It creates class and package usage pages |
| Windowtitle | It shows the window title of the document |
| Header | It includes for header text of the page |
| Footer | It includes footer text for the page |
| Bottom | It includes bottom text for the page |
| Package | Shows package classes and members |
| Protected | shows protected classes and members |
| Classpath | Helps to find user class files |
| noindex | doesn't provide the index |
| nohelp | doesn't provide the help link |
| notree | doesn't provide class hierarchy |
To document source code developers use certain commenting styles and Javadoc tags. A Java block comment starting with /** will begin a Javadoc comment block This comment block will be included in the HTML. Some tags are provided in the above table which begins with an "@" (at sign).
Javah - Header File Generator
In Java programming we need to implement some native methods. You must be wondering about what's native methods.
Firstly, The native methods are in pure C code, not C++. The function prototypes are in an object-oriented form of C which are being provided by javah , but they are still not object methods.
Secondly, We can call native methods applications only. However due to some security reasons, we cannot call applets from native methods.
Thirdly, native methods are platform-specific. This is the most important point to remember, you have to build a dynamically loadable library to link your java application with the native operating system (Windows OS, Machintosh, Linux, Unix ..). For each native platform your application targets, a dynamically loadable library is needed to be shipped.. That means any system-specific code has to be ported along with the java code.
On the other hand, native methods are the only way to use any system features not provided by the Java Virtual Machine.
To implement these methods Javah generates C header and source files that are used by C programs to reference an Object's instance variables from native source name. The name of the header file and the structure declared within it are derived from the name of the class.
By default javah creates a header file for each class listed on the command line and puts the files in the current directory. As stated above the name of the header file is derived from the name of the class. If any class inside the package is passed to javah, the package name gets prepended to both the header file name and the structure name.
Following are some options to use :
-o outputfile
This option concatenates the resulting header or source files for all the classes listed on the command line into output file.
-help
print help message for javah usage.
-d directory
This option sets the directory where javah saves the header files or the stub files. Only one of -d or -o may be used.
-classpath path
Specifies the path javah used to look up classes. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. Thus the general format for path is:
.;
For example:
.;C:\users\dac\classes;C:\tools\java\classes
-stubs
Causes javah to generate C declarations from the Java object file.
-verbose
Indicates verbose output and causes javah to print a message to stdout concerning the status of the generated files.
Thursday, November 5, 2009
Java Debugger
jdb session
The way to start the jdb session is to have jdb launch a new JVM (Java virtual machine) with the main class. Then the application of the main class is debugged by substituting the command jdb in command-line. For instance, if the main class of the application is TempClass, the following command is used to debug it:
% jdb TempClass
There is another way to use jdb i.e.to attach jdb to Java VM which is already running.
There are few options which are used to debug a VM with jdb. These are:option purpose
-Xdebug Enables debugging in the VM
-Xrunjdwp:transport=dt_socket,server=y,suspend=n Loads in-process debugging libraries and specifies the kind of connection to be made
The following command will run the TempClass application to which the jdb will connect afterwords.
% java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n TempClass
Now the jdb will be attached to the VM in this way: % jdb -attach 8000
You can go through the basic jdb commands that the Java debugger supports.
cont
Ihis command Continues the execution of the debugged application after a breakpoint, exception, or step.
run
Use run command to start the execution after starting jdb, and setting any necessary breakpoints, use run command to start the execution . When jdb launches the debugged application then only this command is available.
This command is used to display Java objects and primitive values. The actual value is printed for the variables or fields of primitive types. For objects, a short description is printed.
Few examples of print command are:
print TempClass.myStaticField
print myObj.myInstanceField
print myObj.myMethod()
dump
This command is similar to print command. For objects, it is used to print the current value of each field defined in the object including Static and instance fields.
The dump command supports the same set of expressions as the print command.
Exceptions
If any kind of exception occurs in the program, the VM will print an exception trace and exits. It will only print this exception when there isn't any catch statement in the throwing thread's call stack.
There is another command to be used to stop the debugged applications at other thrown exceptions. The command is catch command. For instance, "catch java.io.FileNotFoundException" or "catch mypackage.BigTroubleException.
Also the ignore command negates the effect of a previous catch command.
help, or ?
The command which helps in displaying the list of recognized commands is the help or ? command.
threads
This command list the threads that are currently running. The name and current status are printed for each thread. The index is also printed that can be used for other commands, for example:
4. (java.lang.Thread)0x1 main running
This example shows that the thread index is 4, the thread is an instance of java.lang.Thread, the thread name is "main", and it is currently running.
thread
This command selects a thread to be the current thread. Some jdb commands are based on the setting of the current thread. The thread index specifies the thread as explained in the threads command above.
where
The command where is used to dump the stack of the current thread. Whereas the command where all is used to dump the stack of all threads in the current thread group. And the where thread index command is used to dump the stack of the specified thread.
Breakpoints
The way to use Breakpoints is to set it in jdb at line numbers or at the first instruction of a method, for example:
stop at TempClass:14 (sets a breakpoint at the first instruction for line 14 of the source file containing TempClass)
stop in TempClass.
It is essential to specify the argument types whenever we overload any method in order to select the proper method for a breakpoint. For example, "TempClass.myMethod(int,java.lang.String)", or "TempClass.myMethod()".
We also use the clear command to remove breakpoints using a syntax as in "clear TempClass:20" and the cont command continues execution.
Command Line Options
Use jdb in place of the Java application launcher on the command line, it accepts most of the same options as the java command, which includes -D, -classpath, and -X
Wednesday, November 4, 2009
Java Interpreter
When a Java interpreter is installed on any platform that means it is JVM (Java virtual machine) enabled platform. It (Java Interpreter) performs all of the activities of the Java run-time system. It loads Java class files and interprets the compiled byte-code. You would be glad to know that some web browsers like Netscape and the Internet Explorer are Java enabled. This means that these browsers contain Java interpreter. With the help of this Java interpreter we download the Applets from the Internet or an intranet to run within a web browser. The interpreter also serves as a specialized compiler in an implementation that supports dynamic or "just in time," compilation which turns Java byte-code into native machine instructions.
Throughout Java programming, we'll build both, the standalone Java programs and applets.
Sun's Java interpreter is called java. Lets learn how to start a standalone application with it. Load an initial class and specify it. Some options can also be specified to the interpreter, and any command-line arguments needed for the application as well:
% java [interpreter options] class name [program arguments]
The class should be specified as a fully qualified class name including the class package, if any.
Note : Moreover, we don't include the .class file extension. Here are a few examples:
% java animals.birds.BigBird
% java test
Once the class is loaded, java follows a C-like convention and searches for the class that contains a method called main(). If it finds an appropriate main() method, the interpreter starts the application by executing that method. From there, the application starts additional threads, reference other classes, and create its user interface.
Now, lets see how to go about an Applet. Although Java applet is a compiled Java code, the Java interpreter can't directly run them because they are used as part of a larger applications. For this we use Java Applet Viewer. It is a command line program to run Java applets. It is included in the SDK. It helps you to test an applet before you run it in a browser. We will learn more about it later.
Sunday, November 1, 2009
Java Enabled browser
Internet Explorer
Netscape
HotJava
Firefox 1.0.4
Mozilla 1.6
Internet Explorer: It is the most widely used Browser. Internet Explorer abbreviates as IE . Windows Internet Explorer is developed by Microsoft in 1995 and it was included in series of Microsoft Windows 95 operating system. Advanced version of IE are supported on different operating systems such as Internet Explorer for UNIX, Internet Explorer for Mac. Internet Explorer 7.0 is the recent version available for free update for Windows XP with Service Pack 2 and Windows Server 2003 with Service Pack 1, it is also included with Windows Vista.
Netscape: Netscape Web browser version 8.0(as the first version) was first time released in 30 November 2004 . It was developed by Mercusial Communication. Then version 8.0.1 came in the market with a minor enhancement. Various versions of the Netscape were based on different technology like version 8 was based on Mozila Firefox whereas version 1 to 4 was based on Netscape Navigator and Netscape Communicator whereas Netscape 6 and 7 are based on Mozilla Application Suite.
HotJava: It is the modular version of the web browser developed by the Sun Microsystems to support the applets. Now it is not currently used. It was just like the clone of the internet browser Mosaic. This browser had three version HotJava Browser 3.0, HotJava Browser 1.1.5 and HotJava Browser 1.1.2 .
Techniques to check whether a browser is java enabled or not.
Most of the browsers today are java enabled. There are various ways to check whether the browser being used is java enabled or not. However we manually check its support for java. The following procedure shows how to check, whether the browser is java enabled or not.
Firefox 1.0.4: Just , Go through by putting the following entry in the address bar.
about : java plugins
If java is installed then it will show the multiple entries under the lable "Java Plug-in". It will also provide the information about the version and installation.
We can also check manually by going through the following steps in the tool bar.
Click on Tools -> Options -> Web features (or Content ).
If the check box is checked, the browser is java enabled.
Mozilla 1.6: Mozilla also have the two ways to check manually , whether it is java enabled or not.
Go to click on Help-> About Plug-ins.
If it shows nothing then java is not installed. If java is installed then it shows various plug-in entries according to the version.
The second method is that, Click on Tools -> Web Development. If it shows the Java Console option then java is installed otherwise it shows nothing.
Saturday, October 31, 2009
Java Compiler
The output from a Java compiler comes in the form of Java class files (with .class extension). The java source code contained in files end with the .java extension. The file name must be the same as the class name, as classname.java. When the javac compiles the source file defined in a .java files, it generates bytecode for the java source file and saves in a class file with a .class extension.
The most commonly used Java compiler is javac, included in JDK from Sun Microsystems.
Following figure shows the working of the Java compiler:
Once the byte code is generated it can be run on any platform using Java Interpreter (JVM). It interprets byte code (.class file) and converts into machine specific binary code. Then JVM runs the binary code on the host machine.
How to use Java Compiler
When you run javac command on the command prompt, it shows the following output.
C:\>javac
Usage: javac
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath
-cp
-sourcepath
-bootclasspath
-extdirs
-endorseddirs
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor
-processorpath
-d
-s
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding
-source
-target
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J
C:\>
Above output shows the different options of javac tool.
Using java compiler to compile java file:Following example shows how a Compiler works. It compiles the program and gives the Syntax error, if there is any. Like in this example, we haven't initialized 'a' and we are using it in the next statement as 'int c=a+b'. That is why its showing a syntax error.
class A{ public static void main(String[] args){ int a; int b=2; int c=a+b; System.out.println(c); } }
Output of program:
C:\Program Files\Java\jdk1.6.0_01\bin>javac A.javaA.java:6: variable a might not have been initializedint c=a+b;^1 errorC:\Program Files\Java\jdk1.6.0_01\bin>
Now, lets tweak this example. In this we have initialized 'a' as 'int a =2'. Hence, no syntax error has been detected.
class A{ public static void main(String[] args) { int a=2; int b=2; int c=a+b; System.out.println(c); } }
Output of program:
C:\Program Files\Java\jdk1.6.0_01\bin>javac A.java
basic features of java
The concept of Write-once-run-anywhere (known as the Platform independent) is one of the important key feature of java language that makes java as the most powerful language. Not even a single language is idle to this feature but java is more closer to this feature. The programs written on one platform can run on any platform provided the platform must have the JVM.
Simple:
There are various features that makes the java as a simple language. Programs are easy to write and debug because java does not use the pointers explicitly. It is much harder to write the java programs that can crash the system but we can not say about the other programming languages. Java provides the bug free system due to the strong memory management. It also has the automatic memory allocation and deallocation system.
Object Oriented:
To be an Object Oriented language, any language must follow at least the four characteristics.
Inheritance : It is the process of creating the new classes and using the behavior of the existing classes by extending them just to reuse the existing code and adding the additional features as needed.
Encapsulation: : It is the mechanism of combining the information and providing the abstraction.
Polymorphism: : As the name suggest one name multiple form, Polymorphism is the way of providing the different functionality by the functions having the same name based on the signatures of the methods.
Dynamic binding : Sometimes we don't have the knowledge of objects about their specific types while writing our code. It is the way of providing the maximum functionality to a program about the specific type at runtime.
As the languages like Objective C, C++ fulfills the above four characteristics yet they are not fully object oriented languages because they are structured as well as object oriented languages. But in case of java, it is a fully Object Oriented language because object is at the outer most level of data structure in java. No stand alone methods, constants, and variables are there in java. Everything in java is object even the primitive data types can also be converted into object by using the wrapper class.
Robust:
Java has the strong memory allocation and automatic garbage collection mechanism. It provides the powerful exception handling and type checking mechanism as compare to other programming languages. Compiler checks the program whether there any error and interpreter checks any run time error and makes the system secure from crash. All of the above features makes the java language robust.
Distributed:
The widely used protocols like HTTP and FTP are developed in java. Internet programmers can call functions on these protocols and can get access the files from any remote machine on the internet rather than writing codes on their local system.
Portable:
The feature Write-once-run-anywhere makes the java language portable provided that the system must have interpreter for the JVM. Java also have the standard data size irrespective of operating system or the processor. These features makes the java as a portable language.
DynamicWhile executing the java program the user can get the required files dynamically from a local drive or from a computer thousands of miles away from the user just by connecting with the Internet.
Secure :
Java does not use memory pointers explicitly. All the programs in java are run under an area known as the sand box. Security manager determines the accessibility options of a class like reading and writing a file to the local disk. Java uses the public key encryption system to allow the java applications to transmit over the internet in the secure encrypted form. The bytecode Verifier checks the classes after loading.
Performance:
Java uses native code usage, and lightweight process called threads. In the beginning interpretation of bytecode resulted the performance slow but the advance version of JVM uses the adaptive and just in time compilation technique that improves the performance.
MultithreadedAs we all know several features of Java like Secure, Robust, Portable, dynamic etc; you will be more delighted to know another feature of Java which is Multithreaded.Java is also a Multithreaded programming language. Multithreading means a single program having different threads executing independently at the same time. Multiple threads execute instructions according to the program code in a process or a program. Multithreading works the similar way as multiple processes run on one computer. Multithreading programming is a very interesting concept in Java. In multithreaded programs not even a single thread disturbs the execution of other thread. Threads are obtained from the pool of available ready to run threads and they run on the system CPUs. This is how Multithreading works in Java which you will soon come to know in details in later chapters.
Interpreted:
We all know that Java is an interpreted language as well. With an interpreted language such as Java, programs run directly from the source code. The interpreter program reads the source code and translates it on the fly into computations. Thus, Java as an interpreted language depends on an interpreter program.The versatility of being platform independent makes Java to outshine from other languages. The source code to be written and distributed is platform independent. Another advantage of Java as an interpreted language is its error debugging quality. Due to this any error occurring in the program gets traced. This is how it is different to work with Java.
Architecture Ne utral:
The term architectural neutral seems to be weird, but yes Java is an architectural neutral language as well. The growing popularity of networks makes developers think distributed. In the world network it is essential that the applications must be able to migrate easily to different computer system . Not only to computer systems but to a wide variety of hardware architecture and Operating system architectures as well. The Java compiler does this by generating byte code instructions, to be easily interpreted on any machine and to be easily translated into native machine code on the fly. The compiler generates an architecture-neutral object file format to enable a Java application to execute anywhere on the network and then the compiled code is executed on many processors, given the presence of the Java runtime system. Hence Java was designed to support applications on network. This feature of Java has thrived the programming language.