Simple I/O
Whilst obtaining output from Java programs is relatively easy, getting input from the keyboard presents a few problems.
To avoid these problems, we shall be avoiding prompted keyboard input for a while and make use of command line arguments instead.
Strings and output
As most input to and output from a computer is normally in the form of strings of characters, it is necessary to take a short look at the String class and some of its operations.
In Java, character strings are instances of a class called String and strings can be defined between double quotes, hence :-
String message = "Welcome to Java";
declares a variable message to be of type String and initialises it to the character string "Welcome to Java".
To output such a string to the VDU screen we write :-
System.out.println(message);
We could have achieved the same result by :-
System.out.println("Welcome to Java");
where the string is actually coded as a constant in the call of the method println
In these last two lines of code, System refers to the class System and System.out refers to an object contained within the System class called out. out is a PrintStream object, that is, it represents a stream of characters. Rather like a tube, you can stuff characters into a PrintStream object and they appear at the other end in the same order.
In this case, the PrintStream object out is mapped onto the UNIX stdout. So anything sent to System.out appears on the VDU screen.
The way you stuff characters into this stream is with methods such as println - short for print line, or print.
These do the same thing except that println puts a carriage return
at the end of the output whilst print doesn't. E.g.
System.out.println("Good");
System.out.println("bye");
Results in the output :-
Good bye
Whilst :-
System.out.print("Good");
System.out.println("bye");
produces :-
Goodbye
and :-
System.out.print("Good");
System.out.println();
System.out.println("bye");
also produces :-
Good bye
The methods print and println will also handle numbers, e.g. :-
int value = 5;
System.out.println(value);
Automatically converting the numeric value to a string representation.
We frequently want to combine text and numbers on output and this could be done as follows :-
int value1 = 5, value2 = 10;
int sum = value1 + value2;
System.out.print("The sum of ");
System.out.print(value1);
System.out.print(" and ");
System.out.print(value2);
System.out.print(" is ");
System.out.println(sum);
which produces :-
The sum of 5 and 10 is 15
But we can use the joining or concatenation (pasting together) operator + to do this more simply :-
int value1 = 5, value2 = 10;
int sum = value1 + value2;
System.out.println("The sum of " + value1 + " and " + value2 + " is " + sum);
Simple operations on Strings
The String class provides a large number of operations that can be performed on strings. The following are just a few useful ones :-
int len;
String text = "Hello";
len = text.length(); //puts the value 5 into variable len
char ch;
String text = "Hello";
ch = text.charAt(0); //puts the first character H into ch.
//other characters can be accessed as charAt(1), charAt(2) etc
Note that with strings you cannot use == for testing for equality.
String s1 = "Hello";
if ( s1.equals("Hello"))
System.println("s1 equals Hello");
A more general comparison allows two strings to be compared for equal, greater than or less than :-
String s1 = "Hello";
String s2 = "Goodbye";
if ( s1.compareTo(s2) == 0 )
System.println("s1 equals s2");
if ( s1.compareTo(s2) < 0 )
System.println("s1 less than s2");
if ( s1.compareTo(s2) > 0 )
System.println("s1 greater than s2");
Command line input
A simple way to get idata into a program is to use the command line arguments. When a program is run by typing :-
java Classname
addittional values can be appended to the command line, e.g. :-
java Classname fred 15 23.5 bill
These are made available inside the main method as an array of character strings - this is what String [] args represents in the header of the main method.
These can be referred to within the program as - args[0] - the first element of the array = the first command line argument fred as a character string, args[1] - the second element of the array = the second command line argument 15 again as a character string etc.
The number of elements in the array = the number of command line arguments is given by args.length. Hence the following program displays the command line arguments given to it on the screen :-
public class Repeat {
public static void main(String [] args) {
for ( int i = 0; i < args.length; i++)
System.out.println(args[i]);
} //end main
} //end class Repeat
It must be remembered that the array args consists of character strings, so it is not legal to say 2 * args[1] even though the corresponding argument is an integer.
The string must first be converted to the internal representation for integers from character. This is done with a method provided in the class Integer called parseInt(). Hence to input two command line arguments and print their sum :-
public class Addition {
public static void main(String [] args) {
int val1 = Integer.parseInt(args[0]);
int val2 = Integer.parseInt(args[1]);
int sum = val1 + val2;
System.out.println("Sum = " + sum);
} //end main
} //end class Repeat
In this short introduction we have met a number of classes - String, Integer, System, PrintStream and some of the methods within these classes - print, println, parseInt.
Java contains many hundreds of classes, each of which contain many methods. This large collection of code which is immediately available to use forms the standard environment or class library which makes it possible to create complex systems very quickly by reusing existing code.
The next section takes a brief glimpse at this resource.
The standard environment
As mentioned above, Java comes with a very large collection of compinents intended to make the programmers job easier. Unfortunately, this tends to complicate life for novice programmers as there is such a wealth of information to assimilate.
The standard environment is split into a series of packages, each of which contains a large collection of components (classes) which in turn each contain many programs. An example of some of the packages are :-
There are hundreds of packages containing thousands of classes which together contain tens of thousands of methods (programs) and whilst you can write Java programs without knowing much about this environment, to be really effective and wroite complex programs quickly you have to develop a working knowledge of the standard environment at some level.
From time to time you should take a look at the standard environment documentation which is referenced on the course home page and is referred to as Java2 SDK.