01 IntroToJava
01 IntroToJava
Java was first developed in 1991 for small appliances. It is an object-oriented programming
language. It is platform independent which means you can a Java program on one platform (e.g.
Windows) and run it on another (e.g. Linux or Mac).
It can be used to create both web and desktop applications and the code itself looks similar to that
of the C++ language.
Like C++, Java provides a library of pre-defined functions which programmers may used in building
their programs. This library is called the Application Programming Interface (API) and contains
more than 2700 classes. Every Java program utilizes code in the Java API.
The JDK (Java Development Kit) contains the fundamental tools needed for compiling, running,
documenting and archiving Java code:
Note that JDK is an older name for the Java SDK (Software Development Kit).
The Java Software Development Kit (SDK) contains three tools: a Java compiler, a Java
Virtual Machine, and the API. It can be used to create and run Java programs.
The Java Runtime Environment (JRE) contains two tools: a Java Virtual Machine and the
API. Only used to run existing java programs. There is no compiler. `Therefore, it cannot
be used to create programs – only run existing ones.
The diagram below (by Dr Colin Depradine), outlines the steps in the compilation process.
.java File
3. The Java compiler (javac.exe) checks the code for syntax errors.
5. This .class file is passed to the Java Virtual Machine (java.exe) for execution.
For more details about installing and running Java programs, ensure that you work through the
Running Java Programs handout.
Point to Note
When naming files, they must have a .java extension. Example: HelloWorld.java
When they are compiled, a .class file will be created. Example: HelloWorld.class
Files names should match the name of the class which contains the main function.
A java program is essentially a class definition with a function (method) called main. This main
method is the entry point for every Java program. The simple program below will output a “Hello
WORLD” message to the screen.
Your program may be composed of multiple class definitions. However, there can be only one which
contains the main method.
There are some naming and coding conventions which you will be expected to follow. The main
points are:
All method names start with a lowercase letter followed by uppercase for internal words.
This is also known as lower camel case.
Example: setBookTitle()
Constants are written entirely in uppercase letters. If the constant name is made up of
multiple words, they are separated by underscores.
Example: MAX_LIMIT
The points outlined above are only a sample. Please ensure that you go through the Naming
Conventions and Coding Conventions handouts for further details.
Recall:
An array is a container that holds a fixed number of values all of the same type.
Once created, the length of an array is fixed i.e. the size is static.
Values in an array are stored in consecutive, back-to-back locations in the computer’s
memory.
Each location or element in the array has the same data type, therefore they occupy the
same amount of space.
Each element in an array can be referenced using a unique number called a subscript or
index, which indicates the element’s position in the array.
All array subscripts are positive integers.
The first element is always assigned subscript 0.
The last subscript is always one less than the size of the array.
Each element in an array is referenced using the array name and the element’s subscript.
An array must be declared before it can be used. To declare an array, you need to specify the data
type, followed by square brackets and the array name.
You can also place the brackets after the array's name. However, note that this form is discouraged
as the brackets identify the type and should appear with the type description.
Unlike C++, declaring an array in Java does not create it (i.e. does not allocate memory
space to it).
If this statement is missing, then compilation fails and an error, like the following, is printed:
Array elements may be accessed in a similar fashion to that of C++, using the array name and the
subscript.
Alternatively, you can create and initialize an array at the same time as follows:
//this array will be set to a size of 6
int[] anArray = {
100, 200, 300,
400, 500, 600,
};
Note that in this case, the length of the array is determined by the number of values
provided. The size of the array will be fixed once set.
prices[0][0] = 3.75;
prices[0][1] = 2.56;
prices[2][2] = 4.19;
prices[0][3] = 1.99;
The code above creates a 2-dimensional array which has 3 rows and 4 columns.
Each position in the 2-D array will hold a floating point value.
Elements are accessed using the format: array-name[row-index][column-index].
Every element will be initialized to the default value of 0.0.
The 2-D array conceptually looks like the diagram below:
0 1 2 3
System.out.println(greatGuys[0][0] + greatGuys[1][0]);
System.out.println(greatGuys[0][1] + greatGuys[1][1]);
System.out.println(greatGuys[0][2]);
The code above creates a 2-dimensional array which has 2 rows and 3 columns.
Each position in the 2-D array will hold a String value.
Elements are accessed using the format as outlined in Example 1.
The 2-D array conceptually looks like the diagram below:
0 1 2
1 Flintstone Rubble
It may be useful to know the following. Please look up further details on their usage. There are
many useful operations found in the java.util.Arrays class.
There is a built-in length property which can be used to determine the size of an array.
System.out.println(myArray.length);
arraycopy(): (found in the System class) can be used to copy data from one array to the
next. You can specify which subset of elements to copy.
CopyOfRange(): copy from one array to the next but does not require the creation of a
destination array before calling the method.
binarySearch()
Screen Output
There are three common functions used to output data to the screen in Java:
println: Output is displayed to the screen and then a carriage return. Any further output
will be done on the next line.
print: Output is displayed to the screen. Any further output will be made on the same line.
There is no carriage return.
printf: Provides the programmer with the ability to format output. Any further output will
be made on the same line unless a carriage return is specified in the formatting.
Output: Output:
Java is Java is awesome!
Awesome!
Formatted Output
The printf method can be used to give output a specific format and works in a similar way to the
printf function found in the C++ language.
The first argument of the function is always a format string (containing format specifiers)
followed by the values to be output in the format specified.
The number of format specifiers and the number of arguments must match.
Note: The escape sequence \n may be used to indicate a line break. However, it is preferable to
use %n. What happens when \n is used may be system dependent but %n should always mean a
new line on any system.
Java includes a Scanner class which allows for simple keyboard input.
You can now use methods in the Scanner class via your myScanner object to read data from the
keyboard. There are various methods for different types of data. A few are outlined below.
String Concatenation
System.out.println(100 + 42);
Output: 142 {addition}