Saturday, July 16, 2011

Addition of two numbers

import java.io.*; //import java.io package
import java.lang.*; //import java.lang package


class add
{
public static void main(String args[])throws IOException
{

DataInputStream in = new DataInputStream(System.in);
int a=0;
int b=0;
a = Integer.parseInt(in.readLine());
b = Integer.parseInt(in.readLine());
int d = a+b;
System.out.println(d);
}
}


/*package contain classes and methods*/


Description:
1. uses of static
we cannot call any instance method without create a any instance to the class..at the time of program starts we cant have any instance,bcos java is object oriented so only
declaration of main also require one class...static can used to compile without have an object

2. java is case sensitive
we need to specify correctly class name,and methods

3.input metods
i thing in java various way to get input from console..

=> using Scanner class
=> using BufferedDeader class
=> using command line arguments args[0],args[1],etc
=> using datainputstream class


in this program we can use datainputstream class
its under the package of java.io.datainputstream;

the input read from the console is string , so if we need other type we can convert the string using the following methods.
1.integer.parseInt(string)

we "in" is a object for datainputstream class,using this object to call the method readLine() = read from the console and return the string



>javap java.io.DataInputStream

Compiled from "DataInputStream.java"
public class java.io.DataInputStream extends java.io.FilterInputStream implements java.io.DataInput{
public java.io.DataInputStream(java.io.InputStream);

public final int read(byte[]) throws java.io.IOException;
public final int read(byte[], int, int) throws java.io.IOException;
public final void readFully(byte[]) throws java.io.IOException;
public final void readFully(byte[], int, int) throws java.io.IOException;
public final int skipBytes(int) throws java.io.IOException;
public final boolean readBoolean() throws java.io.IOException;
public final byte readByte() throws java.io.IOException;
public final int readUnsignedByte() throws java.io.IOException;
public final short readShort() throws java.io.IOException;
public final int readUnsignedShort() throws java.io.IOException;
public final char readChar() throws java.io.IOException;
public final int readInt() throws java.io.IOException;
public final long readLong() throws java.io.IOException;
public final float readFloat() throws java.io.IOException;
public final double readDouble() throws java.io.IOException;
public final java.lang.String readLine() throws java.io.IOException;
public final java.lang.String readUTF() throws java.io.IOException;
public static final java.lang.String readUTF(java.io.DataInput) throws java.io.IOException;
}



system.out.print - for printing continiously


system.out.println - for printing line by line

1 comment:

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...