Java Tutorial

What is Java

  • formerly known as Oak
  • not JavaScript - a scripting language used with Netscape 2.0+
  • Java is a simple robust, object-oriented, platform-independent, multithreaded, dynamic, general-purpose programming environment. It's best for creating applets and applications for the Internet, and intranets.
  • HotJava is a world wide web browser written in Java

  • Motivation

  • Simple
  • Efficient
  • Small
  • Architecture Neutral
  • Secure
  • Robust
  • Multi-Threaded

  • Example

    int sum(int arr[]) {
        int result = 0;
        for (int i=arr.length; --i >= 0; ) {
           results += arr[i];
        }
        return result;
    }
    

    History

  • 1990 - Started by James Gosling
  • 1991 - Consumer Electronics (PDA)
  • 1993 - Interactive TV (Time-Warner bid)
  • 1995 - Internet & HotJava

  • Syntax

  • Very Similar to C/C++
  • Easy to Learn
  • Fewer Concepts

  • Simple Types

  • boolean (1 bit)
  • byte (8 bits, signed)
  • char (16 bits, unsigned, UNICODE)
  • short (16 bits, signed)
  • int (32 bits, signed)
  • long (64 bits, signed)
  • float (32 bits, IEEE 754)
  • double (64 bits, IEEE 754)
  • declare like "int temperature;"

  • Arrays

  • Arrays are objects
  • Arrays are dynamic
  • Arrays are passed by reference
  • Arrays are bounds checked
  • int month_days[];
    month_days = new int[12];
    month_days[0] = 31;

  • Operators

  • Arithmetic (+ - * / % +)
  • Inc/Dec (++, --)
  • Bitwise (~ & | ^ >> >>> <<)
  • Relational (== != > < >= <=)
  • Boolean Logic (& | & || && !)

  • Control Flow

  • if ( expr ) statement else statement ;
  • for ( expr ; expr ; expr ) statement ;
  • while ( expr ) statement ;
  • do statement while ( expr ) ;
  • switch ( expr ) {
    case literal : statement ...
    ...
    }

  • Strings

  • Strings are objects
  • Strings are 16 bit UNICODE
    System.out.println("Hello World");
    
    String hello = "Hello";
    String world = "World";
    System.out.println(hello + " " + world);
    
    int len = hello.length();
    String str = hello.substring(1, len);
    System.out.println("j" + str);
    
    char ch = hello.charAt(0);
    

  • Advanced Language Topics

  • Threading
  • Networking
  • Packages
  • Input/Output

  • Class Example

    class Car {
        void beep() {
           System.out.println("beep");
        }
        int maxSpeed() {
           return 55;
        }
    }
    
    class Porsche extends Car {
        int maxSpeed() {
           return 200;
        }
    }
    
    Car c = new Car();
    Porsche p = new Porsche();
    
    c.beep();
    p.beep();
    
    int s1 = c.maxSpeed();
    int s2 = c.maxSpeed();
    

    Exceptions

    try {
        int result = a / b;
        if (result > 5) {
           throw new ResultTooLarge(sum);
        }
        return result;
    } catch (ArithmeticException e) {
        System.out.println("divide by zero");
        return -1;
    }
    

    The "Hello World" Application

    Create HelloWorld.java file

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

    Compile the Class Definition

    javac HelloWorld.java
    

    Run the Class

    java HelloWorld.java
    

    Libraries

  • java.lang - Object, Thread, Exception, String, System, Number ...
  • java.util - Hashtable, Vector, BitSet, Regexp, Date, Cache ...
  • java.io - InputStream, OutputStream, BufferedInputStream ...
  • java.tools - Compilation, Debugging, Documentation ...
  • java.awt - Abstract Windowing Toolkit
  • java.net - Networking

  • The "Hello World" Applet

    Create HelloWorld.java file

    import java.applet.*;
    import java.awt.*;
    import java.applet.*;
    class HelloWorld extends Applet {
        public void init() {
            resize(150,25);
        }
        public void paint(Graphics g) {
            g.drawString("Hello world!", 50, 25);
        }
    }
    

    Compile the Source File

    javac HelloWorld.java
    
    This creates HelloWorld.class
    

    Create an HTML file that includes the Applet

      
    
      A Simple
    Program   
    Here is the output of my program:
    Hosted by www.Geocities.ws

    Load the html file

    file:/java/mycode/hello.html in Netscape
         or
    appletviewer hello.html
    

    the Java Development Kit (JDK)

  • For Windows95
  • http://java.sun.com/JDK-1.0/index.html
  • ftp://ftp.javasoft.com/pub/JDK-1_0_1-win32-x86.exe
  • ftp://www.blackdown.org/pub/Java/pub/JDK-1_0_1-win32-x86.exe
  • Download is ~4,391,137 bytes
  • Remobe previous version of JDK
  • Unpack in the root directory
        c:
        cd \
        \netscape\program\JDK-1_0_1-win32-x86.exe
    
  • Add the c:\java\bin directory to your path
  • Try it out
        c:
        cd \java\demo\TicTacToe
        appletviewer example1.html
    

  • Learning More

  • USENET News Discussion in comp.lang.java
  • Documentation at http://java.sun.com/doc/programmer.html
  • FAQ at http://sunsite.unc.edu/javafaq/javafaq.html
  • Books
    • The Java Handbook, by Patrick Naughton
    • Java in a Netshell, by David Flanagan
    • Hooked on Java
    • http://sunsite.unc.edu/javafaq/books.html

  • Java Future

  • Java is slow - may be fixed by just-in-time compilation
  • Battle between Sun and Microsoft
  • Haven't actually seen a serious application
  • JDBC - Java database API
    • The JDBC API defines Java classes to represent database connections, SQL statements, result sets, database metadata, etc. It allows a Java programmer to issue SQL statements and process the results.
    • JavaSoft is writing a reference implementation of JDBC on the Microsoft ODBC interface. This will allow JDBC apps to run on existing ODBC drivers (June).
    • http://splash.javasoft.com/jdbc/
  • Sean Fuller's Pages


    1