Effort has been made to ensure that the information in this book is accurate and complete. However, the author and the publisher do not warrant the accuracy of the information, text and graphics contained within the book due to the rapidly changing nature of science, research, known and unknown facts. The author and the publisher do not hold any responsibility for errors, omissions or contrary interpretation of the subject matter herein. This book is presented solely for motivational and informational purposes only.
All Rights Reserved.
No part of this publication or the information in it may be quoted from or reproduced in any form by means such as printing, scanning, photocopying or otherwise without prior written permission of the copyright holder.
This book contains proven steps and strategies on how to create programs using the Java programming language. It also contains details about the topics that every beginner should be aware of.
This book also contains useful information regarding the features you can find in Java as well as why Java is a good program to use. You will also find sample programs that you can use as guidelines when writing your own programs.
Chapter 1: Introduction to Java
Java is an object-oriented programming language created in 1991 at Sun Microsystems, which is renowned for their high class Unix workstations. The programming language was modeled after C++ and was initially designed to be simple, small, and compatible with a variety of operating systems and platforms.
Java is usually placed in the same category as HotJava, which is a browser from Sun, much like Mosaic or Netscape. Perhaps the most obvious difference of HotJava from other browsers is its ability to play and download applets on your system. These applets show up in your Web page just like any other images, except that they are interactive and dynamic. You can use them to make animations and figures, as well as other things that can respond to your input.
Even though HotJava was the first ever World Wide Web browser to host applets, the support for Java is quickly being made available in many other browsers. For instance, Netscape 2.0 supports Java applets.
If you want to develop an applet, you have to write programs using Java and then compile it. Refer to the applet in the HTML Web pages. Place your Java and HTML files on the website just as you would with any other image or HTML files. The moment someone else views your Web page using a HotJava browser, the browser automatically downloads the embedded applet to their local system and runs it. The reader can then view and interact with the applet in any way they want.
Keep in mind that there are so many things you can do with Java aside from developing applets. The programming language was written in a way to help you solve problems and accomplish tasks just as you would using the C language or C++. In fact, HotJava, including its display, networking, and interface, is created using Java.
A Brief History of Java
The innovation of computer languages is primarily influenced by two factors: programming improvements and computing environment changes. With Java, you can still find the elements found in C and C++, but with more features and refinements. The programming language boasts of features that restructure programming for better architecture.
Java was the brainchild of James Gosling, Chris Wrath, Patrick Naughton, Mike Sheridan, and Ed Frank. Developed at Sun Microsystems, it was originally called Oak. Later in 1995, its name was changed to Java. The original purpose of Java was not to be connected to the Internet, but rather to serve as a platform-independent language that can be used to develop software for various electronic devices, including remote controls and microwave ovens. Different kinds of CPUs were used as controllers. However, during that time, computer languages were mostly designed to be compiled for a particular target.
Even though compiling C++ programs for any type of CPU was possible, doing so requires a full compiler for that particular CPU. Because compilers take a lot of time to create and are costly, Gosling and his team searched for a way to have a more portable language that can produce code that is compatible with different CPUs in different environments. The resulting output was the Java programming language.
Principles and Platform
Java has five main objectives: It has to be simple, familiar, and object-oriented; it has to be secured and robust; it has to be portable and architecture-neutral; it has to execute with high performance; and it has to be dynamic, interpreted, and threaded.
Portability is one of Javas design goals. This means that programs created for its platform have to run similarly on the combinations of operating system and hardware with enough runtime support. This can be achieved when the programming languages code is compiled to an intermediate representation known as Java bytecode, rather than an architecture-specific machine code.
Take note that the bytecode instructions of Java are analogous to the machine code. Nevertheless, they are meant to be executed by a special type of virtual machine (VM) made especially for the host hardware. A Java Runtime Environment (JRE) is typically used by end users. It is either installed in a Web browser for applets or their machines for standalone applications.
Features, including graphics, networking, and threading are accessed through the standard libraries. Porting becomes simpler with universal bytecodes. Then again, the bytecode interpretation overhead into the machine instructions tends to make the interpreted programs run slower as compared to native executables. The just-in-time (JIT) compilers, on the other hand, have been long introduced to compile bytecodes into machine code. Keep in mind that Java is platform-independent.
Implementations and Performance
Currently, the Java platform is owned by Oracle Corporation. Their implementation is available on Microsoft Windows, including XP. It is also available for Solaris, Mac OS X, and Linux. Such implementation is actually the de facto standard since Java does not have a formal standardization that is recognized by ANSI, ISO/IEC, Ecma International, or any other third-party standards organization.
This implementation is also packaged into two distributions: the Java Development Kit, which includes a debugger, Jar, Javadoc, and Java compiler among other development tools, and the Java Runtime Environment, which contains the platform parts necessary for running programs. Another Java implementation licensed under GNU GPL is the OpenJDK. Its implementation began when Java source code is released under GPL. Since Java SE 7, it has been the official reference implementation.
Javas goal is to make every implementation compatible with one another. The trademark license of Sun for using Java actually insists for all implementations to be compatible. Because of this, a legal dispute has resulted when Sun claimed that the implementation of Microsoft did not support JNI or RMI. Sun sued Microsoft and won a $20 million settlement in 2001. Microsoft does not ship Windows with Java anymore.
With regard to performance, all programs created in Java are known for being slow and needing a huge memory. However, ever since just-in-time compilation was introduced in 1997 and 1998, the execution speed of Java has significantly improved. More language features that support better code analysis have been added. These include inner classes, optional assertions, and StringBuilder. Optimizations like HotSpot have been added as well.
The Purpose of Java