Saturday, February 23, 2013

Core Java Tutorial




What is Java technology and why do I need it?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. It is the underlying technology that powers state-of-the-art programs including utilities, games, and business applications. Java runs on more than 850 million personal computers worldwide, and on billions of devices worldwide, including mobile and TV devices.
Why do I need Java?
There are lots of applications and websites that won't work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!
Is Java free to download?
Yes, Java is free to download. Get the latest version at http://java.com.
If you are building an embedded or consumer device and would like to include Java, please contact Oracle for more information on including Java in your device.
 
Why should I upgrade to the latest Java version?
The latest Java version contains important enhancements to improve performance, stability and security of the Java applications that run on your machine. Installing this free update will ensure that your Java applications continue to run safely and efficiently.
TECHNICAL INFORMATION 
Originally called OAK, it was renamed as the Java programming language in 1995.
What will I get when I download Java software?
The Java Runtime Environment (JRE) is what you get when you download Java software. The JRE consists of the Java Virtual Machine (JVM), Java platform core classes, and supporting Java platform libraries. The JRE is the runtime portion of Java software, which is all you need to run it in your Web browser. When you download Java software, you only get what you need - no spyware, and no viruses.
What is Java Plug-in software?
The Java Plug-in software is a component of the Java Runtime Environment (JRE). The JRE allows applets written in the Java programming language to run inside various browsers. The Java Plug-in software is not a standalone program and cannot be installed separately.
I've heard the terms Java Virtual Machine and JVM. Is this Java software?
The Java Virtual Machine is only one aspect of Java software that is involved in web interaction. The Java Virtual Machine is built right into your Java software download, and helps run Java applications.
What are the Java features?

List the basic features that make Java a powerful and popular programming language:
Platform Independence: The Write-Once-Run-Anywhere ideal has not been achieved (tuning for different platforms usually required), but closer than with other languages.
Object Oriented: Object oriented throughout - no coding outside of class definitions, including main().An extensive class library available in the core language packages.
Compiler/Interpreter Combo: Code is compiled to bytecodes that are interpreted by a Java virtual machines (JVM).This provides portability to any machine for which a virtual machine has been written.The two steps of compilation and interpretation allow for extensive code checking and improved security.
Robust: Exception handling built-in, strong type checking (that is, all data must be declared an explicit type), local variables must be initialized. 
Several dangerous features of C & C++ eliminated:
No memory pointers
No pre-processor
Array index limit checking
Automatic Memory Management: Automatic garbage collection - memory management handled by JVM.
Security:  No memory pointers
Programs runs inside the virtual machine sandbox.
Array index limit checking
Code pathologies reduced by
bytecode verifier - checks classes after loading
class loader - confines objects to unique namespaces. Prevents loading a hacked "java.lang.SecurityManager" class, for example.
security manager - determines what resources a class can access such as reading and writing to the local disk.
Dynamic Binding: The linking of data and methods to where they are located, is done at run-time.
New classes can be loaded while a program is running. Linking is done on the fly.
Even if libraries are recompiled, there is no need to recompile code that uses classes in those libraries.

This differs from C++, which uses static binding. This can result in fragile classes for cases where linked code is changed and memory pointers then point to the wrong addresses.
Good Performance: Interpretation of bytecodes slowed performance in early versions, but advanced virtual machines with adaptive and just-in-time compilation and other techniques now typically provide performance up to 50% to 100% the speed of C++ programs. 
Threading: Lightweight processes, called threads, can easily be spun off to perform multiprocessing.
Can take advantage of multiprocessors where available
Great for multimedia displays.
Built-in Networking: Java was designed with networking in mind and comes with many classes to develop sophisticated Internet communications.


Core Java FAQ


Question: What is the difference between the various editions?

Answer:
First (JDK 1.0) edition:
Objects. Inheritance. Interfaces. Graphics. AWT. Applets. Data Structures. Streams. Multithreading. Network programming.
Second (JDK 1.02) edition:
Additional topics: Object streams. JDBC. RMI. Native methods
Third (JDK 1.1) Edition:
Split into a two volume set. Additional topics in volume 1: Inner classes. Events. Printing. JAR files. In volume 2: Security. Signed applets. Advanced AWT. JavaBeans
Fourth (Java SE 1.2) Edition:
Moved streams to volume 1. Additional topics in volume 1: Swing. Java Plug-In. All examples have been rewritten to use Swing. Additional topics in volume 2: 2D Graphics. Advanced Swing components. Container classes
Fifth (Java SE 1.3/1.4) Edition:
Rewrite of first six chapters. Proxies. Additional debugging tips. Moved some Swing components from volume 2. Code with line numbers. Figures more UML compliant. More Unix/Linux coverage. Removed corejava package.
Sixth (Java SE 1.4) Edition:
Added coverage of 1.4 features: Logging, regular expressions, NIO file channels, preferences API, Java Web Start,  formatted text fields,  new focus architecture, more events (mouse wheel etc.), new frame functionality, spinners, spring layouts, assertions. Numerous bug fixes and updates throughout all chapters.
Seventh (Java SE 5.0) Edition
Added coverage of 5.0 features: Generic classes, autoboxing, varargs, enumerated types, annotations, java.util.concurrent. Numerous bug fixes and updates throughout all chapters.
8th (Java SE 6) Edition
Coverage of Java SE 6 features. Collections and multithreading moved to volume 1. New material on Swing and AWT enhancements, compiler and scripting APIs. Numerous bug fixes and updates throughout all chapters.
9th (Java 7) Edition
Coverage of Java 7. Resource management. Diamond operator. Other miscellaneous Java 7 features. Numerous bug fixes and updates throughout all chapters.

Question: I have the 9th/8th/7th edition of Core Java. Why do most of the sample programs have compiler errors?

Answer: Make sure that the language level of your IDE is set to Java 7/6/5. Some IDEs which we won't name (such as Eclipse) tend to be a version or two behind.

Question: I own an older version of the book. Can I upgrade to a newer version at a reduced price? Do you have the new chapters available for free?

Answer: Unfortunately no. There are numerous revisions in all chapters, so it isn't just a matter of giving out the new chapters for free. We looked at offering a rebate if you turn in your old copy to the bookstore, but the bookstores balked at that.

Question: In chapter 2 of volume 1, you write: “Open a shell or terminal window”. I use Windows/Macintosh, and I've never done that. Now what?

Answer: Under Windows, select the MS-DOS or Command Prompt application from the start menu. There are many tutorials on the net—you may want to search Google to find one that you like.
On Mac OS X, run the Terminal application. 

Question: Why doesn't any of the code compile? Or, why does some of the code compile and some of it gives error messages about missing classes?

Answer: It is the path and/or class path.
Do the following. First, deactivate any Java installation that you may have. That is, temporarily remove the JDK you downloaded yesterday, the beta test of the nifty new environment that your brother in law gave you, etc. Install the JDK from the Oracle web site. Download the Core Java files from this web site. Set the path and remove any class path settings. Reboot. Double-check that the class path is either unset or contains the current directory (.). Keep trying. This stuff always works out eventually.
I have had to deal with hundreds of users who swore that they did everything right, except they sheepishly admitted a couple of emails later that they didn't pay attention to capitalization, thought that there was no essential difference between a ; and a :, thought it was ok to add a few spaces to "improve" on the looks of the class path, didn't want to go through the trouble of deactivating their old version of Java, or whatever. Please: If the Java interpreter can't find some or all of the classes, check your path and class path, and don't report it as an error. Thanks!

Question: Why I does my code compile but not run?

Answer: This can happen if you have a class path but you forget to add the current directory(.) into it. This is because system classes (the ones in rt.jar) are always part of the class path. The current directory is also automatically searched if no explicit class path is provided. However, if an explicit class path is provided, the current directory is automatically searched by javac but not by java.

Question: Why don't the applets work in my browser?

Answer: You need to install and activate the Java Plug-in to run Java applets in your browser. Admittedly, this installation has never been easy and has been getting harder because browser makers have mistrusted Java, sometimes for good reason when Oracle dragged its feet on closing security loopholes. If you care about applets, be persistent. The installer for the JDK is supposed to install the plug-in for the default browser, but you may still need to activate Java in the browser.

Question: The RMI examples don't work, even though I did everything exactly like in the book.

Answer: The RMI setup is complex and it is very easy to get some detail wrong. Also, remember to always erase unneeded class files and to restart the naming service whenever you make a change in the configuration. Keep trying--it'll work eventually.