Print Array Using For Loop

/*Print Array Using For Loop*/
//programming789.blogspot.com/
import java.util.Arrays;
class PrintArray
{
public static void main(String args[])
  {
     int Arr[] = new int[]{1,2,3,4,5,6,7,8,9,10};
     for(int i=0; i< 10; i++)
      {
         System.out.print(Arr[i] +", ");
      }
  }
}


● Save the file as PrintArray.java.

Output:
>java PrintArray
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

Create Two Dimensional Array In Java

Declaration

int[][] multiples = new int[4][5];           // 2D integer array with 4 rows and 5 columns 
String[][] cities = new String[2][3];      // 2D String array with 2 rows and 3 columns

When you declare 2 dimensional array must remember to specify first dimension
Example
int[][] wrong = new int[][];   // error at compile time
int[][] right = new int[2][];    // not show error 

Initialize

boolean[][] booleans = new boolean[3][3]; 
String[][] strings= new string[3][3];
byte[][] bytes = new byte[2][2]; 
char
[][] chars = new char[1][1]; 
short[][] shorts = new short[2][2]; 
int[][] ints = new int[2][2];

Example

/*two dimensional array example in java */
//programming789.blogspot.com/
import java.util.Arrays; 
class ArrayExample
 public static void main(String args[]) 
   {  
    String[][] names = { {"John", "Cena"}, {"Jack", "Taylor"}, {"James", "Brown"}, }; 
    System.out.println(names[0][0]+" " +names[0][1]);

    int[][] no={{1,2},{4,1},{6,2},};
    System.out.println(no[0][0]+" " +no[0][1]);
     } 
}

● Save the file as ArrayExample.java.

Output:
John Cena
1 2

Write Java Program to Print Fibonacci Series upto N Number

//Write Java Program to Print Fibonacci Series upto N Number
//programming789.blogspot.com/
import java.util.Scanner;
public class Fibonacci 
{
public static void main(String args[]) 
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter 'N' value :");
int n = Integer.parseInt(scan.nextLine());
int a = -1;
int b = 1;
int c = 0;
     int temp
System.out.println("The Fibonacci Series are :");
for(int i=1; i<=n; i++) 
{
c = a + b;
temp = a;
a = b;
b = c;
System.out.println(c);
}
}
}

● Save the file as Fibonacci.java.

Output:
Enter 'N' value  : 
12
The Fibonacci Series are : 
0
1
1
2
3
5
8
13
21
34
55
89


Recursion In Java

   Java supports recursion. Recursion is the process of defining something in terms of itself. recursion is the attribute that allows a method to call itself. A method that calls itself is said to be recursive.
The best example of recursion is the computation of the factorial of a number.

/*Recursion function example in java*/
//programming789.blogspot.com/
class Factorial {
     int fact(int n)  //recursive function 
    {
     int result;
     if ( n ==1) return 1;
     result = fact (n-1) * n;
     return result;
     }
}
class Recursion {
     public static void main (String args[]) {
          Factorial f =new Factorial();
          System.out.println("Factorial of 1 is " + f.fact(1));
          System.out.println("Factorial of 3 is " + f.fact(3));
          System.out.println("Factorial of 4 is " + f.fact(4));
          System.out.println("Factorial of 5 is " + f.fact(5));
         }
}

● Save the file as Recursion.java.

Output:
>java  Recursion 
Factorial of 1 is 1
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120

Type Casting

  Assigning a value of one type to a variable of another type is known as Type Casting.

In Java, type casting is classified into two types,
    1.Implicit casting
    2.Explicit casting 

1. Implicit casting (widening conversion)


   A data type of lower size is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. 
Example:
/*Implicit type casting program in java.*/ 
//programming789.blogspot.com/
public class CastExample
{
  public static void main(String[] args)
    {
      int i = 102;
      long l = i;       //Implicit type casting 
      double d = l;  //Implicit type casting   
      System.out.println("Int value "+i);  
      System.out.println("Long value "+l);
      System.out.println("Double value "+d);
    }
}
● Save the file as CastExample.java.
Output:
>java  CastExample
Int value 102
Long value 102
Double value 102.0

2.Explicit casting(narrowing conversion)

   When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.
Example:
/*explicit type casting program in java.*/ 
//programming789.blogspot.com/
public class CastExample
{
  public static void main(String[] args)
    {
      double d = 102.04;  
      long l = (long)d;  //explicit type casting 
      int i = (int)l; //explicit type casting   
      System.out.println("Double value "+d);
      System.out.println("Long value "+l);
      System.out.println("Int value "+i);
    }

}
● Save the file as CastExample.java.
Output:
>java  CastExample
Double value 102.04
Long value 102
Int value 102

Interface Program In Java

/*Write an Interface program in java.*/
public class InterfaceTest 
{
  public static void main(String args[]) 

          shapeA circleshape = new circle();
          circleshape.Draw();
        }
}
interface shapeA 
{
    public String baseclass = "shape";
    public void Draw();
 } 
interface shapeB extends shapeA 
{
    public String baseclass = "shape2";
    public void Draw2();

interface shapeC 
{
    public String baseclass = "shape3"; 
    public void Draw3();
}
class circle implements shapeA, shapeB, shapeC 
{
    public void Draw() 
{
        System.out.println("Drawing Circle here:" + shapeA.baseclass);
         }
    public void Draw2() 
{
        System.out.println("Drawing Circle here:" + shapeB.baseclass);
         }
    public void Draw3() 
         {
        System.out.println("Drawing Circle here:" + shapeC.baseclass);
        }
}

● Save the file as InterfaceTest.java.

Output:-
>java InterfaceTest
Drawing Circle here:shape

Exception Handling Simple Program In Java

/*Write a program for Handling an Exception In java.*/
class ExceptionHandling
{
public static void main(String args[])
{
try
{
int a=Integer.parseInt(arr[0]);
int b=Integer.parseInt(arr[1]);
int sum = a/b;
System.out.println("Result:"+ sum);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

● Save the file as ExceptionHandling.java.

Output:-