Notes Java J1

Math 10 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 10 Homepage | Return to Notes Listing Page]

Contents


Introduction to Java

Java was developed by James Gosling at Sun Microsystems in 1990. The languages was based on C and C++ but went a step further in incorporating the Object-Oriented paradigm, so that classes are omni-present and classes that are useful for graphics on web pages are built into the language.

After a few years of internal use at Sun, Java was released on the web in 1993 and began to get public attention around 1995 with the official release of version 1.0. Version 1.1 became available in 1997. Note that there are enough differences between the versions that newer code will not run on older web browsers which are Java-enabled.

Java programs are activated in two stages. First, one compiles the Java code which results in a "class" file (written in byte-code). This compiled code can be interpreted by any machine that has a Java intepreter (or, if written for a web page, by a web browser). Because Java is not compiled directly for a specific platform, it is much slower than other languages. Because Java can be run on a variety of machines, it is more versatile than other languages for certain applications.

Applets are Java programs written for linking to web pages and useful especially for graphical features.

Writing Java Code

The logical and mathematical core of Java code is oftentimes indistiguishable from C/C++ code. For example, the following for loop will run correctly in either language:
     sum = 0;
     // Test loop
     for(int i = 1; i < 10; i++)
         {
	   sum = sum + 1.0/i ;
	 }
There are some major differences in the languages, however.

Embedding Code in a Main Program

The following is a sample Java program into which the code which appears above has been embedded. Output is achieved by a call to the method System.out.println with the desired output included in parentheses. As is seen in the second output line, several items may be included together within the parentheses, joined by a plus sign. An alternative method, System.out.print may be used instead, which does not advance to the next line after outputting the data.
  class Testjava2 {
    public static void main (String[] args)
    {
      double sum;
      System.out.println("Test program");
      sum = 0;
      // Test loop
      for (int i = 1; i < 10; i++)
        {
	  sum = sum + 1.0/i;
        }
      System.out.println("Sum =" + sum);
    }
  }
The following is a sample C++ program equivalent to the Java program. Notice that the main differences consist in the presence of the #include statement, the main statement, and the output functions.
  #include<iostream.h>

  int main ()
    {
      double sum;
      cout << "Test program" << endl;
      sum = 0;
      // Test loop
      for (int i = 1; i < 10; i++)
        {
  	  sum = sum + 1.0/i;
        }
      cout << "Sum =" << sum << endl;
      return 0;
    }

Running Self-Standing Java Code

A self-standing Java program is compiled and executed according to the following steps.


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1998, Dennis C. Smolarski, SJ, All rights reserved. Last changed: 6 December 1998.