Array index out of bounds exception Java program

//java program to demonstrate array index out of bounds exception


// Program for exception handling in java

 

Description


This program demonstrates ArrayIndexOutOfBoundsException in Java. This type of error occurs when an array is full and cannot include a new element. This program declares an array nos[] of size 3, values are allocated to each array indexes. The statement nos[3]=3; produces an error, because the array is of size 3 and there is no location nos[3], as specified in the statement. 

 

Program


 
public class Bound

{

public static void main(String s[])

{

int nos[]=new int[3];

try

{

nos[0]=0;

nos[1]=1;

nos[2]=2;

nos[3]=3;

}

catch(ArrayIndexOutOfBoundsException ex)

{

System.out.println("Error!! Array Out of Bound");

}

}

}


Output

java program - array index out of bounds exception
Array index out of bounds exception

Java program for function overloading

//program to demonstrate function overloading
// Function overloading in java

Description


Function overloading refers to the use of two or more functions with the same name but with different parameters. The function to execute at run time is identified by the number and type of the parameters. This program defines two functions sumnop(), one without parameter and the other with two parameters. The function call sm.sumnop(); executes the function public void sumnop() and the function call sm.sumnop(x,y); executes the function public void sumnop(int p,int q).

Program



import java.io.*;

class Sum

{

int a,b,c;

public void sumnop()

{

this.a=10;

this.b=5;

this.c=a+b;

System.out.println("Value of a is "+a+"\nValue of b is "+b);

System.out.println("Sum is "+c);

}

public void sumnop(int p,int q)

{

c=p+q;

System.out.println("Sum is "+c);

}

public static void main(String s[]) throws Exception

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int x,y;

Sum sm=new Sum();

System.out.println("Function without parameter");

sm.sumnop();

System.out.println("Function with parameter");

System.out.println("Enter the value for a and b");

x=Integer.parseInt(br.readLine());

y=Integer.parseInt(br.readLine());

sm.sumnop(x,y);

}

}


Output


Program for function overloading in Java
Function Overloading in Java


Java Program to read characters from buffer

//Program to read character by character from the buffer

 

Description

This program demonstrates the concept of BufferedReader in Java. The program declares an instance br of BufferedReader class. The characters entered by the user is read into the variable c using the read() function, each time printing the character to the output screen. This procedure continues until the entered character is not 'q'. 

Program



import java.io.*;

public class BReader

{

 public static void main(String s[])throws Exception

 {char c;

  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

  System.out.println("Type in some characters end with q");

  do

  {

   c=(char)br.read();

   System.out.print(c);

  } while(c!='q');

 }

}


Output


read characters from buffer java program.
Reading characters using buffer