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:-

Command Line Argument

/*Write a program for command line argument.*/
//programming789.blogspot.com
class CommandLineArgument
{
  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 is =" +sum);
  }
  catch(Exception e)
  {
System.out.println(e); 
  }
  }
}

● Save the file as CommandLineArgument.java.


Output:-
>java CommandLineArgument 30 40
result is =70

Method Overloading In Java

/*Write a simple program for method overloading in java.*/
//programming789.blogspot.com
class Overloading
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a,double b)
{
System.out.println(a+b);
}
public static void main(String args[])
{
Overloading obj=new Overloading();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}

● Save the file as Overloading.java.

Output:-
>java Overloading
21.0
40

Concept Of Super Keyword In Java.

  The super keyword in java is a reference variable that is used to refer immediate parent class object.

/*program with super keyword*/
class Bike
       {
int speed=50;
}
class SuperTest extends Bike
{
    int speed=100;
    void display()
    {
    System.out.println(super.speed);//will print speed of Bike
    }
    public static void main(String args[])
     {
      SuperTest b=new SuperTest();
      b.display();
     }
}

● Save the file as SuperTest.java.

Output:-
>java SuperTest
50

/*program without super keyword*/
class Bike
       {
int speed=50;
}
class SuperTest2 extends Bike
{
 int speed=100;
 void display()
{
 System.out.println(speed);//will print speed of Bike
}
 public static void main(String args[])
{
 SuperTest2 b=new SuperTest2();
 b.display();
}
}

● Save the file as SuperTest2.java.

Output:-
>java SuperTest2
100

Concept Of This Keyword.

  This Keyword
         This is a reference variable that refers to the current object.

Usage Of Java this Keyword.
  1. this keyword can be used to refer current class instance variable.
  2. this() can be used to invoke current class constructor.
  3. this keyword can be used to invoke current class method (implicitly)
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this keyword can also be used to return the current class instance.

/*Concept Of This keyword Example In Java.*/

//programming789.blogspot.com

class KeyTest

{

int id;

String name;

   KeyTest(int id,String name)
      {  
        this.id = id;
this.name = name;
       }
   void display()
       {
System.out.println(id+" "+name);
        }
   public static void main(String args[])
       {
KeyTest s1 = new KeyTest(142,"Bob");
KeyTest s2 = new KeyTest(452,"John");
s1.display();
s2.display();
        }
}

● Open notepad and add the code.
● Save the file as KeyTest.java.

Output:-
>java KeyTest
142 Bob
452 John

Sum Of Two Numbers

Sum of two integer number:
/* Addition of two integer */
//programming789.blogspot.com
import java.util.Scanner;
class AddNumbers
{
   public static void main(String args[])
   {
      int a, b, c;
      Scanner in = new Scanner(System.in);       
      System.out.println("Enter 1st no:");
      a = in.nextInt();
      System.out.println("Enter 2st no:");
      b = in.nextInt();
      c = a + b;
      System.out.println("Sum of entered integers = "+c);
   }
}
● Open notepad and add the code.
● Save the file as AddNumbers.java.
● When the program is run, the following output is displayed:

















● Enter the integer value at time of give input.
If you want to add two floating point number add following code:

/* Addition of two float number*/

import java.util.Scanner;
class AddNumbers
{
   public static void main(String args[])
   {
      float a, b, c;
      Scanner in = new Scanner(System.in);       
      System.out.println("Enter 1st no:");
      a = in.nextFloat();
      System.out.println("Enter 2st no:");
      b = in.nextFloat();
      c = a + b;
      System.out.println("Sum of entered integers = "+c);
   }
}