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 gedit 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
gedit filename.java
gedit HelloWorld.java gedit 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");
}
}
gedit 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 |
// So the compiler knows what the Scanner class does
import java.util.Scanner;
// Class name (a bit like the program name)
public class AddTwoNumbers
{
// Allow data to be read from the keyboard
static Scanner in = new Scanner(System.in);
// main subroutine (method)
public static void main(String[] args)
{
// Declare variables
int number1; // The first number
int number2;
int result;
// Display a prompt to input number1
System.out.print("Number 1 ? ");
// Read in number1
number1 = in.nextInt();
System.out.print("Number 2 ? ");
number2 = in.nextInt();
// Add the to numbers together
result = number1 + number2;
// Display the result
System.out.println(number1 + " + " + number2 + " = " + result);
}
}
gedit 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 followed by a semicolon. 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.
When inputting a value, a prompt is displayed using System.out.print, then the value is read in using Scanner. When reading a String use next(), for integers use nextInt() and doubles use nextDouble(). e.g. name = in.next(); number = in.nextInt(); length = in.nextDouble();
Calculations end with a semicolon.
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).
Written by Tim Whitfort.