Sunday 23 March 2014

Network operating system in java (NOS)

AIM: Simulating Network Operating System (NOS) commands

Concept: In this Practical make use of socket programming. On the client side design an
application that w ill give the list of commands. The client will invoke these commands.
On the server side these commands will be executed. The implementation of the above
commands will be on the server side.

a) Implementation of stack
Commands: PUSH, POP, DISPLAY, EXIT, etc.

b) Implementation of queue
Commands: INSERT, DELETE, DISPLAY, EXIT, etc.

c) Implementation of link list
Commands: INSERT an element at the beginning, middle and end of the linked
list, DELETE an element from the beginning, middle and end of the linked list.
DISPLAY the linked list. EXIT.


Client program common for all.

Clnt.java

import java.io.*;
import java.net.*;
class clnt
{
public static void main(String args[])
{
try
{
String inc="";
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
Socket s=new Socket("localhost",9999);
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
while(true)
{
System.out.println(br2.readLine());
inc=br1.readLine();
pw.println(inc);
if(inc.equals("D"))
{
s.close();
System.out.println("operation complete...");
break;
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}



a. Implementation of stack.


Stack1.java

class Stack1
{
private int stack[];
private int tos;
Stack1(int size)
{
stack=new int[size];
tos=-1;
}
boolean isFull()
{
if(tos==stack.length-1)
return true;
else
return false;
}
boolean isEmpty()
{
if(tos<0)
return true;
else
return false;
}
void push(int item)
{
if(!(tos==stack.length-1))
stack[++tos]=item;
}
int pop()
{
return stack[tos--];
}
int[] getStack()
{
return stack;
}
int getCrSize()
{
return (tos+1);
}
}



srvr_stack.java

import java.io.*;
import java.net.*;


Parallel processing & distributed computing M.Sc. IT part-ll 3
class srvr_stack implements Runnable
{
static ServerSocket ss;
Socket s;
BufferedReader br;
PrintWriter pw;
Stack1 stack;
Thread th;
int cid,size=0,sizen=0,i=0,e=0,tmpsize=0;
static int c=0;
String ch=" ";
public srvr_stack(Socket skt) throws Exception
{
this.s=skt;
cid=++c;
System.out.println("client:"+cid+" Accepted-");
br=new BufferedReader(new InputStreamReader(skt.getInputStream()));
pw=new PrintWriter(skt.getOutputStream(),true);
th=new Thread(this);
th.start();
}
public void run()
{
try
{
getSize();
menu();
while(!(ch.equals("D"))){
ch=br.readLine();
if(ch.equals("A")) pushdata();
if(ch.equals("B")) popdata();
if(ch.equals("C")) displaydata();
if(ch.equals("D")) exitstack();
if(ch.equals("M")) menu();
}
}
catch(Exception e)
{
pw.println(e);
}
}
public void pushdata() throws Exception
{
if(stack.isFull()) pw.println("STACK FULL..(press M for menu)");
else
{
for(i=0;i<size;i++)
{
pw.println("ENTER ELEMENT:");
e=Integer.parseInt((br.readLine()).trim());


Parallel processing & distributed computing M.Sc. IT part-ll 4
stack.push(e);
}
size=sizen+size;
pw.println(i+" elements pushed..(press M for menu)");
}
}
public void popdata() throws Exception
{
if(stack.isEmpty()){pw.println("STACK EMPTY..(press M for menu)");}
else pw.println(stack.pop()+" popped.. (press M for menu)");
sizen=stack.getCrSize();
size=tmpsize-sizen;
}
public void displaydata() throws Exception
{
if(stack.isEmpty()){pw.println("STACK EMPTY..(press M for menu)");}
else
{
int[] st=stack.getStack();
String str="";
for(int i=0;i<stack.getCrSize();i++)
str=str+" "+st[i];
pw.println("Stack contains: "+str+" (press M for menu)");
}
}
public void exitstack() throws Exception
{
s.close();
System.out.println("Client "+cid+" exited..");
}
public void menu()
{
pw.println("A.push B.pop C.display D.exit Enter choice:");
}
public void getSize() throws Exception
{
pw.println("enter size of stack:");
size=Integer.parseInt((br.readLine()).trim());
stack=new Stack1(size);
tmpsize=size;
}
public static void main(String args[]) throws Exception
{
ss=new ServerSocket(9999);
System.out.println("server started...");
while(true)
{
Socket sckt=ss.accept();
srvr_stack server=new srvr_stack(sckt);
}}}





rmi program java

AddClient.java

import java.rmi.*;
public class AddClient {
public static void main(String args[]) {
try {
String addServerURL = "rmi://" + args[0] + "/AddServer";
AddServerIntf addServerIntf =
(AddServerIntf)Naming.lookup(addServerURL);
System.out.println("The first number is: " + args[1]);
double d1 = Double.valueOf(args[1]).doubleValue();
System.out.println("The second number is: " + args[2]);
double d2 = Double.valueOf(args[2]).doubleValue();
System.out.println("The sum is: " + addServerIntf.add(d1, d2));
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}

AddServer.java

import java.net.*;
import java.rmi.*;
public class AddServer {
public static void main(String args[]) {
try {
AddServerImpl addServerImpl = new AddServerImpl();
Naming.rebind("AddServer", addServerImpl);
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}

AddServerIntf.java

import java.rmi.*;
public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws RemoteException;
}

AddServerImpl.java

import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject
implements AddServerIntf {
public AddServerImpl() throws RemoteException {
}
public double add(double d1, double d2) throws RemoteException {
return d1 + d2;
}
}

java

How secure is the Java Wallet

Most readers are probably aware of the numerous security problems discovered in the past two years in Java-enabled Web browsers. Hostile applets remain a threat, even in the latest browsers, and third parties who claim to offer tools to bolster security frequently fail to do so. Thus there is a real need to scrutinize all types of Java-based products for security problems and to candidly report those problems in a public forum. This has the desirable dual effects of alerting users to potential threats and bringing market pressure to bear on vendors who continue to offer flawed products as secure solutions.
In keeping with this line of thinking, we have recently taken a close look at Sun's Java Wallet. Our examination revealed a number of problems that allow untrusted applets to exploit systems on which the Java Wallet is installed. Among our examples are applets such as the following:
  • Pickpocket: an applet which steals information and passwords from the Java Wallet
  • BookMarker: an applet which alters the behavior of the Java Wallet's "Help Contents" button
  • DemonDialer: an applet which can gain full access to your modem.
Since the Java Wallet is a recent and evolving product, we begin with an overview of its key components before moving on to consider several of its problems. For more details, the reader is invited to consult The Java Wallet Architecture White Paper and the Java Commerce Business Perspective from Sun Microsystems which are linked from the bottom of this article.
What is the Java Wallet?
There are at least two distinct senses in which the term Java Wallet is used. In its broadest sense, the Java Wallet is simply the Java Electronic Commerce Framework (JECF). In a non-O'Reilly nutshell, the JECF is designed to be an open, extensible architecture for developing secure electronic commerce applications in Java. It consists of a number of application programming interfaces (APIs) that extend those of the Java Developer's Kit (JDK) as well as a suite of tools to assist developers in building electronic commerce applications with those APIs. (These APIs are sometimes collectively referred to as the Java Commerce Client (JCC) Framework.) In its narrower sense, the Java Wallet is a program with a wallet-like graphical user interface that can be used for on-line purchasing and numerous other financial transactions. As such it is one of the JECF tools developed by Sun using the JECF API's. It is in this narrower sense that we consider the Java Wallet.
In order to better understand how the Java Wallet can fail, we first review some of its key concepts and components. We may enumerate these briefly as follows:
  • Gateway Security Model: The Gateway Security Model is the foundation of the Java Wallet's security. It defines such concepts as roles, permits, gates and contexts which the Java Wallet implements and uses to enforce security restrictions.

  • Instruments, Protocols and Operations: Instruments are objects that represent confidential user data and, more significantly, a Java Wallet user's relationships with businesses and financial institutions. The classic example of an instrument is a credit card. Protocols are fixed sequences of actions, agreed upon in advance, that allow two or more parties to complete a task. Operations are procedures which utilize protocols and instruments to complete significant tasks, such as the electronic transfer of payments.
·         Commerce JavaBeans: Commerce JavaBeans are modular components that extend the JavaBeans model in three ways: they are typed, they offer an extended version of Java Bean Info and they support the Gateway Security Model.
  • Cassettes: Cassettes are signed Java archive (JAR) files that may contain JavaBeans as well as the resources required for their operation. In order to be useful, cassettes must be signed for the proper roles and installed in the Java Wallet. Operations can require specific cassettes, which can be installed dynamically, and cassettes can depend on other cassettes being installed in order to function.
  • Java Commerce Messages: A Java Commerce Message (JCM) is a block of text sent from a commerce server to the user. JCMs use a specific format to convey information about operations, protocols and instruments. They can be downloaded from static files, generated by scripts and invoked by applets. In every case they are used to activate the Java Wallet which parses them, instantiates the necessary Commerce JavaBeans and carries out the requested operations.
  • Java Wallet GUI: The Java Wallet's GUI is typically invoked when a JCM is downloaded. This allows a user and a commerce server to interact effectively. Though designed to be almost completely customizable by developers, the user interface should always contain three main areas: the selector area, the service area and the frame. Of these, the frame, which contains the Trusted Seal indicating communication with a secure server, is not designed to be manipulated by developers for security reasons.
  • JCC Database and its properties: Installing the Java Wallet and creating a wallet user sets up a relational database, the JCC Database, in a file with a name of the form wallet_login.wlt. Since the JCC Database contains sensitive data about registered cassettes and may contain credit card numbers, PINs and passwords, it is encrypted and should be accessible only to the wallet owner who logs in to the Java Wallet. Other important configuration information can be stored in file called jecf.properties in a .jcc folder under the user's home directory. When a user chooses to save a default login name and password for the Java Wallet, that information is stored here in plain text. As we will see, this can lead to a certain amount of applet mischief.
This review of the Java Wallet's main components, while perhaps too brief, should be sufficient to understand its problems. Though a commerce server can invoke a user's Java Wallet in a variety of ways, most of these will not concern us here. Our main interest lies in exploring what untrusted applets can do when the Java Wallet is present. Two of our applets will simply employ the Java Wallet's classes to obtain unauthorized access to sensitive information and services on the user's machine. Though a third applet will invoke the Java Wallet, it will do so in trivial way in order to induce a careless user into using a "Help Contents" button whose behavior the applet has altered.
Applet exploits
We are now ready to take a look at three examples of untrusted applets that exploit weaknesses in the Java Wallet. In order to develop these applets, we downloaded the Java Wallet (Early Access Release 1) and installed it according to the instructions. Since the Java Wallet required Sun's Java Plug-in 1.1 to be installed, we downloaded and installed that as well. We later learned that the Java Wallet does not always work properly with the Java Plug-in, and so we also installed it with the Project Java Activator (Early Access Release 3). Unfortunately, our example applets work with both the Activator and the Plug-in-in.
Pickpocket: an applet which steals information and passwords from the Java Wallet
Applications that save login and password data for your convenience are always tempting targets. Unfortunately, Sun's Java Wallet offers this feature, and we found that it is easy for an untrusted applet to recover any saved information. Our first example shows how an applet can discover the Java Wallet's home on a user's machine. This necessarily contains the user's home directory (according to the user.home property). The example also shows that an applet can read the Java Wallet's properties file (jecf.properties). This allows the applet to gather saved user login data and "decrypt" saved passwords.
To test this applet, you first need to set up Sun's Java Plug-in (or Activator) and Java Wallet, and you need to create and save a wallet as the default for auto-login. We set up a wallet with a login name "dummy" and password "easy2read," and then we ran our applet. This yielded some very interesting information. You can also read the source code to see how applets can easily access the jecf.properties file. Clearly some restrictions will need to be placed on this applet capability. Using a BASE64Encoder to "encrypt" passwords is also not a very good idea.
BookMarker: an applet which alters the behavior of the Java Wallet's "Help Contents" button
Our second example demonstrates how an untrusted applet can alter the behavior of the Java Wallet's "Help Contents" button. On Solaris systems this allows the applet to execute commands via Netscape's Communicator when that button is pressed.
In order for the "Help Contents" button to function at all, the user must have already set the property jecf.browser.path in the file jecf.properties. Since this is an apparently unadvertised feature of the Java Wallet, a second applet runs on the same page as the BookMarker and prompts the user to set that property. Note that this applet runs via the Communicator's own Java Runtime and that it uses Netscape's security apparatus to display Netscape's certificate along with its own message.
In order to try these applets, you need to set up Sun's Java Plug-in (or Activator) and Java Wallet along with Netscape's Communicator (4.04 or 4.05) on a Solaris system. You should also have Sun's Purchase 1.1 Java Wallet cassette and a Java Wallet user set up. Then you should be able to run the applets.
Helper.java retrieves private information about Netscape's System Principal by creating a handy encoder. It then creates a fake System Principal and uses that to display Netscape's certificate along with a message asking you to verify that jecf.browser.path is properly set in jecf.properties. If this social engineering effort works, the Java Wallet's "Help Contents" button will function and be able to do the applet's bidding.
BookMarker.java first downloads a bogus Java Commerce Message to start the Java Wallet. Once the Java Wallet is started, the applet can gain access to the field JECF.globals.releaseBundle and set that to a new ListResourceBundle of its own construction. The bogus Java Commerce Message causes the Java Wallet to display a phony message about an internal test along with instructions to press the "Help Contents" button for more information. If you do so, the applet then adds a bookmark to the Hostile Applets Home Page to your bookmarks.
Note that on Solaris systems lots of other options are available to the applet. Using the -remote flag allows the applet to run any of the Communicator's xfeDoCommand options. Using the -java flag allows the applet to have the Communicator's Java Runtime execute any Java program in your CLASSPATH. For example, this particular ListResourceBundle allows the applet to set up a ServerSocket and echo server on your machine. Clearly the ability to change the behavior of the "Help Contents" button can lead to much mischief.

The Java Wallet and its problems
While troubles like these are serious and embarrassing for vendors of any product that touts itself as a secure solution to the problems of electronic commerce, they need not be fatal for the Java Wallet. It is clear that they can all be corrected by properly restricting public access to methods in the Java Wallet's classes. (Saving login and password information, while perhaps convenient for the user, is a foolish feature that should be removed from future releases of the Java Wallet.) All of these problems may, however, give another black eye to electronic commerce and leave lingering doubts in the public's mind about the wisdom of trusting products like the Java Wallet. In that regard the Java Commerce team may be shooting itself in the foot by releasing a product with too many obvious flaws. We can only hope that as electronic commerce, Java Security and the Java Wallet all mature, we will see fewer and fewer such problems in Java-based products.


JAVA program

AIM:- Design a user define bean name as Temperature. Which is use to check the temperature. Temperature bean has a property currentTemp Also define the TemperaturLlistener and TemperatureEvent class for it Temperature Listener contain one handler called temperatureChanged which is called when temperature changes and TemperatureEvent class has getTemperature() as method.

Source Code

Thermometer.java:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

class Thermometer extends JSlider implements ChangeListener
{
private Vector listeners = new Vector();

public Thermometer(int min,int max,int initial)
{
super(min,max,initial);
addChangeListener(this);
}

public int getTemperature()
{
int i=getValue();
return i;
}
public void setTemperature(int i)
{
setValue(i);
}

public void setPaintTemperatureTicks(boolean state)
{
setPaintTicks(state);
}
public void setPaintTemperatureDegrees(boolean state)
{
setPaintLabels(state);
}

public void setMinorTemperatureTickSpace(int i)
{
setMinorTickSpacing(i);
}
public void setMajorTemperatureTickSpace(int i)
{
setMajorTickSpacing(i);
}

public synchronized void addTemperatureListener(TemperatureListener tl)
{
listeners.addElement(tl);
}

public synchronized void removeTemperatureListener(TemperatureListener tl)
{
listeners.removeElement(tl);
}

public void stateChanged(ChangeEvent ce)
{
TemperatureEvent te = new TemperatureEvent(this);
for(Enumeration e = listeners.elements();e.hasMoreElements();)
{
TemperatureListener t=(TemperatureListener)e.nextElement();
t.temperatureChanged(te);
}
}

}

TemperatureEvent.java:

public class TemperatureEvent extends java.util.EventObject
{
public TemperatureEvent(Object source)
{
super(source);
}
}

TemperatureListener.java:

public interface TemperatureListener extends java.util.EventListener
{
void temperatureChanged(TemperatureEvent m);
}


MyThermometer.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class MyThermometer extends JFrame implements TemperatureListener
{
Container cont;
Thermometer t;
JLabel lblTemp;

public MyThermometer()
{
cont=getContentPane();

t=new Thermometer(-5,100,30);
t.setMinorTemperatureTickSpace(1);
t.setMajorTemperatureTickSpace(5);
t.setPaintTemperatureTicks(true);
t.setPaintTemperatureDegrees(true);
t.addTemperatureListener(this);
cont.add(t,BorderLayout.SOUTH);

lblTemp=new JLabel("<html>"+t.getTemperature()+"<sup>o</sup> C<html>",JLabel.CENTER);
lblTemp.setFont(new Font("Arial Black",Font.BOLD,80));
cont.add(lblTemp,BorderLayout.CENTER);

setSize(500,500);
setVisible(true);
}
public void temperatureChanged(TemperatureEvent te)
{
int t1=((Thermometer)te.getSource()).getTemperature();
lblTemp.setText("<html>"+t1+"<sup>o</sup> C</html>");
}

public static void main(String[] args)
{
MyThermometer mt=new MyThermometer();
mt.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}






Sunday 16 March 2014

Java - Methods Part8

Java - Methods

A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console.
Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, overload methods using the same names, and apply method abstraction in the program design.

Creating a Method:

In general, a method has the following syntax:
modifier returnValueType methodName(list of parameters) {
  // Method body;
}
A method definition consists of a method header and a method body. Here are all the parts of a method:
  • Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.
  • Return Type: A method may return a value. The returnValueType is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the returnValueType is the keyword void.
  • Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.
  • Parameters: A parameter is like a placeholder. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.
  • Method Body: The method body contains a collection of statements that define what the method does.
Java Methods
Note: In certain other languages, methods are referred to as procedures and functions. A method with a nonvoid return value type is called a function; a method with a void return value type is called a procedure.

Example:

Here is the source code of the above defined method called max(). This method takes two parameters num1 and num2 and returns the maximum between the two:
/** Return the max between two numbers */
public static int max(int num1, int num2) {
   int result;
   if (num1 > num2)
      result = num1;
   else
      result = num2;

   return result; 
}

Calling a Method:

In creating a method, you give a definition of what the method is to do. To use a method, you have to call or invoke it. There are two ways to call a method; the choice is based on whether the method returns a value or not.
When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed or when its method-ending closing brace is reached.
If the method returns a value, a call to the method is usually treated as a value. For example:
int larger = max(30, 40);
If the method returns void, a call to the method must be a statement. For example, the method println returns void. The following call is a statement:
System.out.println("Welcome to Java!");

Example:

Following is the example to demonstrate how to define a method and how to call it:
public class TestMax {
   /** Main method */
   public static void main(String[] args) {
      int i = 5;
      int j = 2;
      int k = max(i, j);
      System.out.println("The maximum between " + i +
                    " and " + j + " is " + k);
   }

   /** Return the max between two numbers */
   public static int max(int num1, int num2) {
      int result;
      if (num1 > num2)
         result = num1;
      else
         result = num2;

      return result; 
   }
}
This would produce the following result:
The maximum between 5 and 2 is 5
This program contains the main method and the max method. The main method is just like any other method except that it is invoked by the JVM.
The main method's header is always the same, like the one in this example, with the modifiers public and static, return value type void, method name main, and a parameter of the String[] type. String[] indicates that the parameter is an array of String.

The void Keyword:

This section shows how to declare and invoke a void method. Following example gives a program that declares a method named printGrade and invokes it to print the grade for a given score.

Example:

public class TestVoidMethod {

   public static void main(String[] args) {
      printGrade(78.5);
   }

   public static void printGrade(double score) {
      if (score >= 90.0) {
         System.out.println('A');
      }
      else if (score >= 80.0) {
         System.out.println('B');
      }
      else if (score >= 70.0) {
         System.out.println('C');
      }
      else if (score >= 60.0) {
         System.out.println('D');
      }
      else {
         System.out.println('F');
      }
   }
}
This would produce the following result:
C
Here the printGrade method is a void method. It does not return any value. A call to a void method must be a statement. So, it is invoked as a statement in line 3 in the main method. This statement is like any Java statement terminated with a semicolon.

Passing Parameters by Values:

When calling a method, you need to provide arguments, which must be given in the same order as their respective parameters in the method specification. This is known as parameter order association.
For example, the following method prints a message n times:
public static void nPrintln(String message, int n) {
  for (int i = 0; i < n; i++)
    System.out.println(message);
}
Here, you can use nPrintln("Hello", 3) to print "Hello" three times. The nPrintln("Hello", 3) statement passes the actual string parameter, "Hello", to the parameter, message; passes 3 to n; and prints "Hello" three times. However, the statement nPrintln(3, "Hello") would be wrong.
When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as pass-by-value. If the argument is a variable rather than a literal value, the value of the variable is passed to the parameter. The variable is not affected, regardless of the changes made to the parameter inside the method.
For simplicity, Java programmers often say passing an argument x to a parameter y, which actually means passing the value of x to y.

Example:

Following is a program that demonstrates the effect of passing by value. The program creates a method for swapping two variables. The swap method is invoked by passing two arguments. Interestingly, the values of the arguments are not changed after the method is invoked.
public class TestPassByValue {

   public static void main(String[] args) {
      int num1 = 1;
      int num2 = 2;

      System.out.println("Before swap method, num1 is " +
                          num1 + " and num2 is " + num2);

      // Invoke the swap method
      swap(num1, num2);
      System.out.println("After swap method, num1 is " +
                         num1 + " and num2 is " + num2);
   }
   /** Method to swap two variables */
   public static void swap(int n1, int n2) {
      System.out.println("\tInside the swap method");
      System.out.println("\t\tBefore swapping n1 is " + n1
                           + " n2 is " + n2);
      // Swap n1 with n2
      int temp = n1;
      n1 = n2;
      n2 = temp;

      System.out.println("\t\tAfter swapping n1 is " + n1
                           + " n2 is " + n2);
   }
}
This would produce the following result:
Before swap method, num1 is 1 and num2 is 2
        Inside the swap method
                Before swapping n1 is 1 n2 is 2
                After swapping n1 is 2 n2 is 1
After swap method, num1 is 1 and num2 is 2

Overloading Methods:

The max method that was used earlier works only with the int data type. But what if you need to find which of two floating-point numbers has the maximum value? The solution is to create another method with the same name but different parameters, as shown in the following code:
public static double max(double num1, double num2) {
  if (num1 > num2)
    return num1;
  else
    return num2;
}
If you call max with int parameters, the max method that expects int parameters will be invoked; if you call max with double parameters, the max method that expects double parameters will be invoked. This is referred to as method overloading; that is, two methods have the same name but different parameter lists within one class.
The Java compiler determines which method is used based on the method signature. Overloading methods can make programs clearer and more readable. Methods that perform closely related tasks should be given the same name.
Overloaded methods must have different parameter lists. You cannot overload methods based on different modifiers or return types. Sometimes there are two or more possible matches for an invocation of a method due to similar method signature, so the compiler cannot determine the most specific match. This is referred to as ambiguous invocation.

The Scope of Variables:

The scope of a variable is the part of the program where the variable can be referenced. A variable defined inside a method is referred to as a local variable.
The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.
A parameter is actually a local variable. The scope of a method parameter covers the entire method.
A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration to the end of the block that contains the variable as shown below:
Java Variable Scope
You can declare a local variable with the same name multiple times in different non-nesting blocks in a method, but you cannot declare a local variable twice in nested blocks.

Using Command-Line Arguments:

Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main( ).
A command-line argument is the information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy.they are stored as strings in the String array passed to main( ).

Example:

The following program displays all of the command-line arguments that it is called with:
public class CommandLine {

   public static void main(String args[]){ 
      for(int i=0; i<args.length; i++){
         System.out.println("args[" + i + "]: " +
                                           args[i]);
      }
   }
}
Try executing this program as shown here:
java CommandLine this is a command line 200 -100
This would produce the following result:
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

The Constructors:

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.
Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other startup procedures required to create a fully formed object.
All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used.

Example:

Here is a simple example that uses a constructor:
// A simple constructor.
class MyClass {
   int x;
   
   // Following is the constructor
   MyClass() {
      x = 10;
   }
}
You would call constructor to initialize objects as follows:
public class ConsDemo {

   public static void main(String args[]) {
      MyClass t1 = new MyClass();
      MyClass t2 = new MyClass();
      System.out.println(t1.x + " " + t2.x);
   }
}
Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor's name.

Example:

Here is a simple example that uses a constructor:
// A simple constructor.
class MyClass {
   int x;
   
   // Following is the constructor
   MyClass(int i ) {
      x = i;
   }
}
You would call constructor to initialize objects as follows:
public class ConsDemo {

   public static void main(String args[]) {
      MyClass t1 = new MyClass( 10 );
      MyClass t2 = new MyClass( 20 );
      System.out.println(t1.x + " " + t2.x);
   }
}
This would produce the following result:
10 20

Variable Arguments(var-args):

JDK 1.5 enables you to pass a variable number of arguments of the same type to a method. The parameter in the method is declared as follows:
typeName... parameterName
In the method declaration, you specify the type followed by an ellipsis (...) Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it.

Example:

public class VarargsDemo {

   public static void main(String args[]) {
      // Call method with variable args  
   printMax(34, 3, 3, 2, 56.5);
      printMax(new double[]{1, 2, 3});
   }

   public static void printMax( double... numbers) {
   if (numbers.length == 0) {
      System.out.println("No argument passed");
      return;
   }

   double result = numbers[0];

   for (int i = 1; i <  numbers.length; i++)
      if (numbers[i] >  result)
      result = numbers[i];
      System.out.println("The max value is " + result);
   }
}
This would produce the following result:
The max value is 56.5
The max value is 3.0

The finalize( ) Method:

It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.
For example, you might use finalize( ) to make sure that an open file owned by that object is closed.
To add a finalizer to a class, you simply define the finalize( ) method. The Java runtime calls that method whenever it is about to recycle an object of that class.
Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed.
The finalize( ) method has this general form:
protected void finalize( )
{
   // finalization code here
}
Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class.
This means that you cannot know when or even if finalize( ) will be executed. For example, if your program ends before garbage collection occurs, finalize( ) will not execute.

Java - Streams, Files and I/O

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, Object, localized characters, etc.
A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
Java provides strong but flexible support for I/O related to Files and networks but this tutorial covers very basic functionality related to streams and I/O. We would see most commonly used example one by one:

Byte Streams

Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream andFileOutputStream. Following is an example which makes use of these two classes to copy an input file into an output file:
import java.io.*;

public class CopyFile {
   public static void main(String args[]) throws IOException
   {
      FileInputStream in = null;
      FileOutputStream out = null;

      try {
         in = new FileInputStream("input.txt");
         out = new FileOutputStream("output.txt");
         
         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}
Now let's have a file input.txt with the following content:
This is test for copy file.
As a next step, compile above program and execute it, which will result in creating output.txt file with the same content as we have in input.txt. So let's put above code in CopyFile.java file and do the following:
$javac CopyFile.java
$java CopyFile

Character Streams

Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Characterstreams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are , FileReader and FileWriter.. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.
We can re-write above example which makes use of these two classes to copy an input file (having unicode characters) into an output file:
import java.io.*;

public class CopyFile {
   public static void main(String args[]) throws IOException
   {
      FileReader in = null;
      FileWriter out = null;

      try {
         in = new FileReader("input.txt");
         out = new FileWriter("output.txt");
         
         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}
Now let's have a file input.txt with the following content:
This is test for copy file.
As a next step, compile above program and execute it, which will result in creating output.txt file with the same content as we have in input.txt. So let's put above code in CopyFile.java file and do the following:
$javac CopyFile.java
$java CopyFile

Standard Streams

All the programming languages provide support for standard I/O where user's program can take input from a keyboard and then produce output on the computer screen. If you are aware if C or C++ programming languages, then you must be aware of three standard devices STDIN, STDOUT and STDERR. Similar way Java provides following three standard streams
  • Standard Input: This is used to feed the data to user's program and usually a keyboard is used as standard input stream and represented as System.in.
  • Standard Output: This is used to output the data produced by the user's program and usually a computer screen is used to standard output stream and represented as System.out.
  • Standard Error: This is used to output the error data produced by the user's program and usually a computer screen is used to standard error stream and represented as System.err.
Following is a simple program which creates InputStreamReader to read standard input stream until the user types a "q":
import java.io.*;

public class ReadConsole {
   public static void main(String args[]) throws IOException
   {
      InputStreamReader cin = null;

      try {
         cin = new InputStreamReader(System.in);
         System.out.println("Enter characters, 'q' to quit.");
         char c;
         do {
            c = (char) cin.read();
            System.out.print(c);
         } while(c != 'q');
      }finally {
         if (cin != null) {
            cin.close();
         }
      }
   }
}
Let's keep above code in ReadConsole.java file and try to compile and execute it as below. This program continues reading and outputting same character until we press 'q':
$javac ReadConsole.java
$java ReadConsole
Enter characters, 'q' to quit.
1
1
e
e
q
q

Reading and Writing Files:

As described earlier, A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
Here is a hierarchy of classes to deal with Input and Output streams.
Java I/O Streams
The two important streams are FileInputStream and FileOutputStream, which would be discussed in this tutorial:

FileInputStream:

This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream object to read the file.:
InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows:
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Once you have InputStream object in hand, then there is a list of helper methods which can be used to read to stream or to do other operations on the stream.
SNMethods with Description
1public void close() throws IOException{}
This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
2protected void finalize()throws IOException {}
This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
3public int read(int r)throws IOException{}
This method reads the specified byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if it's end of file.
4public int read(byte[] r) throws IOException{}
This method reads r.length bytes from the input stream into an array. Returns the total number of bytes read. If end of file -1 will be returned.
5public int available() throws IOException{}
Gives the number of bytes that can be read from this file input stream. Returns an int.
There are other important input streams available, for more detail you can refer to the following links:

FileOutputStream:

FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output.
Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object to write the file:
OutputStream f = new FileOutputStream("C:/java/hello") 
Following constructor takes a file object to create an output stream object to write the file. First, we create a file object using File() method as follows:
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Once you have OutputStream object in hand, then there is a list of helper methods, which can be used to write to stream or to do other operations on the stream.
SNMethods with Description
1public void close() throws IOException{}
This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
2protected void finalize()throws IOException {}
This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
3public void write(int w)throws IOException{}
This methods writes the specified byte to the output stream.
4public void write(byte[] w)
Writes w.length bytes from the mentioned byte array to the OutputStream.
There are other important output streams available, for more detail you can refer to the following links:

Example:

Following is the example to demonstrate InputStream and OutputStream:
import java.io.*;

public class fileStreamTest{

   public static void main(String args[]){
   
   try{
      byte bWrite [] = {11,21,3,40,5};
      OutputStream os = new FileOutputStream("test.txt");
      for(int x=0; x < bWrite.length ; x++){
         os.write( bWrite[x] ); // writes the bytes
      }
      os.close();
     
      InputStream is = new FileInputStream("test.txt");
      int size = is.available();

      for(int i=0; i< size; i++){
         System.out.print((char)is.read() + "  ");
      }
      is.close();
   }catch(IOException e){
      System.out.print("Exception");
   } 
   }
}
The above code would create file test.txt and would write given numbers in binary format. Same would be output on the stdout screen.

File Navigation and I/O:

There are several other classes that we would be going through to get to know the basics of File Navigation and I/O.

Directories in Java:

A directory is a File which can contains a list of other files and directories. You use File object to create directories, to list down files available in a directory. For complete detail check a list of all the methods which you can call on File object and what are related to directories.

Creating Directories:

There are two useful File utility methods, which can be used to create directories:
  • The mkdir( ) method creates a directory, returning true on success and false on failure. Failure indicates that the path specified in the File object already exists, or that the directory cannot be created because the entire path does not exist yet.
  • The mkdirs() method creates both a directory and all the parents of the directory.
Following example creates "/tmp/user/java/bin" directory:
import java.io.File;

public class CreateDir {
   public static void main(String args[]) {
      String dirname = "/tmp/user/java/bin";
      File d = new File(dirname);
      // Create directory now.
      d.mkdirs();
  }
}
Compile and execute above code to create "/tmp/user/java/bin".
Note: Java automatically takes care of path separators on UNIX and Windows as per conventions. If you use a forward slash (/) on a Windows version of Java, the path will still resolve correctly.

Listing Directories:

You can use list( ) method provided by File object to list down all the files and directories available in a directory as follows:
import java.io.File;

public class ReadDir {
   public static void main(String[] args) {
      
      File file = null;
      String[] paths;
            
      try{      
         // create new file object
         file = new File("/tmp");
                                 
         // array of files and directory
         paths = file.list();
            
         // for each name in the path array
         for(String path:paths)
         {
            // prints filename and directory name
            System.out.println(path);
         }
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
   }
}
This would produce following result based on the directories and files available in your /tmp directory:
test1.txt
test2.txt
ReadDir.java
ReadDir.class

Java - Exceptions Handling

An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following:
  • A user has entered invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.
Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.
To understand how exception handling works in Java, you need to understand the three categories of exceptions:
  • Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
  • Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.
  • Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

Exception Hierarchy:

All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class.
Errors are not normally trapped form the Java programs. These conditions normally happen in case of severe failures, which are not handled by the java programs. Errors are generated to indicate errors generated by the runtime environment. Example : JVM is out of Memory. Normally programs cannot recover from errors.
The Exception class has two main subclasses: IOException class and RuntimeException Class.
Java Exceptions
Here is a list of most common checked and unchecked Java's Built-in Exceptions.

Exceptions Methods:

Following is the list of important medthods available in the Throwable class.
SNMethods with Description
1public String getMessage() Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.
2public Throwable getCause() Returns the cause of the exception as represented by a Throwable object.
3public String toString() Returns the name of the class concatenated with the result of getMessage()
4public void printStackTrace() Prints the result of toString() along with the stack trace to System.err, the error output stream.
5public StackTraceElement [] getStackTrace() Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.
6public Throwable fillInStackTrace() Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.

Catching Exceptions:

A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:
try
{
   //Protected code
}catch(ExceptionName e1)
{
   //Catch block
}
A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.

Example:

The following is an array is declared with 2 elements. Then the code tries to access the 3rd element of the array which throws an exception.
// File Name : ExcepTest.java
import java.io.*;
public class ExcepTest{

   public static void main(String args[]){
      try{
         int a[] = new int[2];
         System.out.println("Access element three :" + a[3]);
      }catch(ArrayIndexOutOfBoundsException e){
         System.out.println("Exception thrown  :" + e);
      }
      System.out.println("Out of the block");
   }
}
This would produce the following result:
Exception thrown  :java.lang.ArrayIndexOutOfBoundsException: 3
Out of the block

Multiple catch Blocks:

A try block can be followed by multiple catch blocks. The syntax for multiple catch blocks looks like the following:
try
{
   //Protected code
}catch(ExceptionType1 e1)
{
   //Catch block
}catch(ExceptionType2 e2)
{
   //Catch block
}catch(ExceptionType3 e3)
{
   //Catch block
}
The previous statements demonstrate three catch blocks, but you can have any number of them after a single try. If an exception occurs in the protected code, the exception is thrown to the first catch block in the list. If the data type of the exception thrown matches ExceptionType1, it gets caught there. If not, the exception passes down to the second catch statement. This continues until the exception either is caught or falls through all catches, in which case the current method stops execution and the exception is thrown down to the previous method on the call stack.

Example:

Here is code segment showing how to use multiple try/catch statements.
try
{
   file = new FileInputStream(fileName);
   x = (byte) file.read();
}catch(IOException i)
{
   i.printStackTrace();
   return -1;
}catch(FileNotFoundException f) //Not valid!
{
   f.printStackTrace();
   return -1;
}

The throws/throw Keywords:

If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the different in throws and throw keywords.
The following method declares that it throws a RemoteException:
import java.io.*;
public class className
{
   public void deposit(double amount) throws RemoteException
   {
      // Method implementation
      throw new RemoteException();
   }
   //Remainder of class definition
}
A method can declare that it throws more than one exception, in which case the exceptions are declared in a list separated by commas. For example, the following method declares that it throws a RemoteException and an InsufficientFundsException:
import java.io.*;
public class className
{
   public void withdraw(double amount) throws RemoteException,
                              InsufficientFundsException
   {
       // Method implementation
   }
   //Remainder of class definition
}

The finally Keyword

The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.
Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.
A finally block appears at the end of the catch blocks and has the following syntax:
try
{
   //Protected code
}catch(ExceptionType1 e1)
{
   //Catch block
}catch(ExceptionType2 e2)
{
   //Catch block
}catch(ExceptionType3 e3)
{
   //Catch block
}finally
{
   //The finally block always executes.
}

Example:

public class ExcepTest{

   public static void main(String args[]){
      int a[] = new int[2];
      try{
         System.out.println("Access element three :" + a[3]);
      }catch(ArrayIndexOutOfBoundsException e){
         System.out.println("Exception thrown  :" + e);
      }
      finally{
         a[0] = 6;
         System.out.println("First element value: " +a[0]);
         System.out.println("The finally statement is executed");
      }
   }
}
This would produce the following result:
Exception thrown  :java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed
Note the following:
  • A catch clause cannot exist without a try statement.
  • It is not compulsory to have finally clauses when ever a try/catch block is present.
  • The try block cannot be present without either catch clause or finally clause.
  • Any code cannot be present in between the try, catch, finally blocks.

Declaring you own Exception:

You can create your own exceptions in Java. Keep the following points in mind when writing your own exception classes:
  • All exceptions must be a child of Throwable.
  • If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.
  • If you want to write a runtime exception, you need to extend the RuntimeException class.
We can define our own Exception class as below:
class MyException extends Exception{
}
You just need to extend the Exception class to create your own Exception class. These are considered to be checked exceptions. The following InsufficientFundsException class is a user-defined exception that extends the Exception class, making it a checked exception. An exception class is like any other class, containing useful fields and methods.

Example:

// File Name InsufficientFundsException.java
import java.io.*;

public class InsufficientFundsException extends Exception
{
   private double amount;
   public InsufficientFundsException(double amount)
   {
      this.amount = amount;
   } 
   public double getAmount()
   {
      return amount;
   }
}
To demonstrate using our user-defined exception, the following CheckingAccount class contains a withdraw() method that throws an InsufficientFundsException.
// File Name CheckingAccount.java
import java.io.*;

public class CheckingAccount
{
   private double balance;
   private int number;
   public CheckingAccount(int number)
   {
      this.number = number;
   }
   public void deposit(double amount)
   {
      balance += amount;
   }
   public void withdraw(double amount) throws
                              InsufficientFundsException
   {
      if(amount <= balance)
      {
         balance -= amount;
      }
      else
      {
         double needs = amount - balance;
         throw new InsufficientFundsException(needs);
      }
   }
   public double getBalance()
   {
      return balance;
   }
   public int getNumber()
   {
      return number;
   }
}
The following BankDemo program demonstrates invoking the deposit() and withdraw() methods of CheckingAccount.
// File Name BankDemo.java
public class BankDemo
{
   public static void main(String [] args)
   {
      CheckingAccount c = new CheckingAccount(101);
      System.out.println("Depositing $500...");
      c.deposit(500.00);
      try
      {
         System.out.println("\nWithdrawing $100...");
         c.withdraw(100.00);
         System.out.println("\nWithdrawing $600...");
         c.withdraw(600.00);
      }catch(InsufficientFundsException e)
      {
         System.out.println("Sorry, but you are short $"
                                  + e.getAmount());
         e.printStackTrace();
      }
    }
}
Compile all the above three files and run BankDemo, this would produce the following result:
Depositing $500...

Withdrawing $100...

Withdrawing $600...
Sorry, but you are short $200.0
InsufficientFundsException
        at CheckingAccount.withdraw(CheckingAccount.java:25)
        at BankDemo.main(BankDemo.java:13)

Common Exceptions:

In Java, it is possible to define two catergories of Exceptions and Errors.
  • JVM Exceptions: - These are exceptions/errors that are exclusively or logically thrown by the JVM. Examples : NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException,
  • Programmatic exceptions: - These exceptions are thrown explicitly by the application or the API programmers Examples: IllegalArgumentException, IllegalStateException.