Overview on Java Programming Languange - FTSM

Document Sample
Overview on Java Programming Languange - FTSM Powered By Docstoc
   Originally from Sun Microsystem.
   Main components of Java:
     Java   Programming Language
       Programming     language used to write software for the Java
     Java   API
       Rich   and fully featured class library.
     Java   Platform
      A range of runtime environments that support execution of
       software written in Java
Advantages of JAVA
   Java is simple: Java was designed to be easy to use and
    is therefore easy to write, compile, debug, and learn than
    other programming languages. The reason that why Java
    is much simpler than C++ is because Java uses automatic
    memory allocation and garbage collection where else
    C++ requires the programmer to allocate memory and to
    collect garbage.
   Java is object-oriented: Java is object-oriented because
    programming in Java is centered on creating objects,
    manipulating objects, and making objects work together.
    This allows you to create modular programs and reusable
Advantages of JAVA…
   Java is platform-independent: One of the most significant
    advantages of Java is its ability to move easily from one
    computer system to another.
   The ability to run the same program on many different
    systems is crucial to World Wide Web software, and Java
    succeeds at this by being platform-independent at both
    the source and binary levels.
   Java is distributed: Distributed computing involves several
    computers on a network working together. Java is
    designed to make distributed computing easy with the
    networking capability that is inherently integrated into it.
Advantages of JAVA…
   Writing network programs in Java is like sending and
    receiving data to and from a file. For example, the diagram
    below shows three programs running on three different
    systems, communicating with each other to perform a joint
   Java is interpreted: An interpreter is needed in order to run
    Java programs. The programs are compiled into Java
    Virtual Machine code called bytecode. The bytecode is
    machine independent and is able to run on any machine that
    has a Java interpreter. With Java, the program need only
    be compiled once, and the bytecode generated by the Java
    compiler can run on any platform.
Advantages of JAVA…
   Java is secure: Java is one of the first programming
    languages to consider security as part of its design.
    The Java language, compiler, interpreter, and
    runtime environment were each developed with
    security in mind.
   Java is robust: Robust means reliable and no
    programming language can really assure reliability.
    Java puts a lot of emphasis on early checking for
    possible errors, as Java compilers are able to
    detect many problems that would first show up
    during execution time in other languages.
Advantages of JAVA…
   Java is multithreaded: Multithreaded is the
    capability for a program to perform several tasks
    simultaneously within a program. In Java,
    multithreaded programming has been smoothly
    integrated into it, while in other languages,
    operating system-specific procedures have to be
    called in order to enable multithreading.
    Multithreading is a necessity in visual and network
Java Platform
   JVM
     Java Virtual Machine
     The emulation of a hardware device

   JRE
     Java  Runtime Environments
     Include core classes from the Java API.
     J2SE (Java 2 Platform, Standard Edition)
     J2EE (Java 2 Platform, Enterprise Edition)
     JSP (Java Server Pages)
     etc
Java Network Programming
   Network clients
   Games
   Software agents
   Web applications
   Distributed systems.
Development Tools
   Integrated Development Environments (IDE)
     Borland  Jbuilder
     Symantec Visual Café

     Visual Age

     Visual J++

     Kawa

     Blue J

     Notepad

   Java System Development Kits (Java SDK)
Writing and Compiling JAVA
   Setting the path and classpath
     path=c:\jdk1.6.0_14\bin

     setclasspath=C:\jsdk2_1-win\jsdk2.1;C:\jswdk1_0_1-
      C:\jdk1.6.0_14\jre\lib; C:\jdk1.6.0_14\jre\lib\ext;
      C:\TK6224 Network Programming\;
   Write Programs
     Beware  of file name and class name
     File name could have different class name.

     The JAVA compiler should produce class name.

     Class name is important file in running JAVA programs
Writing and Compiling JAVA
   Compile
     javac  yourprograms.java
     If there are errors
         Correctthe error
         Compile the program once again

       if there are no errors
         Make  sure the class name.
         yourprograms.class (case sensitive)

   Run
     javayourprograms
     Options are ready for expert users.
Writing and Compiling JAVA
   Different setting for Applet
     javac yourapplet.java
     write html file for applet

     appletviewer or using web browser

   Different setting for JAVA network programming
     ClientServer – tomcat, client site, server site
     Socket Programming – socket in java

     Java Servlet – deal with database, JDBC

     RMI – rmic, rmiregistry, client, server,
Writing and Compiling JAVA
   JAVA Network Security
     https

     Security API.
     Certificate Agents, Certificate Authority.

     Timestamp.

     SSL, SSH
Sample of Java Program
public class Fibonacci {
  public static long fib(int n) {
      if (n <= 1) return n;
      else return fib(n-1) + fib(n-2);
  public static void main(String[] args) {
      int N = Integer.parseInt(args[0]);
      for (int i = 1; i <= N; i++)
          System.out.println(i + ": " + fib(i));
Sample of Java Program …
   Compile
     Set the path and classpath.
     javac Fibonacci.java

     This produced Fibonacci.class

   Run the program
     java   Fibonacci
   Very limited size of integer.
   Rewrite the program when deals with bigger size of
Time is NOW
import java.util.*;
public class Now {
  public static void main(String[] args) {
    Date now = new Date();
    long nowLong = now.getTime();
    System.out.println("Value is " + nowLong);
Time IS NOW (Friendly Version)
import java.util.*;
import java.text.*;
public class NowString {
  public static void main(String[] args) {
    Date now = new Date();
    DateFormat df = DateFormat.getDateInstance();
    String s = df.format(now);
    System.out.println("Today is " + s);
   Simple Applet
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class randomDots extends Applet implements ActionListener
   Button drawButton;
   public void init() {
          // create the button widget
          drawButton = new Button("Draw");

          // add it to the applet gui

          // register a listener for the button

   } // end of init
   Simple Applet
public void paint( Graphics g ) {
      int x, y, r;
      for (int i=0; i < 5000; i++) {
                 // generate a random point to be the center of the circle
                 // restrict it to beween 0 and 200 (the size of the applet window)
                 x = (int) (Math.random()*400);
                 y = (int) (Math.random()*400);

                // select a random size of the radius (make it in the range 10..40)
                r = 10 + (int)(Math.random()*30);

                // draw the circle
                drawCircle(g, x, y, r);
} // end of paint
Simple Applet
 private void drawCircle(Graphics g, int x, int y, int r) {
       // draws a circle at x,y of radius r in the graphics window g
       // chooses a random color for it as well
       int red, green, blue;

       // choose the rgb values
       red = (int) (Math.random() * 256);
       green = (int) (Math.random() * 256);
       blue = (int) (Math.random() * 256);

       // set the foreground to the seledted color
       g.setColor(new Color(red, green, blue));

        // draw the ciclre
        g.fillOval(x-r, y-r, 2*r, 2*r);
 } // end of drawCircle
Simple Applet

public void actionPerformed(ActionEvent e) {
   // Nothing much to do here except repaint, for now
        if (e.getSource() == drawButton)
   } // end of actionPerformed
} // end of applet
HTML File for Applet
<APPLET CODE="randomDots.class" WIDTH=400

Shared By: