Showing posts with label Java Programming language. Show all posts
Showing posts with label Java Programming language. Show all posts

Tuesday, 28 August 2012

EXCEPTION HANDLING IN JAVA !!


Hello friends !! Today i am  going to tell about EXCEPTION HANDLING in Java ...

EXCEPTION HANDLING



The first question that we strike to our mind is what is EXCEPTION ??

  Exception: is a problem that arises during the execution of a program.
These are following reason by which there is a possibility to come an exception:
·         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.
Exception Categories :
·         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 compliation.
·         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.

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.

Let us take an example….through which we can easily understand how an exception may come and how we handle that exception……..
class abc
{
public static void main(String[] arg)
{
int x=10;
int y=5;
int z=5;
try{
int p=x/(y-z);
}
catch(AirthmeticException e)
{
System.out.println(“Exception”);
}
}
}
output:
Exception

There is an another example of ArrayIndexOutOfBoundException
class abc
{
Public static void main(String[] arg)
{
Int a[]={5,10};
Int b=5;
Try{
Int x=a[2]/a[1];  //exception come due to a[2] as this was not defined
}
Catch(ArrayIndexOutOfBoundException e)
{
System.out.println(“exception”);
}
}
}
Multiple catch statement:
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
}

Example of multiple catch  statement:
class abc
{
public static void main(String[] arg)
{
int x=10;
int y=5;
int z=5;
try{
int p=x/(y-z);
}
catch(ArithmeticException e)
{
System.out.println(“Exception”);
}
catch(ArrayIndexOutOfBoundException e)
{
System.out.println(“Array range”);
}
Catch(ArrayStoreException e)
{
System.out.println(“Array wrong data type”);
}
}
}
output:
Exception

Now !! You can  download the copy of this article in pdf format by just click on below download button..
If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)

Saturday, 25 August 2012

Multithreading in Java !!



Hi Friends !! Today I am talking about one of the most important topic of java…which is MULTITHREADING.
MULTITHREADING
To see this topic, the very first ques. strike to our mind is that….What is multithreading??????
                          A multithread program contains two or more parts that can run concurrently….these such parts of program is known as thread.

Each thread defines a separate part of execution.So,we an say Multithreading is same as or a type of Multitasking….

Multithreading enables you to write very efficient program that make maximum use of CPU, bcoz idle time of CPU kept to a minimum.

Thread life cycle:
Threads exit in several stage:
Running:It means it can be ready to run as soon as it gets CPU time.
Suspended:It means temporarily suspend the activity..
Resume: it means suspended thread allowing it to pick up where it left off.
Block: a thread is waiting for a resource.

  •      At any time,a thread can be terminated,which halts its execution immediately.Once    terminated, a thread cannot  be resumed.
  •      A main thread is declared in the program ,which begins running immediately when the Java program starts up…
This main thread is mainly use for 2 reason:
1) It is the thread from which other “child ”threads will be spawned.
2) Often it must be the last thread to finish execution bcoz it performs various shutdown actions….
5 phases of a thread can be explain through diagram as:=>
                                                                                                                                      these are the methods through which we go from one state to another state…
Let us see an simple example of multithreading:
Class abc extends Thread
{
public void run()
{for(int x=1;x<=5;x++)
{
System.out.println(x);
}
}
}         
Class def extends Thread
{
public void run()
{for(int y=1;y<=5;y++)
{
System.out.println(y);
}
}
}         

Class ghi extends Thread
{
public void run()
{for(int z=1;z<=5;z++)
{
System.out.println(z);
}
}
}         
Class pqr
{
public static void main(String[] args)
{
abc a1=new abc();
a1.start();
def d1=new def();
d1.start();
ghi g1=new ghi();
g1.start();
}
}
output:
1,2,3,4,5
Another example of multithreading in which we use the methods- yeild(),sleep(),stop()
Class abc extends Thread
{
public void run()
{for(int x=1;x<=5;x++)
{
if(x==1)
yield();
System.out.println(x);
}
}
}         
Class def extends Thread
{
public void run()
{for(int y=1;y<=5;y++)
{
System.out.println(y);
if(y==3)
stop();
}
}
}         

Class ghi extends Thread
{
public void run()
{for(int z=1;z<=5;z++)
{
System.out.println(z);
if(z==1)
try{
sleep(1000);
}
catch(Exception e)
{System.out.println(“exception”);
}
}
}
}         
Class pqr
{
public static void main(String[] args)
{
abc a1=new abc();
a1.start();
def d1=new def();
d1.start();
ghi g1=new ghi();
g1.start();
}
}
 Now !! You can download copy of this article in pdf format by just click on below download link..
If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)

Monday, 20 August 2012

Assingning Object reference variables !!


Hello friends !! Today I am teliing about how we assign object reference variables and adding a method to the class……


Assingning Object reference variables 

Lets us go through an example….
Abc a1=new Abc();
Abc a2=a1;

Here a2 is being assigned a reference to a copy of the object referred to by a1.  
                This means any change made to the object through a2 will affect the object to which  a1 is referring..,since they are the same object.
             Although a1 and a2 both refer to the same object ,they are not linked in any other way.
For eg:
 A assignment to a1 will simply unhook from the original object without affecting the object or affecting a2.
Abc a1=new Abc();
Abc a2=a1;
a1=null;
Note:when you assign one object reference variable to another object reference variable ,you are not creating a copy of the object, you are only making a copy of the object,you are only making a copy of the reference.

Methods declaration in class:
          Usually a class contain two things: data members and member function(methods).
The topic of metods in java is soo vast because  java gives them so much power and flexibility .
General way to declare a method:
Type  name(parameter list){
//body of the method
}
Here type defines the data type or returned type of the parameter list.
If a method does not return to a value, it must be have return type void.
Name specified the method name.It must be a legal identifier.
Parameter list is a sequence of type and identifier pairs separated by commas.
These are essential variables that receive the value of the arguments paased to the method when it is invoked.
If no parmeter list thn it must be empty.
Methods that have a return type other than void returns a value by invoking the using the return statement…
return value; //value is the value returned by method.

Example showing function declaration:
class abc
{
int a;
int b;
void ab()
{
System.out.println(“product of a and b” + a + b);
System.out.println(a*b);
}
}
class def
{
public static void main(String[] args)
{
abc a1=new abc();
abc a2=new abc();
a1.a=2;
a1.b=3;
a2.a=3;
a2.b=4;
a1.ab();
a2.ab();
}
Output:
 product of a and b 6
 product of a and b 12

Now ! You can download the pdf file of this article by just click on below download link ...

If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)


Saturday, 18 August 2012

CLASS IN JAVA !!


Hello Friends !! Today we are starting the concept of class.



Introduction to class !!


  • A class is basically consist of data member and member function.
  • Typically ,a class is declared by using the keyword class .

Syntax of basic class structure:
class  classname
{
Type instance-variable1;
Type methodname(parameter-list)
{
//body of method
}
}
Data ,or variables ,defined within in a class are called instance  variables.

The code is contained within the methods.
 A simple class :
class abc{
int x;
int  y;
}
Create an object of class abc:
abc a1=new abc();
where abc is class name and abc() is constructer
to assign value to the int x with the help of object :
a1.x=10;
a1.y=2;
/* here is the complete program that uses the abc class…
/* save this file as def.java
*/
class abc{
int x;
int y;
}
//this class declare an object of type abc
class def
{
public static void main(String[] args)
{
abc a1=new abc();
int z;
a1.x=10;
a1.y=2;
z=a1.x * a1.y;
System.out.println(“the product of x & y” + z );
}
}
o/p:
the product of x &y 20

Now !! You can download the copy of this article in pdf format by just click on below download link....

If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)

Friday, 17 August 2012

ARRAY IN JAVA !!


Hiii… !!! Today I am going to tell you about the use of  array in Java..
Array !!


It is a type of data structure,which contains fixed size sequential homogeneous set of elements.
Syntax: (declaration of an array)
Datatype[] arrayname;
Example:
Int[5] n;
int c = 15;
int[] p={1,2,3,4,5};

Creation of an array:
  • Actually in Java,array is act as object created by the new method.

Example:
n=new n[5]; \\ allocate memory for the 5 elements in array n
Some points:
  •  When memory is allocated for an array, the elements are automatically initialized to their default values:  zero for all numeric primitive data types, false for boolean variables and null for references.
  • The first element in every array is the zeroth element of that array.
  • When accessing an array element, if the index is less than zero or greater than or equal to the array length then an ArrayIndexOutOfBoundsException is thrown.
  • Arrays have a length data field (read only), specifying the number of elements in the array.
  • The length data field can be accessed through the dot operator.
for ( int k=0; k < a.length ; k++ )
  • System.out.println( a[ k ] );The bounds of an array are integers between 0 and length – 1…..

Coping of an array …
Elements of an Arrays can be copied by System.arraycopy() method:
Arraycopy() does not allocate memory for the destination array; the memory must already
be allocated.

Example: 
int[] p = { 1, 2, 3,4, 5 };
int[] c = new int[ p.length ];
System.arraycopy(p,0,c,0,p.length);  //copy array p to array c
Multidimensional array:
Java also support multidimensional array or you call call it as arrays of arrays.
This can be declared by appending the brackets after the array name.
Example:
Int[][] n=new int[5][5];  //integer array 5 x5 elements
          Int[][][] three=new int[2][4][5]; //integer array 2x4x5 elements

Now !! You can download the copy of this article in pdf file by just click on below download link..

If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)



Wednesday, 15 August 2012

WRAPPER CLASS AND ESCAPE SEQUENCE IN JAVA

Hello friends !!  My todays topic is wrapper class and Escape sequence...


wrapper class and Escape sequence...



Wrapper class:       As we have already studied about the primitive data type ,sometimes we go through a situation where we need to use objects instead of primitive data type(int ,char,float, double…..etc).In order to achieve such case Java introduce the concept of wrapper class for each primitive data type..


 This wrapping is taken care of by the compiler.
When a primitive is used when an  object is required the compiler boxes the primitive type in its wrapper class. …(this process is called boxing)…..
||’larly, the compiler unboxes the object to a primitive as well.
Number  is an example of boxing and unboxing….
Example…
public class abc
{
   public static void main(String args[])
{
      Integer x = 7; // boxes int to an Integer object
      x =  x + 10;   // unboxes the Integer to a int
      System.out.println(x); 
   }
}
Output:
17
When x is assigned value 7 i.e. integer values, the complier boxes the integer bcoz x is integer objects..later, x is unboxed so that they can be added as integers.

Escape sequences:

Some character preceded by backslash (\) is known as escape sequence and has meaning known to compiler.
Here is a table which defines the use of  some escape sequences:


Escape Sequence
Description
\t
Insert a tab in the text at this point.
\b
Insert a backspace in the text at this point.
\n
Insert a newline in the text at this point.
\r
Insert a carriage return in the text at this point.
\f
Insert a form feed in the text at this point.
\'
Insert a single quote character in the text at this point.
\"
Insert a double quote character in the text at this point.
\\
Insert a backslash character in the text at this point.


Now you can download the copy of this article in pdf file by just click on below download link..

If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)


Share

Twitter Delicious Facebook Digg Stumbleupon Favorites