Lab 2: Java notes

This page provides some extra notes on Java that may be helpful as you work on Lab 2. The sections are:

Calling methods

(top)

Some operations in Java are indicated by symbols, which we call operators; those include the arithmetic operators we have seen in class and the relational (comparison) operators.

A more general way of specifying operations is to give them names; we refer to named operations as methods. When we use one of these operations, we refer to that as calling the method (or, for those who prefer Latinate vocabulary, invoking it). We write a method call be giving the name of the method, following it by a list of parameters in parentheses. For example, if we had a method named foo that takes a String and an int for its parameters, we could write a call as foo("something", 4) Each parameter could be any expression that yields the right type of value; so if we had a String variable s, we could make the call foo(s, 2+3*4) Some method calls are used as statements, because they do something; some are used as expressions, because they produce a value.

System.out.println()

(top)

Our programs will often need to display results or other information for the person using the program. The simplest way to do this is to "print" the data in Terminal window from which the program is run. In Java, we do this by calling the built-in method named System.out.println. This method is used as a statement; it takes a single parameter, which should be either a String or something that Java knows how to convert into a String. What this will do is print the string, then advance to the next line.

Sometimes you might want to print without advancing to the next line. You can do that with the method System.out.print, which is otherwise the same as println.

String operations

(top)

The one operator we've seen so far for the type String is the use of + for concatenation. A useful trick takes advantage of the fact that if you try to concatenate a non-String to a String, in

""+5
Java will convert the non-String into a String, if it knows how. Conveniently, Java knows how to convert each of its built-in types (including int) into strings.

The string type also has a lot of methods that you can use in expressions. Below are a few methods that might be especially useful in this lab, displayed as they appear in the (much-longer) Java documentation.

To explain the notation, consider the first entry, for charAt. What it means is that if you have a String variable named s, the call s.charAt(0) gives you the character at position 0 in s. The char at the beginning tells you that the method produces a value of type char; the int index in the parentheses tells you that there is one parameter of type int.

Method Summary
 char charAt(int index)

Returns the char value at the specified index.

 boolean equals(String anotherString)

Compares this string to anotherString, returning true if they look the same.

 int length()

Returns the length of this string.

 String substring(int beginIndex)

Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

 "unhappy".substring(2) returns "happy"
 "Harbison".substring(3) returns "bison"
 "emptiness".substring(9) returns "" (an empty string)
 
 String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"
 

Cary Gray
Last modified: Mon Jan 17 14:36:10 CST 2011