Core Java

Java Evolution
Java History
Java Features
Benefits of Java over C and C++
How Java works
The Java Programming Language
The Java Platform
Java Development Kit
Disadvantages of Java
Overview of Java Language
Developing the Java Application
Comments in Java Code
Defining a Class
The main Method
Using Classes and Objects
Java Program Structure
Java Tokens
Constant, Variable and Datatype
Declaring Variables
Rules on Variable Names
Variable Types
Primitive Data Types
Operator and Expression
Expressions and Operators
Operator Precedence
Integer Operators
Relational Integer Operators
Floating point Operators
Relational Floating-Point Operators
String Operator
Assignment Operators
Arithmetic assignment operations
Boolean expressions and operations
Logical operators
Bitwise operations
The complement operator
Decision making, Branching and Looping
Flow control with if and else
The if statement
The else statement
Switch statements
for, while, and do-while statements
The for statement
The while statement
The do-while statement
Using break and continue
The break statement
The continue statement
Class Object and Method
Introduction to Classes
The Benefit of Classes
Defining Classes
Class using constructor
Declaring an Object
Instantiating an Object
Initializing an Object
Referencing an Object's Variables
Calling an Object's Methods
Creating a Class
The Class Declaration
The Class Body
Constructors for Classes
Implementing Methods
The Method Body
A Method's Name
Example of the Math class
The Applet Package
Array and String
Java arrays
Object arrays
String Concatenation
Creating Subclasses
Member Variables In Subclass Inherit?
Hiding Member Variables
Methods In Subclass Inherit?
Overriding Methods
Methods a Subclass Cannot Override
Methods a Subclass Must Override
The Benefits of Inheritance
Interfaces and Classes
Implementing and Using Interfaces
Implementing Multiple Interfaces
Creating and Extending Interfaces
Methods Inside Interfaces
Extending Interfaces
Declaring Packages
Importing Packages
Creating Our Own Packages
The Java Language Package
The Java I/O Package
The Java Utility Package
The Java Networking Package
The Applet Package
The Abstract Window Toolkit Packages
Thread Attributes
Thread State
Thread Group
Methods that Operate on the Group
Access Restriction Methods
The notifyAll() and wait() Methods
Frequently used Method
Exception Handling
What Is an Exception?
If Exceptions than?
The exception handling technique
Some Terminology
Throw an Exception
Throw, try, and catch Blocks
Multiple catch Blocks
The finally Clause
The Throwable Class
Types of Exceptions
Different List of Exception
Built-In Exceptions
How Applets and Applications Are Different
Limitation of Applet
The Applet class
Major Applet Activities
The life cycle of a Web page applet
Including an Applet on a Web Page
Essential HTML to launch an applet and pass it parameters
Launching an applet in an HTML document
A sample applet that receives a parameter
Posting a Web page that launches a custom applet
Managing Input/Output Files in Java
Input Streams
The Abstract Class InputStream
The File class
The FileDialog class
Low-level and high-level stream classes
The FileOutputStream class
The FileInputStream class
The DataOutputStream class
The DataInputStream class
The ObjectOutputStream class
The ObjectInputStream class
Examples of Core Java
A simple program printing 'Hello'.
Writing the first ten multiples of any number using for loop.
To check whether a number is a PERFECT NUMBER or not.
A simple program using accumulator.
Calculate the square of the numbers from 1 to 10


Thread Attributes

Java threads are implemented by the Thread class, which is part of the java.lang package. The Thread class implements a system independent definition of Java threads. But under the hood, the actual implementation of concurrent operation is provided by a system-specific implementation.
The life of a thread
In Java, all threads are objects that are constructed, pass between several "states", and die in keeping with the following diagram:


1. When initially constructed, the thread is New
2. When all resources the thread requires are available, it enters the Ready state. The thread is ready to use the CPU.
1. When selected by the JVM for processing, the thread enters the Running state. This is the state that all threads aspire to but only one is ever running at an instant in time.
2. When the thread requests to sleep for an interval of time, must wait for a resource (such as completion of I/O), or is suspended by the JVM, it becomes Blocked. It gives up the CPU and will return to the Ready state when the block is removed.
3. When the thread completes its processing or is killed by the JVM, it enters the Dead state. The thread object will still exist, but will no longer be allowed to use the CPU.
Thread Body
All of the action takes place in the thread's body--the thread's run() method. We can provide the body to a Thread in one of two ways:
a. by subclassing the Thread class and overriding its run() method,
b. by creating a Thread with a Runnable object as its target.
Creating the Thread
The application in which an applet is running calls the applet's start() method when the user visits the applet's page.
The Clock applet creates a Thread, clockThread, in its start() method and starts the thread.
public void start()
if (clockThread == null)
clockThread = new Thread(this, "Clock");
First, the start() method checks to see if clockThread is null. If clockThread is null, then the applet has just been loaded or has been previously stopped and a new thread must be created.
Otherwise, the applet is already running. The applet creates a new thread with this invocation:
clockThread = new Thread(this, "Clock");
Here--the Clock applet--is the first argument to the thread constructor. The first argument to this Thread constructor must implement the Runnable interface and becomes the thread's target. When constructed in this way, the clock thread gets its run() method from its target Runnable object--in this case, the Clock applet.
The second argument is just a name for the thread.
After a thread has been created and initialized, the runtime system calls its run() method. The code in the run() method implements the behavior for which the thread was created.
Stopping the Thread
When we leave the page that displays the Clock applet, the application in which the applet is running calls the applet's stop() method.
The Clock's stop() method sets the clockThread to null.
This tells the main loop in the run() method to terminate eventually resulting in the thread stopping and being garbage collected.
public void stop()
clockThread = null;
we can use clockThread.stop() instead, which would immediately stop the clock thread.
However, the Thread class's stop() method has a sudden effect, which means that the run() method might be in the middle of some critical operation when the thread is stopped.
For more complex run() methods, using Thread's stop() method might leave the program in an inconsistent or awkward state. For this reason, it's best to avoid using the Thread class's stop() method when possible.
If we revisit the page, the start() method is called again, and the clock starts up again with a new thread.
The Run Method
And finally the Clock's run() method implements the heart of the Clock applet and looks like this:
public void run()

// loop terminates when clockThread is set to null in stop()
while (Thread.currentThread() == clockThread)


catch (InterruptedException e)