The tutorial has so far introduced several variables of type
String
,
but without much explanation. You learn more about strings in this section, and also
find out when and how to use operators. Strings
Handling strings in C is labor-intensive because they're null-terminated arrays of 8-bit characters that you must manipulate. In the Java language, strings are first-class objects of typeString
, with methods that help you
manipulate them. (The closest Java code gets to the C world with regard to strings
is the char
primitive data type, which can hold a single Unicode
character, such as a.) You've already seen how to instantiate a
String
object and set its
value (back in Listing 4), but you have several other ways
to do that. Here are a couple of ways to create a String
instance with
a value of hello
:
1
| String greeting = "hello"; |
1
| greeting = new String("hello"); |
String
s are first-class objects in the Java language, you can
use new
to instantiate them. Setting a variable of type
String
has the same result, because the Java language creates a
String
object to hold the literal, then assigns that object to the
instance variable. Concatenating strings
You can do many things withString
, and the class has many helpful
methods. Without even using a method, you've already done something interesting with
two String
s by concatenating, or combining, them:
1
| l.info("Name: " + p.getName()); |
+
) sign is shorthand for concatenating String
s
in the Java language. (You incur a performance penalty for doing this type of
concatenation inside a loop, but for now, you don't need to worry about that.) Concatenation example
Now, you can try concatenatingString
s inside of the
Person
class. At this point, you have a name
instance
variable, but it would be nice to have a firstName
and
lastName
. You can then concatenate them when another object
requests Person
's full name. The first thing you need to do is add the new instance variables (at the same location in the source code where
name
is currently defined):
1
2
3
| //private String name; private String firstName; private String lastName; |
name
anymore; you've replaced it with
firstName
and lastName
. Chaining method calls
Now you can generate getters and setters forfirstName
and
lastName
(as shown back in Figure 6), remove
the setName()
method, and change getName()
to look like
this:
1
2
3
| public String getName() { return firstName.concat(" ").concat(lastName); } |
String
, where a modification
to an immutable object always returns the modification (but doesn't change the
original). You then operate on the returned, changed value. Operators
As you might expect, the Java language can do arithmetic, and you've already seen how to assign variables. Now, I give you a brief look at some of the Java language operators you need as your skills improve. The Java language uses two types of operators:- Unary: Only one operand is needed.
- Binary: Two operands are needed.
Table 2. Java language's arithmetic operators
Operator | Usage | Description |
---|---|---|
+ | a + b | Adds a
and b |
+ | +a | Promotes a
to int if it's a byte , short , or
char |
- | a - b | Subtracts
b from a |
- | -a | Arithmetically negates
a |
* | a * b | Multiplies
a and b |
/ | a / b | Divides a
by b |
% | a % b | Returns the remainder
of dividing a by b (the modulus operator)
|
++ | a++ | Increments
a by 1; computes the value of a before
incrementing |
++ | ++a | Increments
a by 1; computes the value of a after
incrementing |
-- | a-- | Decrements
a by 1; computes the value of a before
decrementing |
-- | --a | Decrements
a by 1; computes the value of a after
decrementing |
+= | a += b | Shorthand for
a = a + b |
-= | a -= b | Shorthand for
a = a - b |
*= | a *= b | Shorthand for
a = a * b |
%= | a %= b | Shorthand for
a = a % b |
Additional operators
In addition to the operators in Table 2, you've seen several other symbols that are called operators in the Java language, including:- Period (
.
), which qualifies names of packages and invokes methods - Parentheses (
()
), which delimit a comma-separated list of parameters to a method new
, which (when followed by a constructor name) instantiates an object
0 comments:
Post a Comment