Java is the key to developing platform-independant programs. Therefore, it is the ideal option when diverse hardware platforms and OSs need to be supported.
Java was designed at Sun Microsystems, Inc in 1991 and was initially called Oak. It was renamed Java in 1995.
Java was designed to be compatible with different platforms used by computers connected to the Internet. The compiled code of a Java Program can be executed on different platforms without any changes. This is possible because of bytecode that is generated during the compilation of source code. Bytecode is a highly optimized set of instructions that is executed by the Java run-time system called Java Virtual Machine (JVM). The JVM acts as an interpreter for bytecode.
Java Source Files
Fig 1.1 – Java Source Files
A Java source file contains three sections. The first setion is the package section that contains a single line of code that specifies the package to while the source file belongs. The package statement may be omitted, in which case the default package will be assumed.
The second section is the import section. This section includes import statements that tell the compiler which packages to include. The import section may be omitted. The compiler will automatically import the java.lang.package that includes the most commonly used Java classes and interfaces.
The third section includes the class or interface definition. This is where all of the remaining Java code for the source file will be placed. There can only be one top-level public class or a single public interface defined in a source file.
A Java source file must be named with the same name as the top-level class or interface, if that top level class or interface is defined as public. The extension .java is appended to the name to form the source file name. In our example, it would be HelloWorld.java
Java Compiler ignores extra white spaces. So you can use..hell lots of space to make your code more readable.
Comments are used to annotate the code. The compiler will strip the comments from the code during compilation. It is a good idea to use comments to explain code that may be confusing. Comments are also used for producing Javadoc documentation such as the Java API reference.
There are two types of comments that can be coded into a Java source file.
Type 1: Multiple line comments
/* blah blah blag */
Type 2: End-of-line comment