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.
String 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.

 boolean isEmpty()
          Returns true if, and only if, length() is 0.
 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"
 
 String toLowerCase()
          Converts all of the letters in this String to lower case.
 String toUpperCase()
          Converts all of the letters in this String to upper case.
 String trim()
          Returns a copy of the string, with leading and trailing whitespace omitted.

Char operations

(top)

Java also provides a bunch of methods for working on values of type char, in case you need to work on an individual character. Because char is a primitive type, you call these operations in a slightly different way, and their descriptions indicate this by including the keyword static. So, if you have char variables named c1 and c2, the last description below says that you could store into c2 the uppercase equivalent of the value in c1 with
c2 = Character.toUppercase(c1);

You give a static method's full name instead of naming it relative to a variable (instance) of the type, and you have to pass it a value to work on as a parameter.

Character Method Summary
static int digit(char ch, int radix)
          Returns the numeric value of the character ch in the specified radix.
static char forDigit(int digit, int radix)
          Determines the character representation for a specific digit in the specified radix.
static boolean isDigit(char ch)
          Determines if the specified character is a digit.
static boolean isLetter(char ch)
          Determines if the specified character is a letter.
static boolean isLowerCase(char ch)
          Determines if the specified character is a lowercase character.
static boolean isUpperCase(char ch)
          Determines if the specified character is an uppercase character.
static boolean isWhitespace(char ch)
          Determines if the specified character is white space according to Java.
static char toLowerCase(char ch)
          Converts the character argument to lowercase.
static char toUpperCase(char ch)
          Converts the character argument to uppercase.

Cary Gray
Last modified: Sun Sep 4 16:27:45 CDT 2011