Yes, a String
reference is often a parameter.
String
References as Parameters
Some methods require a parameter that is a reference to a String
object.
For example,
String stringA = "Random Jottings"; String stringB = "Lyrical Ballads"; if ( stringA.equals( stringB ) ) System.out.println("They are equal."); else System.out.println("They are different.");
The String
referred to by stringA
has an equals()
method.
That method is
called with a parameter, a reference to stringB
.
The method checks if both strings contain identical characters, and
if so, evaluates to true.
Careful:
The previous paragraph is correctly stated, but awkward.
People often say "String" when they really mean "reference to a String".
This is fine,
but remember that a variable like stringA
does not contain an object,
but only a reference to an object.
This may seem picky, but there is nothing quite as picky as a computer.
Students who are not careful about this
often run into problems.
What is usually said | Careful meaning |
---|---|
The equals method of stringA
is called with stringB . |
The equals method of the String referenced
by stringA
is called with a reference to stringB . |
(Review:) Examine the following snippet of code. Answer the questions using careful words (like the above on the right).
String a; Point b;