Named (eventually) after a type of coffee, Java embraces the Object-Oriented Programming paradigm (OOP), a current, state-of-the-art way of developing computer programs that model real life.
Based on C++, it was originally intended as a language with which to develop software for white goods (washing machines, fridges, microwaves, stoves etc). As such it was designed to be run on many different "platforms" i.e. to run as different machine code on different computers.
This was soon seen to be a perfect way for running small applications (applets) associated with web pages running on different computers.
Java also embraces all of the structured programming constructs proposed by Bohm and Jacopini. When writing methods (code to perform varying actions) for objects, structured programming principles should be applied.
To create a program in Java, you usually make use of the following software components...
When programming in Java, you can create 2 different types of programs...
We will only be looking at applications in this subject.
Two main file types are used when developing Java programs:
Java source code files are text file that contains human readable Java code (see Example Java Programs further down). Java source code files usually have the extension ".java" to make it easier to identify the files. For example, a Java source code file might be called HelloWorld.java or AddTwoNumbers.java. The source code file is created and modified with a text editor. Before the program can be run it needs to be compiled into byte code.
Byte code files are produced by a Java compiler. When a Java source code file (filename.java) is successfully compiled a byte code file (filename.class) is produced . Byte code files have the extension ".class". Byte code files are binary files, they are not readable by humans. The byte code file is interpreted by the Java Virtual Machine (JVM), which interprets the instructions and executes them.
Developing a Java program typically consists of an edit-compile-run cycle with some steps repeated depending on whether there are syntax and/or logic errors.
I suggest that you use nedit to create and edit Java programs, and use the preferences discussed in lecture and tutorial 18. The editor is usually run in the background (follow the command by an ampersand).
The file can be left open in the editor when compiling, just make sure the file is saved before compiling as the compiler uses the file as it is on disk
nedit filename.java
nedit HelloWorld.java nedit HelloWorld.java &
A compiler is used to compile the source code into byte code.
javac filename.java
javac HelloWorld.java
Successful compilation of the above file would produce the file: HelloWorld.class.
The java interpreter (JVM) is used to execute (run) a java program. The interpreter uses the ".class" file produced by the compiler. The ".class" extension must not be included in the filename when using the command.
java filename
java HelloWorld
The above command would interpret (run) the file HelloWorld.class.
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
nedit HelloWorld.java& javac HelloWorld.java java HelloWorld
addTwoNumbers()
Input number1, number2
total = number1 + number2
Display total
STOP
Data Dictionary
| Name | Data Type | Description |
| number1 | Integer | The first number to be added |
| number2 | Integer | The second number to be added |
| total | Integer | The total of number1 and number2 added together |
// Name: AddTwoNumbers.java // Author: Tim Whitfort // Purpose: Add two numbers together displaying the sum.
// This is necessary so the program knows what JOptionPane is import javax.swing.JOptionPane;
// Declare the class AddTwoNumbers. We will treat this as the program name.
public class AddTwoNumbers
{
// The main procedure (method)
public static void main(String args[])
{
// Variable declarations
int number1;
String number1Str;
int number2;
String number2Str;
int total;
// Input the first number as a string storing it in number1Str
number1Str = JOptionPane.showInputDialog("Number 1 ?");
// Convert number1Str to an integer and store it in number1
number1 = Integer.parseInt(number1Str);
// Input the second number as a string storing it in number2Str
number2Str = JOptionPane.showInputDialog("Number 2 ?");
// Convert number12tr to an integer and store it in number2
number2 = Integer.parseInt(number2Str);
// Add the two numbers together
total = number1 + number2;
// Display the sum of the two numbers
System.out.println("total = " + total);
// Indicate normal successful completion of the program
System.exit(0);
}
}
nedit AddTwoNumbers.java javac AddTwoNumbers.java java AddTwoNumbers
The class name must be the same as the file name, with the .java left off. e.g. if the file name is CalcTax.java, then the class name must be CalcTax
Java is case sensitive. e.g. number1 and Number1 would be considered different variables.
Comments to end-of-line start with //.
{} are used to show the start and end of a group of statements.
Variable declaration have the data type first, then the variable name. The data types we will use are: int, double and String (The data type String must have a capital S).
Semicolons (;) are important. There are places where they must be and others where they mustn't be.
There is no way to input numbers directly into a numeric variable. JOptionPane.showInputDialog allows a string to be input. If the value input is intended to be used as a number, then the string needs to be converted to an appropriate numeric data type. To convert from a string to an integer Interger.parseInt is used. JOptionPane.showInputDialog displays an input dialog (window) on the screen, and stores the string input.
System.out.println is used to display text to the console window. println stands for print line (it is a lowercase L not a capital i or one). "+" is used to concatenate (string together) strings (similar to & in Basic).