Strings and operators.

 

 

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 type String, 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");
Because Strings 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 with String, and the class has many helpful methods. Without even using a method, you've already done something interesting with two Strings by concatenating, or combining, them:
1
l.info("Name: " + p.getName());
The plus (+) sign is shorthand for concatenating Strings 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 concatenating Strings 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;
You don't need name anymore; you've replaced it with firstName and lastName.

Chaining method calls

Now you can generate getters and setters for firstName 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);
}
This code illustrates chaining of method calls. Chaining is a technique commonly used with immutable objects like 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.
The Java language's arithmetic operators are summarized in Table 2.
Table 2. Java language's arithmetic operators
OperatorUsageDescription
+a + bAdds a and b
++aPromotes a to int if it's a byte, short, or char
-a - bSubtracts b from a
--aArithmetically negates a
*a * bMultiplies a and b
/a / bDivides a by b
%a % bReturns the remainder of dividing a by b (the modulus operator)
++a++Increments a by 1; computes the value of a before incrementing
++++aIncrements a by 1; computes the value of a after incrementing
--a--Decrements a by 1; computes the value of a before decrementing
----aDecrements a by 1; computes the value of a after decrementing
+=a += bShorthand for a = a + b
-=a -= bShorthand for a = a - b
*=a *= bShorthand for a = a * b
%=a %= bShorthand 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
The Java language syntax also includes several operators that are used specifically for conditional programming — that is, programs that respond differently based on different input. You look at those in the next section.
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment