Friday, January 25, 2013

Introduction to Java Programming : Simple Java Program


Simple Java Program

In this section, our plan is to lead you into the world of Java programming by taking you through the three basic steps required to get a simple program running. The Java system is a collection of applications not unlike any of the other applications that you are in touch to using (such as your word processor, e-mail program, or Internet browser). As with any application, you need to be sure that Java is properly installed on your computer. You also need an editor and a terminal application. Here are three steps to make basic application.

  1. Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java.
  2. Compile it by typing "javac MyProgram.java" in the Command Prompt(cmd).
  3. Run (or execute) it by typing "java MyProgram" in the Command Prompt(cmd).
Creating a Java program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need to write sequence of characters systematicall just like we write in e-mail. HelloWorld.java is an example program. Type these character into your text editor and save it into a file named HelloWorld.java

public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello World !!!");
   }
}

Note. It is compulsory to give program name and class name which contain the public static void main method same.

Compiling a Java program. At first, it might seem to you as though the Java programming language is designed to be best understood by the computer. Actually, the language is designed to be best understood by the programmer (that's you). A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version). To compile go to the path of the file and type boldfaces text bold boldfaced text below at the cmd.
 
javac HelloWorld.java

If you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above.

Executing a Java program. Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following at the cmd:

java HelloWorld

If all goes well, you should see the following response
Hello World !!!
  • Understanding a Java program. The key line with System.out.println() send the text "Hello World !!!". When we begin to write more complicated programs, we will discuss the meaning of public, class, main, String[], args, System.out, and so on.
  • Creating your own Java program. For the time being, all of our programs will be just like HelloWorld.java, except with a different sequence of statements in main(). The easiest way to write such a program is to:
    • Copy HelloWorld.java into a new file whose name is the program name followed by .java.
    • Replace HelloWorld with the program name everywhere.
    • Replace the print statement by a sequence of statements.
Errors.
Most errors are easily fixed by carefully examining the program as we create it, in just the same way as we fix spelling and grammatical errors when we type an e-mail message.
  • Compile-time errors. These errors are caught by the system when we compile the program, because they prevent the compiler from doing the translation (so it issues an error message that tries to explain why).
  • Run-time errors. These errors are caught by the system when we execute the program, because the program tries to perform an invalid operation (e.g., division by zero).
  • Logical errors. These errors are (hopefully) caught by the programmer when we execute the program and it produces the wrong answer. Bugs are the bane of a programmer's existence. They can be subtle and very hard to find.
One of the very first skills that you will learn is to identify errors; one of the next will be to be sufficiently careful when coding to avoid many of them.

Some Idea regarding Java Program:
Hey guys, Here I have provided some more basic examples, kindly follow it and check output,

Swap Two Number Example using Core Java:

  public class SwapElementsExample {
   
          public static void main(String[] args) {
                 
                  int num1 = 10;
                  int num2 = 20;
                
                  System.out.println("Before Swapping");
                  System.out.println("Value of num1 is :" + num1);
                  System.out.println("Value of num2 is :" +num2);
                 
                  //swap the value
                  swap(num1, num2);
          }
   
          private static void swap(int num1, int num2) {
                 
                  int temp = num1;
                  num1 = num2;
                  num2 = temp;
                 
                  System.out.println("After Swapping");
                  System.out.println("Value of num1 is :" + num1);
                  System.out.println("Value of num2 is :" +num2);
                 
          }
  }

Reverse No. Example Using Core Java

  public class ReverseNumber {
   
          public static void main(String[] args) {
                 
                  //original number
                  int number = 1234;
                  int reversedNumber = 0;
                  int temp = 0;
                 
                  while(number > 0){
                         
                          //use modulus operator to strip off the last digit
                          temp = number%10;
                         
                          //create the reversed number
                          reversedNumber = reversedNumber * 10 + temp;
                          number = number/10;
                           
                  }
                 
                  //output the reversed number
                  System.out.println("Reversed Number is: " + reversedNumber);
          }
  }

No comments:

Post a Comment