Java Reference
In-Depth Information
consolenextInt becomes a new identifier. If you used consolenextInt in a program,
the compiler (could) generate an undeclared identifier syntax error. Similarly, omitting
the parentheses, as in console.nextInt , also results in a syntax error.
Usually, several methods and/or variables are associated with a particular class ,each
doing a specific job. In the dot notation, the dot separates the class variable name,
that is, the object name from the member, or method, name. It is also worth noting
that methods are distinguished from (reference) variables by the presence of parentheses, and
methods that have no parameters must have empty parentheses (like in nextInt() ). For
example, console is the name of a (reference) variable, and nextInt is the name of
amethod.
3
class String
This section explains how to use String methods to manipulate strings. First, we review
some terminology that we typically use while working with strings and the class String .
Consider the following statements:
String name; //Line 1
name = "Lisa Johnson"; //Line 2
The statement in Line 1 declares name to be a String variable. The statement in Line 2
creates the string "Lisa Johnson" and assigns it to name .
In the statement in Line 2, we usually say that the String object, or the string "Lisa
Johnson" , is assigned to the String variable or the variable name . In reality, as explained
before, a String object with the value "Lisa Johnson" is instantiated (if it has not
already been created) and the address of the object is stored in name . Whenever we
use the term ''the string name '', we are referring to the object containing the string
"Lisa Johnson" . Similarly, when we use the terms (reference) variable name or String
variable name , we simply mean name , whose value is an address.
The remainder of this section describes various features of the class String . In Chapter
2, you learned that two strings can be joined using the operator +. The class String
provides various methods that allow us to process strings in various ways. For example,
we can find the length of a string, extract part of a string, find the position of a particular
string in another string, convert a number into a string, and convert a numeric string into
a number.
Each method associated with the class String implements a specific operation and has
a specific name. For example, the method for determining the length of a string is named
length , and the method for extracting a string from within another string is named
substring .
As explained in the earlier section, Using Predefined Classes and Methods in a Program,
in general, to use a method you must know the name of the class containing the
 
 
Search WWH ::




Custom Search