Java Programming

Java program code consists of instructions which will be executed on your computer system to perform a task as an example say arrange given integers in ascending order. This page contains sample programs for beginners to understand how to use Java programming to write simple Java programs. These programs demonstrate how to get input from a user, working with loops, strings, and arrays. Programs are provided with the output, and you can also download class file.


Compiling and executing Java programs:


Java programming Tool: To compile and run Java program code you need to download Java Development Kit (JDK).

To compile : javac file_name.java where file_name is the name of the file containing Java source code.
Javac is the Java compiler which converts java code into bytecode.


To run type: java main_method_class where main_method_class is the name of the class which defines main method.

Example:

 Show message on computer screen:


class example1 
{
  public static void main(String[] arguments) 
 {
    System.out.println("Start with our first example of Java programing.");
  }

}

Output :
Start with our first example of Java programing.




Following are the second example of java program that how to print numbers:





Development IDE

Following are two popular open source IDE's:

1. Netbeans IDE
2. Eclipse IDE

Using these IDE's helps you a lot while coding as they offer many useful features such as you can create GUI in Netbeans without writing any code, Netbeans will show you any compilation error before you compile your code and it can also show hints on how to fix that.

Java technology has changed our life as most of the devices we use today includes java that's why to learn java programming is a good thing. Java was developed by Sun Microsystems but now owned by Oracle. Here is a quick java tutorial for beginners, Java is an object-oriented computer programming like C++, if you already know C++ or any other object-oriented language, then it will be easier for you to learn java. Java program consists of classes which contain methods; you can't write a method outside of a class. As we know Objects are instances of classes. Then Consider the following code:

class JavaLang 
{
  //These are attributes
  String lang_name;
  String lang_type;

  // This is constructor
  JavaLang(String n, String t) 
  {
    lang_name = n;
    lang_type = t;
  }

  //main method
  public static void main(String[] args) 
  {

  JavaLang C = new JavaLang("C", "Process Oriented");
    JavaLang Cpp = new JavaLang("C++", "Object oriented");

    //method  Calling
    C.show();
    Cpp.show();
  }

  void show() 
  {
    System.out.println("Language name:"+lang_name);
    System.out.println("Language type:"+lang_type);
  }
}

There is a JavaLang class, we can create instances of the class using "new" keyword. And all programming languages will be instances of this class. We have considered only two attributes language name and type;  There is a constructor method which is invoked when an object of a class is created we use it to name the programming language and its type. "Main" method is a must and acts as a starting point of a program, "show" method is used to print information about programming language object. Class names in Java begin with a capital letter, and if there are more words in class then their first letter will also be capital. For example, MyJavaClass is a class name, and for methods (functions in C or C++) the first letter is small and other words first letter is capital as an example myJava is method name. 


Java program to reverse a number


Following program prints reverse of a given number, i.e., if input is 268 then the output will be 862.

Source code :

import java.util.Scanner;

class Rev
{
   public static void main(String arg[])
   {
      int x, rev = 0;

      System.out.println("Enter a number to reverse");
      Scanner sc = new Scanner(System.in);
      x = sc.nextInt();

      while(x != 0)
      {
          rev = rev * 10;
          rev = rev + x%10;
          x = x/10;
      }

      System.out.println("Reverse of the number is = " + rev);
   }

}


Java program to print prime numbers:


This program prints prime numbers. And also Remember that smallest prime number is 2.

Source code:

import java.util.*;

class PrimeNum
{
   public static void main(String args[])
   {
      int n, status = 1, num = 3;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of prime numbers you want");
      n = in.nextInt();

      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are :-");
         System.out.println(2);
      }

      for ( int count = 2 ; count <=n ;  )
      {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            count++;
         }
         status = 1;
         num++;
      }         
   }

}