Java is an object-oriented language: the world is modelled as objects, and the methods (operations) you can perform on them.
The syntax for performing the method meth with arguments args on the object obj is: obj.meth(args)
Examples of method calls on strings:
s1.length() // the length of s1
s1.charAt(4) // the 5th character of s1
s1.equals(s2) // whether s1 and s2 are equal
s1.equalsIgnoreCase(s2) s1.substring(2,10) // the substring of s1 which starts
// at the 3rd character and ends at the 10th character
s1.substring(4) // the substring of s1 which starts at the 5th
// character and continues to the end
s1.indexOf(s2) // the index in s1 of the first character of a
// matching substring s2
s1.toLowerCase() // s1 in lower caseNote: There are no methods in the String class which change a string. If you want a different string to the ones you have already, you have to create a new one. String objects are said to be immutable. That's rather inefficient (because it can mean the interpreter has to copy strings around a lot), but it's safer when you pass objects as arguments to methods, as explained below.
The methods applicable to arrays (such as a method for accessing or changing a cell) are built-in to the core Java language, rather than using the obj.meth() syntax. But arrays behave like objects in every other way.
Arrays are not immutable. You can change arrays after they have been created.
Data types are stored as values, while the objects are stored as references (also known as pointers in other languages). This can give rise to some odd behaviour if you're not careful. For example, take a look at the ValRef.java piece of code and answer the following questions:
Another area where you have to know the difference between having your data stored as a value or as a reference, is when you assign objects. The code in Copying.java illustrates this.
Answer the following questions (on paper, with diagrams):
The same phenomenon is exhibited when you pass an object as an argument to a method. The object is assigned to the parameter of the method. If the object has methods that can change it, then the body of the method could change the original object by calling those methods on the parameter.
Since immutable objects like strings don't have any such methods, passing a string argument to a method is safe: the method cannot change the string. Arrays are mutable, so a method which gets an array as its parameter could change it.
Objects such as strings are defined in classes, such as the String class. A vast collection of 1800 classes are privided in what is known as the Java API (application program interface). The API is divided into packages, of which these are the most important:
java.io classes for input and output streams and files.
java.lang the core language, e.g. Math class
java.util utility classes, such as the ArrayList class
java.awt abstract window toolkit
A package is thus a collection of related classes, and is a convenient way to organise & distribute classes. The String class lives in java.lang. This core package and is the only one which does not have to be explicitly imported in order to be used. The full name of the ArrayList class in the java.util package is java.util.ArrayList. By including the statement import java.util.ArrayList or import java.util.* at the top of a .java file, we can refer to it simply as ArrayList.
Integer, Double etc. The primitive types int and double are not objects. Sometimes it is useful to have versions of them which are objects. (Reasons why we would want this include: being able to store them in object stores (the "collection classes"); being able to pass them to methods which take object arguments; etc.) Objects of the class Integer store an integer, and we can convert between ints and Integers as follows:
int x=6; Integer in = new Integer(x); int y = in.intValue();
There's an absurdly complicated set of over 60 classes in java.io which deal with file input and output. In this section, we just give some common idioms.
import java.io.*; ... BufferedReader kbd = new BufferedReader( new InputStreamReader( System.in ) );If you want to read a number from the keyboard, use Integer.parseInt(s) or Double.parseDouble(s).
try { String s = kbd.readLine();
}
catch (IOException e) { . . . }
BufferedReader myfile = new BufferedReader( new FileReader(filename) );
int c = myfile.read();
End of file is indicated by read() returning -1.
BufferedWriter myfile = new BufferedWriter( new FileWriter(filename) );
myfile.write(c); // int c
Set up myfile as above; and use
String line = myfile.readLine();
End of file indicated when readLine() returns null.
myfile.writeLine(s); // String s
DataInputStream dis = new DataInputStream(new FileInputStream(filename));
double d = dis.readDouble(); // reads 8 bytes as a double int i = dis.readInt(); // reads 4 bytes as an int
String s = dis.readLine();
char c = readChar();
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename));
dos.writeDouble(d);
Also supports writeLine and writeByte()
import java.net.*;
import java.io.*;
...
URL url = new URL("http://www.yahoo.com/");
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(url.openStream()));
} catch (ConnectException e) {
System.err.println("ConnectException");
break;
}
...
String s = in.readLine()
Note: if your web set-up involves using a proxy proxyhost on port number
portNumber, then the Java program can be invoked withjava -Dhttp.proxyHost=proxyhost -Dhttp.proxyPort=portNumber yourClass
© 2001 Mark Ryan and Alan Sexton