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
HARDWARE AND SOFTWARE REQUIREMENTS
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
Variables
Declaring Variables
Rules on Variable Names
Variable Types
Primitive Data Types
Constants
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
Conversions
Casts
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
Object
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
Arrays
Java arrays
Object arrays
Strings
String Concatenation
Inheritance
Introduction
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
Interfaces and Classes
Implementing and Using Interfaces
Implementing Multiple Interfaces
Creating and Extending Interfaces
Methods Inside Interfaces
Extending Interfaces
Package
Introduction
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
Multithreading
Introduction
Thread
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
Introduction
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
Applet
Introduction
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
Introduction
Streams
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

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);
fd.setVisible(true);
 
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.*;
import java.io.*;

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);
myWindow.setVisible(true);
}

// Class constructor.

public App(String title)
{

// Usual boilerplate for startup and shutdown.

super(title);
//addWindowListener();
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
);

// Create the frame's menu bar.

mainBar = new MenuBar();
setMenuBar(mainBar);
fileMenu = new Menu("File");
find = new MenuItem("Find");
find.addActionListener(this);
fileMenu.add(find);
fileMenu.addSeparator();
exit = new MenuItem("Exit");
exit.addActionListener(this);
fileMenu.add(exit);
mainBar.add(fileMenu);

// 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));
fileInfo.setEditable(false);
add(fileInfo);
}

// 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);
fd.setVisible(true);

// 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());
fileInfo.setCaretPosition(0);
}

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

if (e.getSource().equals(exit)) {
dispose();
System.exit(0);
}
}
}

Output: