Sunday, July 15, 2012

Java Introduction




 Introduction

  • Java was initially designed to solve problem for cable TV switch by SUN Microsystems Engineers.
  • The team was lead by Patrick Naughton and James Gosling. They have to build a solution which will be platform independent. It was initially named as ‘OAK’ then changed as Java. Java version 1 was released in 1996.
      Java is an extremely popular language because it has the following features.
  •  Simple.
  •  Object Oriented.
  •  Network Programming.
  •  Robust.
  •  Secure.
  •  Multithreaded.
  •  Platform Independent.
     It’s Simple
      Java programming language is very easy to use syntax. It has a very well document set of API
      (Application Programming Interface).
      Syntactically it has similarity with C++ so lot of C++ developers can easily switch into this new language
      and can make it extremely popular.
      
      It’s Object Oriented Programming Language

      Java is an object oriented programming language that consists of hierarchical classes and well defined 
      cooperating objects. It supports inheritance and polymorphism. 

     It’s a robust Language
     Java is a robust language .It has many data structures which are already implemented. You don’t have  to
      worry about memory allocation, memory freeing or memory leakage. Java Collections, Array, String are 
      implemented with highly optimized code.   

    It’s a secure Language
    Java is a highly secure programming language as it runs over JRE(Java Runtime Environment) so no direct calls were made outside these boundary .
    Programs are prohibited from many activities, including
  •  Reading or writing to the local disk
  •  Making a network connection to any host, except the host from which the applet came
  •  Creating a new process
  •  Loading a new dynamic library and directly calling a native method 
It’s a multi-threaded programming language

Java supports multithreading and which is very easy to implement. Using Java you can take advantage of multi processor system. Code for using multithreading remains the same but the performances varies according to the server specification. Java is widely accepted in server development for its multithreading feature.

It’s platform independent
Java is platform independent so you don’t need to write different program for  Unix and Windows environment. The language gets an edge over C++ here.

The common abbreviations in Java 


Java Programming environment setup

  • To start Java Programming you need a JDK (Java Development Kit) and to run you Java application you need only a JRE (Java Runtime Environment).


    • The most popular editors for development are Eclipse, Netbeans and IntelliJ.
    • Setup Java bin path in you environment variable so that Java command will be available in your system everywhere.
    • Steps for  setting path My computer (r-click) --  > properties -- > Advanced tab  -- > Environment variables -- >System variables  -- > edit path variable -- > add path as  (path of java installed)/bin




    “Hello world” Program in Java
    Create a file name HelloWorld.java and inside copy the following lines of code

    /**
     * The HelloWorld class implements an application that simply prints
     * "Hello World!" to standard output.
     */
    class HelloWorld {
     public static void main(String[] args) {
      
      // Display "Hello World!".
      System.out.println("Hello World!"); 
      
     }
    }
    
     


    How to Compile a Java File

    Javac  command is used to compile a Java code.

    Start command prompt window in the folder where you created the file name
        HelloWorld  .java
    Now write as Javac HelloWorld.java 
       compilation is complete .
      Enjoy …………
            You have successfully compiled your first  Java Program






    How to Run a Java Class

    Java  command is used to compile a Java code.
    Start command prompt window in the folder where
     you created the file name HelloWorld.java and compilation unit named HelloWorld.class is created.
    Now write as Java HelloWorld  -- running your first Java program is complete and program output as
    Hello World!" as you expected.
        Again Enjoy the moment …………
                      You have successfully run your first
                    Java Program . Consecutively you made two
                     success.





    JVM is a compiler as well as an interpreter


    •  In the Java programming language, all source code is first  written in plain text files ending with the   .java extension.     
    • Those source files are then compiled into .class files by the javac compiler.      
    •   A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine(Java VM). 
    • The java launcher tool then runs your application with an instance of the Java Virtual Machine.

    Closer Look at the "Hello World!“
     Now that you've seen the "Hello World!" application (and perhaps even compiled and run it), you might be wondering how it works. Here again is its code:
    class HelloWorldApp
    {
          public static void main(String[] args)
           {
               System.out.println("Hello World!");  // Display the string.
            }
    } 
    
    

           The "Hello World!" application consists of three primary components: source code comments, the HelloWorldApp class definition, and the main method. The following explanation will provide you with a basic understanding of the code, but the deeper implications will only become apparent after you've finished reading the rest of the tutorial.

    Source Code Comments
        The following bold text defines the comments of the "Hello World!" application:
    /**
     * The HelloWorldApp class implements an application that simply prints "Hello World!" to standard output.
     */
    class HelloWorldApp
    {
          public static void main(String[] args)
           {
               System.out.println("Hello World!");  //Display the string.
            }
    } 
    
    

    Comments are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments: 
    • /* text */ The compiler ignores everything from /* to */.
    •  /** documentation */ This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The javadoc tool uses doc comments when preparing automatically generated documentation. For more information on javadoc.
    • // text The compiler ignores everything from // to the end of the line.  

    The HelloWorldApp Class Definition

    The following bold text begins the class definition block for the "Hello World!" application:
     
    /**
     * The HelloWorldApp class implements an application that simply prints
     * "Hello World!" to standard output.
     */
    class HelloWorldApp
    {
          public static void main(String[] args)
           {
               System.out.println("Hello World!");  // Display the string.
            }
    } 
    
    
    

    • As shown above, the most basic form of a class definition is:      class name { . . . }
    • The keyword class begins the class definition for a class named name, and the code for each class appears between the opening and closing curly braces marked in bold above

    The main() Method

    • In the Java programming language, every application must contain a main method whose signature is:     public static void main(String[] args)
       The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".
    • The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
    • The main method accepts a single argument: an array of elements of type String.
    • public static void main(String[] args)
    • This array is the mechanism through which the runtime system passes information to your application. For example:   
    • java MyApp arg1 arg2  



No comments:

Post a Comment