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

Managing Input/Output Files in Java

The FileDialog class

• Is part of the java.awt package. You will need an import statement to access the class with its abbreviated name.
• Has many ancestor classes as indicated by the following diagram:


• Can be instantiated to display a modal dialog window from which the user can select a file. Because it is a modal dialog, once an application makes a FileDialog object visible, it cannot continue until the user has chosen a file.
• Has two static fields that indicate whether the file will be used for input or output.
They are


• Has several constructors. The most frequently used is demonstrated by
FileDialog fd = new FileDialog(this, "Choose file", FileDialog.LOAD);
which constructs and displays a file dialog window for the current (this) object. The title of the file dialog window will be "Choose file", and the chosen file will be used for input.
• Has a number of methods. The most frequently used are as follows:


                               Consult the Java API documentation for more details.
Sample program
The following menu-driven, Windows program uses the File and FileDialog classes to display a variety of information about a file selected by the user:
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class App extends Frame implements ActionListener

// Object references.

MenuBar mainBar;
Menu fileMenu;
MenuItem find;
MenuItem exit;
TextArea fileInfo;

// Processing starts here.

public static void main(String[] args)
App myWindow = new App("File Analyzer");
myWindow.setSize(500, 200);

// Class constructor.

public App(String title)

// Usual boilerplate for startup and shutdown.

new WindowAdapter()
public void windowClosing(WindowEvent e)

// Create the frame's menu bar.

mainBar = new MenuBar();
fileMenu = new Menu("File");
find = new MenuItem("Find");
exit = new MenuItem("Exit");

// Create the file information text area.

fileInfo = new TextArea(" Use the menu bar to find a file...",
10, 40, TextArea.SCROLLBARS_NONE);
fileInfo.setFont(new Font("Monospaced", Font.PLAIN, 12));

// Required method of the ActionListener interface.

public void actionPerformed(ActionEvent e)

// If the user selected "File" | "Find" from the menu, let them
// choose a file to be analyzed. Then, display its information.

if (e.getSource().equals(find))

// Create and display a modal file dialog box through which the
// user can choose the file they want to analyze.

FileDialog fd = new FileDialog(this, "Choose file", FileDialog.LOAD);

// Construct a File object for the file the user selected.

File f = new File(fd.getDirectory() + File.separator + fd.getFile());

// Display information about the file the user selected.

fileInfo.setText(" FILE INFORMATION ");
fileInfo.append(" Path: " + f.getPath());
fileInfo.append(" Directory: " + fd.getDirectory());
fileInfo.append(" File: " + f.getName());
fileInfo.append(" Last modified: " +
new Date(f.lastModified()).toString());
fileInfo.append(" Byte length: " + f.length());

// If the user selected "File" | "Exit" from the menu, terminate
// the application.

if (e.getSource().equals(exit)) {